2012-01-20 108 views
1

我試着用restsharp這樣做,但沒有運氣......所以我想做一個職位到休息Web服務。我得到了示例XML如下所示:問題,而發佈到休息服務

<?xml version="1.0"?> 
<root> 
<request> 
<APIClientID>0</APIClientID > 
<Version>0</Version> 
<APIPassword>password</APIPassword > 
<Function>functionName</Function > 
<Params> 
<UserId>(current-datetime)</UserId > 
<page>example.aspx</page> 
<application>appName</application> 
<function>functionName</function> 
</Params> 
</request > 
</root > 

的代碼,我試圖做後用是:

HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; 
     request.Method = "POST"; 
     request.ContentType = "text/xml"; 

     byte[] byteData = UTF8Encoding.UTF8.GetBytes(requestXML); 

     request.ContentLength = byteData.Length; 

     // Write data 
     using (Stream postStream = request.GetRequestStream()) 
     { 
      postStream.Write(byteData, 0, byteData.Length); 
     } 

     HttpWebResponse response = request.GetResponse() as HttpWebResponse; 
     StreamReader reader = new StreamReader(response.GetResponseStream()); 

     string responseString = reader.ReadToEnd(); 

然而,每一次我嘗試做後期時我得到的回是服務的通用獲取響應。任何幫助,我可能會做錯了將不勝感激。謝謝!

我想這可能有助於張貼,我得到的服務找回XML ...

<Function>TransAPIStats</Function> 
<Method> 
<post> 
<Description>Post api statistics to the DB via the API.</Description> 
<Params> 
<Client> 
<Required>true</Required> 
<Description>String</Description> 
</Client> 
<Page> 
<Required>true</Required> 
<Description>Integer</Description> 
</Page> 
<Application> 
<Required>true</Required> 
<Description>String</Description> 
</Application> 
<Function> 
<Required>true</Required> 
<Description>String</Description> 
</Function> 
</Params> 
<Return> 
<Result>String</Result> 
<Status>String</Status> 
<Description>String</Description> 
</Return> 
</post> 
</Method> 

我想改變,我是給我以匹配所列參數的示例XML api xml,但它仍然只是做同樣的事情。我有一種感覺,這是我的問題,但它似乎並不想工作...

回答

0

試試這個;

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 
request.Method = "POST"; 
request.ContentType = "application/xml"; 

using (var postStream = new StreamWriter(request.GetRequestStream())) 
{ 
    postStream.Write(requestXML); 
} 

string responseString = null; 
using (var response = (HttpWebResponse)request.GetResponse()) 
using (var dataStream = response.GetResponseStream()) 
using (var reader = new StreamReader(dataStream)) 
{ 
    responseString = reader.ReadToEnd(); 
} 
+0

感謝您的迴應!我做了更改,但它仍然只是返回獲取響應。我在帖子中增加了一些細節,希望能夠提供更多細節來幫助... – Arthurdent510