2010-07-29 3 views
2

저는 C# REST 클라이언트로 작업하는 간단한 RESTful 웹 서비스 응용 프로그램을 얻는 데 어려움을 겪고 있습니다. POST 메서드를 사용하여 순수한 XML을 보내려고합니다.C# REST 클라이언트의 또 다른 (400) 잘못된 요청 : 순수 XML을 POST하는 방법?

저는 실행중인 WCF Rest Service를 만들었습니다 (브라우저에서 서비스 테스트 페이지를 볼 수 있음). 또한 Fiddler를 통해 WCF Rest Service에 전화를 걸면 올바르게 응답합니다 (반환 값을 얻음).

하지만 큰 문제는 REST 클라이언트입니다. 나는 모든 종류의 가이드를 사용하여 모든 것을 해냈다. 또한 동일한 클라이언트 코드가 Java 기반 REST 서비스와 작동합니다. 하지만 .NET에서 내가 얻는 것은 "원격 서버가 오류를 반환했습니다 : (400) Bad Request."라는 오류입니다. 이 예외는 Request.GetResponse() 이후에 발생합니다.

클라이언트 (app.config)와 서버 (web.config)의 구성 설정을 지정하는 데 사용했던 WCF 서비스를 개발했지만 .NET 기반의 RESTful 웹 서비스? 클라이언트 (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 version="1.0" encoding="iso-8859-1"?> 
<MATMAS02> 
<IDOC BEGIN="1"> 
<EDI_DC40 SEGMENT="1"> 
...... 
+0

이 문제는 다음 포스트에서 해결 된

XML은 다음과 같습니다 stackoverflow.com/questions/3372335/what-is-the-correct-uri-for-sending-parameters-via-post-in-wcf-rest-services – user405723

답변

관련 문제