C# to POST HTTP with XML

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Ive been working on a what should be a simple web application. My code will get more complex later, but right now I am hard coding a username/password and an XML snippet into a button. The same combination of information works if I send it manually
via an HTML page that has a standard old fashioned "Post" and "Action". Unfortunately, Im stuck. If I set my ContentType to XML I get a response from their site that says "Please supply input file for processing in HTTP POST format".
If I set it to x-www-form-urlencoded I get an error 500.
I dont know if I need to try a different methodology or if I need more information in my header to distinguish the "body" as XML, or if I need to modify my code to send the XML as if it were embedded in a form. First, here is a compressed version
of the HTML page that works:
<form name="myForm" method="post" action=" https://blah.blah.gov/TheMagicForm.cfm https://blah.blah.gov/TheMagicForm.cfm

<input type="hidden" name="izzit" value="<?xml version=1.0 standalone=yes?><br/>
<MORTGAGEDATA MISMOVersionID=1.0.1><br/>
. . . <ALL the other XML tags> ...
</MORTGAGEDATA> <br/>
<input type="submit" value="Post XML <br/>
</form>

<pre class="prettyprint HttpWebRequest request = null;
WebResponse response = null;

try
{

string myURL = "https://blah.blah.gov/themagicform.cfm"; //The URL for my requests:
request = (HttpWebRequest)WebRequest.Create(myURL); // Create a request using a URL that can receive a post.
request.Credentials = new NetworkCredential("MyUserName", "MyPassWord"); //The credentials for the website.
request.Method = "POST"; // Set the Method property of the request to POST.
//request.ContentType = "text/xml"; // Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
// Specify Headers
request.Headers.Add("HTTP_HOST", "MyZone");
request.Headers.Add("HTTP_USER_AGENT", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)");
request.Headers.Add("HTTP_ACCEPT", "text/xml");

StreamWriter writer = new StreamWriter(request.GetRequestStream()); // Wrap the request stream with a text-based writer

writer.WriteLine(XMLDoc); // Write the xml text into the stream
writer.Close();

/* Previously I tried:
string postData = XMLDoc; // Create POST data and convert it to a byte array.
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentLength = byteArray.Length; // Set the ContentLength property of the WebRequest.
Stream dataStream = request.GetRequestStream(); // Get the request stream.
dataStream.Write(byteArray, 0, byteArray.Length); // Write the data to the request stream.
dataStream.Close(); // Close the Stream object - which submits the "form" data.
*/

Stream dataStream = null;

// Send the data to the webserver
response = request.GetResponse(); // Get the response.
lbl_ResponseInfo.Text = ((HttpWebResponse)response).StatusDescription.ToString(); // Display the status.

dataStream = response.GetResponseStream(); // Get the stream containing content returned by the server.
StreamReader incomingStreamReader = new StreamReader(dataStream); // Open the stream using a StreamReader for easy access.
string responseFromServer = incomingStreamReader.ReadToEnd();// Read the content.
// Display the content. XMLResponse is a special type of label on my asp page
XMLResponse.Visible = true;
XMLResponse.TransformSource = "defaultss.xslt";
XMLResponse.DocumentContent = responseFromServer;
lbl_Received.Visible = true;

// Clean up the streams.
incomingStreamReader.Close();
dataStream.Close();
response.Close();

//Display the XML we sent
XMLSent.Visible = true;
XMLSent.TransformSource = "defaultss.xslt";
XMLSent.DocumentContent = XMLDoc;
lbl_Sent.Visible = true;
}

catch (WebException webEx)
{
lbl_ResponseInfo.Text = webEx.Message.ToString();
}
catch (Exception ex)
{
lbl_ResponseInfo.Text = ex.Message.ToString();
}
finally
{
if (request != null) request.GetRequestStream().Close();
if (response != null) response.GetResponseStream().Close();

}[/code]
<br/>


<br/>

View the full article
 
Back
Top