2013-06-01 81 views
0

我在.net 3.5中使用wcf服務使用http post請求發送xml。在WCF中發送HTTP POST中的Xml

但問題是,當我設置request.ContentType =「文本/ xml」的它拋出異常

下面遠程服務器返回一個錯誤:(400)錯誤的請求。

我有經歷了很多文章,但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; 
    } 

// WEB配置...

 <?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

是的,我調用了它,但它拋出了我已經提到的異常。 –

+0

嘗試切換[WebInvoke(Method =「POST」,GET –

回答

0

例如

的ServiceContract

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

服務

public void Send(string data) 
    { 
     Console.WriteLine(data); 
    } 

客戶端,其中 「web服務」 是端點名稱和IService - 服務合同

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