Note that there are some explanatory texts on larger screens.

plurals
  1. POsending Xml in HTTP POST in WCF
    primarykey
    data
    text
    <p>I am using wcf service in .net 3.5 to send xml using http post request.</p> <p>But the problem is when i set request.ContentType= "text/xml" it throws below exception</p> <p>The remote server returned an error: (400) Bad Request.</p> <p>i have go through a lot of articles but couldnot find any possible solution..</p> <p>why it is not supporting ContentType="text/xml" ?</p> <p>here is my code</p> <p>// service contract</p> <pre><code>[OperationContract(Name = "PostSampleMethod")] [WebInvoke(Method = "POST",UriTemplate = "PostSampleMethod/New")] string PostSampleMethod(Stream data); </code></pre> <p>//.....</p> <pre><code> 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; } </code></pre> <p>//WEB CONFIG...</p> <pre><code> &lt;?xml version="1.0"?&gt; &lt;configuration&gt; &lt;system.web&gt; &lt;compilation debug="true" targetFramework="4.0" /&gt; &lt;/system.web&gt; &lt;system.serviceModel&gt; &lt;services&gt; &lt;service name="DemoHttpPost.HttpPost" behaviorConfiguration="ServiceBehaviour"&gt; &lt;endpoint address="" binding="webHttpBinding" contract="DemoHttpPost.IHttpPost" behaviorConfiguration="web" &gt; &lt;/endpoint&gt; &lt;/service&gt; &lt;/services&gt; &lt;behaviors&gt; &lt;serviceBehaviors&gt; &lt;behavior name="ServiceBehaviour"&gt; &lt;!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment --&gt; &lt;serviceMetadata httpGetEnabled="true"/&gt; &lt;!-- 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 --&gt; &lt;serviceDebug includeExceptionDetailInFaults="false"/&gt; &lt;/behavior&gt; &lt;/serviceBehaviors&gt; &lt;endpointBehaviors&gt; &lt;behavior name="web" &gt; &lt;webHttp/&gt; &lt;/behavior&gt; &lt;/endpointBehaviors&gt; &lt;/behaviors&gt; &lt;serviceHostingEnvironment multipleSiteBindingsEnabled="true" /&gt; &lt;/system.serviceModel&gt; &lt;system.webServer&gt; &lt;modules runAllManagedModulesForAllRequests="true"/&gt; &lt;/system.webServer&gt; &lt;/configuration&gt; </code></pre> <p>//....</p> <p>and here is the code for calling Service.</p> <pre><code>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("&lt;?xml version='1.0'?&gt;&lt;id&gt;"); messagereturn.Append("123456"); messagereturn.Append("&lt;/id&gt;"); 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(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload