2010-07-29 40 views
2

我很難得到一個簡單的RESTful Web服務應用程序,使用我的C#REST客戶端。我正嘗試使用POST方法發送純XML。使用C#REST客戶端的另一個(400)錯誤請求:如何POST純XML?

我創建了一個啓動並運行的WCF Rest服務(我可以在瀏覽器中看到服務測試頁)。此外,我可以通過Fiddler調用我的WCF Rest服務,它的響應正確(我會得到一個返回值)。

但是最大的問題是REST客戶端。我已經完成了使用各種指南的一切。另外,相同的客戶端代碼與基於Java的REST服務一起使用。但在.NET中,我得到的錯誤是「遠程服務器返回錯誤:(400)錯誤請求。」這個異常發生在Request.GetResponse()

因爲我開發了WCF服務,我用它來指定客戶端(app.config)和服務器(web.config)的配置設置,但.NET的情況如何基於REST的Web服務?我的意思是,我是否需要爲客戶端(app.config)和服務器(web.config)指定相同的綁定設置(webHttpBinding)?

下面是客戶端代碼:

XmlDocument TestDocument = new XmlDocument(); 
TestDocument.Load(XMLFilePath); 
byte[] RequestBytes = Encoding.GetEncoding("iso-8859-1").GetBytes(TestDocument.OuterXml); 

Uri uri = new Uri("http://localhost:2319/MyRESTService.svc/receive"); 

HttpWebRequest Request = (HttpWebRequest)HttpWebRequest.Create(uri);  

Request.ContentLength = RequestBytes.Length; 
Request.Method = "POST"; 
Request.ContentType = "text/xml"; 

Stream RequestStream = Request.GetRequestStream(); 
RequestStream.Write(RequestBytes, 0, RequestBytes.Length); 
RequestStream.Close(); 

HttpWebResponse response = (HttpWebResponse)Request.GetResponse(); 
StreamReader reader = new StreamReader(response.GetResponseStream()); 
string ResponseMessage = reader.ReadToEnd(); 
response.Close(); 

我的WCF REST服務的web.config文件看起來像這樣(我應指定在C#REST客戶端的app.config相同的設置?) :

<system.serviceModel> 
<services> 
    <service name="MyRESTService.MyRESTService"> 
    <endpoint binding="webHttpBinding" contract="MyRESTService.IMyRESTService" 
       behaviorConfiguration="webHttp"/> 
    </service> 
</services> 

<behaviors> 

    <endpointBehaviors> 
    <behavior name="webHttp"> 
     <webHttp/> 
    </behavior> 
    </endpointBehaviors> 
</behaviors> 

我的WCF REST服務的接口看起來是這樣的:

[ServiceContract] 
public interface IMyRESTService 

[OperationContract] 
[WebInvoke(
    Method = "POST", 
    UriTemplate = "/receive", 
    RequestFormat = WebMessageFormat.Xml, 
    ResponseFormat = WebMessageFormat.Xml, 
    BodyStyle=WebMessageBodyStyle.Bare)] 
string Receive(string xml); 

據我所知,這是一個客戶端問題,由於在客戶端發現問題而引發異常。但是,在我的C#Rest Client中特別引起此異常的是什麼?

的XML看起來是這樣的:

<?xml version="1.0" encoding="iso-8859-1"?> 
<MATMAS02> 
<IDOC BEGIN="1"> 
<EDI_DC40 SEGMENT="1"> 
...... 
+0

此問題已在以下後得到解決// stackoverflow.com/questions/3372335/what-is-the-correct-uri-for-sending-parameters-via-post-in-wcf-rest-services – user405723 2010-08-03 07:45:06

回答

相關問題