2013-06-01 1 views
0

.net 3.5에서 wcf 서비스를 사용하여 http 게시 요청을 사용하여 xml을 보내고 있습니다. (400) 잘못된 요청 :WCF에서 HTTP POST로 Xml 보내기

은 그러나 문제는 request.ContentType = "text/xml"로 설정 될 때 원격 서버가 에러를 반환 예외

아래 발생한다.

난 기사를 많이 통과하지만, 가능한 해결책을 찾기 couldnot이

..

는 ContentType을 = "텍스트/XML을"지원되지 않는 이유? 여기

내 코드입니다

// 서비스 계약

[OperationContract(Name = "PostSampleMethod")] 
[WebInvoke(Method = "POST",UriTemplate = "PostSampleMethod/New")] 
string PostSampleMethod(Stream data); 

// .....

public string PostSampleMethod(Stream data) 
    { 
     // convert Stream Data to StreamReader 
     StreamReader reader = new StreamReader(data); 
     // Read StreamReader data as string 
     string xmlString = reader.ReadToEnd(); 
     XmlDocument doc = new XmlDocument(); 
     doc.LoadXml(xmlString); 

     string returnValue = doc.InnerXml; 
     // return the XMLString data 
     return returnValue; 
    } 

// 웹 CONFIG ...

 <?xml version="1.0"?> 
<configuration> 

    <system.web> 
    <compilation debug="true" targetFramework="4.0" /> 
    </system.web> 
    <system.serviceModel> 

    <services> 
     <service name="DemoHttpPost.HttpPost" behaviorConfiguration="ServiceBehaviour"> 
     <endpoint address="" binding="webHttpBinding" contract="DemoHttpPost.IHttpPost" behaviorConfiguration="web" > 

     </endpoint> 

     </service> 

    </services> 

    <behaviors> 

     <serviceBehaviors> 

     <behavior name="ServiceBehaviour"> 
      <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment --> 
      <serviceMetadata httpGetEnabled="true"/> 
      <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> 
      <serviceDebug includeExceptionDetailInFaults="false"/> 
     </behavior> 
     </serviceBehaviors> 
     <endpointBehaviors> 

     <behavior name="web" > 
      <webHttp/> 
     </behavior> 
     </endpointBehaviors> 
    </behaviors> 
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 
    </system.serviceModel> 
<system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    </system.webServer> 

</configuration> 

// ....

및 여기 서비스를 호출하기위한 코드입니다. 예를 들어

private void button1_Click(object sender, EventArgs e) 
     { 

      // Create a request using a URL that can receive a post. 
      HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost/DemoHttpPost/HttpPost.svc/PostSampleMethod/New"); 
      // Set the Method property of the request to POST. 
      request.Method = "POST"; 
      // Create POST data and convert it to a byte array. 
      StringBuilder messagereturn = new StringBuilder(); 
      messagereturn.Append("<?xml version='1.0'?><id>"); 
      messagereturn.Append("123456"); 
      messagereturn.Append("</id>"); 
      XmlDocument doc = new XmlDocument(); 
      doc.LoadXml(messagereturn.ToString()); 

      string postData =doc.InnerXml; 
      byte[] byteArray = Encoding.UTF8.GetBytes(postData); 
      // Set the ContentType property of the WebRequest. 
      request.ContentType = "text/xml"; 
      // Set the ContentLength property of the WebRequest. 
      request.ContentLength = byteArray.Length; 
      // Get the request stream. 
      Stream dataStream = request.GetRequestStream(); 
      // Write the data to the request stream. 
      dataStream.Write(byteArray, 0, byteArray.Length); 
      // Close the Stream object. 
      dataStream.Close(); 
      // Get the response. 

      WebResponse response = request.GetResponse(); 
      // Display the status. 
      HttpWebResponse webres= (HttpWebResponse)response; 

      StreamReader reader = new StreamReader(webres.GetResponseStream()); 
      // Read the content. 
      string responseFromServer = reader.ReadToEnd(); 
      // Display the content. 
      richTextBox1.Text = responseFromServer; 
      // Clean up the streams. 
      reader.Close(); 
      dataStream.Close(); 
      response.Close(); 
     } 
+0

http : //localhost/DemoHttpPost/HttpPost.svc/PostSampleMethod/New를 호출하여 xml –

+0

예를 호출했는지 확인하려고 했습니까? 이미 호출했으나 이미 언급 한 예외가 throw됩니다. –

+0

시도 전환 [WebInvoke (Method = "POST", GET –

답변

0

의 ServiceContract

[WebInvoke(Method="POST")] 
    [OperationContract] 
    void Send(string data); 

서비스

"WebService에는"엔드 포인트 이름과 IService입니다
public void Send(string data) 
    { 
     Console.WriteLine(data); 
    } 

클라이언트 - 서비스 계약

using (var factory = new WebChannelFactory<IService>("WebService")) 
    { 
     var channel = factory.CreateChannel(); 
     channel.Send(@"<system.web>5</system.web>"); 
    } 
관련 문제