SKG
SKG

Reputation: 1462

C# HTTP Post code not getting expected response

I have a sample htm file with following structure and it POSTs the xml and gets the response xml. I need to do the same with C#. See my C# code below the html.

<html>
<body>
<table>
<tr><td width=10%>&nbsp;</td><td><h2>API Test Form</h2></td></tr>
<tr><td width=10%>&nbsp;</td><td><h3>Command: get_Details </h3></td></tr>

<form action="https://test.test.com/getDetails" method=POST>


<tr>
<td width=10%>&nbsp;</td>
<td>
<textarea name="xml" rows=15 cols=80>

<?xml version="1.0" encoding="UTF-8"?>
<Request>   
    <test1>xcvb</test1>     
</Request>

</textarea>
</td>

</tr>
<tr><td width=10%>&nbsp;</td><td>&nbsp;</td></tr>
<tr><td width=10%>&nbsp;</td><td><input type="submit" value="Submit Request"></td></tr>
</table>
</form>
</body>
</html>



private static string MSDNHttpPost1()
{
    // Create a request using a URL that can receive a post. 
    WebRequest request = WebRequest.Create("https://test.test.com/getDetails");
    // Set the Method property of the request to POST.
    request.Method = "POST";

    // Create POST data and convert it to a byte array.    

    var doc = new XmlDocument();
    doc.Load(@"C:\request.xml");
    string postData = doc.InnerXml;

    byte[] byteArray = Encoding.UTF8.GetBytes(postData);


    // Set the ContentType property of the WebRequest.
    request.ContentType = "application/x-www-form-urlencoded";
    //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.
    Console.WriteLine(((HttpWebResponse)response).StatusDescription);
    // Get the stream containing content returned by the server.
    dataStream = response.GetResponseStream();
    // Open the stream using a StreamReader for easy access.
    StreamReader reader = new StreamReader(dataStream);
    // Read the content.
    string responseFromServer = reader.ReadToEnd();
    // Display the content.
    Console.WriteLine(responseFromServer);
    // Clean up the streams.
    reader.Close();
    dataStream.Close();
    response.Close();

    return responseFromServer;

}

The C# code was adapted from MSDN site. But the response shows me a message with a error which basically says the server could not read the xml file. I have been suggested to include "data="before the xml while posting. But that makes no difference on the response.

Any clues as to what I am missing?.

Upvotes: 1

Views: 1420

Answers (4)

SKG
SKG

Reputation: 1462

As mentioned in the html in the question, the request xml is housed within a TextArea control

<textarea name="xml" rows=15 cols=80>

I prefixed the request xml with "xml=" which did the trick.

Upvotes: 0

TehBoyan
TehBoyan

Reputation: 6890

First of all I would suggest you use Fiddler or similar tool so that you can see exactly what is being send in the POST request of the form that you have when you execute it separately trough a web browser. Then instantiate the HttpWebRequest object and set all it's properties as per what you saw in Fiddler.

Upvotes: 0

richeym
richeym

Reputation: 4079

The correct content-type when posting XML is:

application/xml

The text/xml content-type is obsolete, and also seriously broken.

Upvotes: 1

Dave Kilian
Dave Kilian

Reputation: 1012

When you loaded the XML document, I think you wanted:

string postData = doc.OuterXml;

Upvotes: 0

Related Questions