Wednesday, March 02, 2005

Programatically POST xml to a page

In .NET you can use the HttpWebRequest class to make page requests. The HttpWebRequest class supports the GET and POST verbs. Suppose that you wanted to retrieve xml from a page by issuing a GET request for the page http://localhost/Test/Default.aspx?Name=Test&Email=Test. You could do this with the following code:


string uriString = "http://localhost/Test/Default.aspx?Name=Test&Email=Test";
HttpWebRequest httpRequest = null;
HttpWebResponse response = null;

string xml = "";
Uri uri = new Uri(uriString);
httpRequest = (HttpWebRequest)WebRequest.Create(uri);
response = (HttpWebResponse)request.GetResponse( );

if (response != null)
{
Stream responseStream = response.GetResponseStream( );
StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);

try
{
xml = reader.ReadToEnd( );
}
finally
{
reader.Close( );
}
}

This will return the xml string into the xml variable.

Now suppose that you wanted to POST xml to the same web page. You can either post the xml directly to the page, or you could put the xml into a form variable and post it to the page. To post the xml as part of the form (in the 'BodyText' variable), you could do this:


XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("test.xml");
body = xmlDoc.OuterXml;

string uriString = "http://localhost/Test/Default.aspx?Name=Test&Email=Test";
HttpWebRequest httpRequest = null;
HttpWebResponse response = null;

string xml = "";
Uri uri = new Uri(uriString);
httpRequest = (HttpWebRequest)WebRequest.Create(uri);

httpRequest.Method = "POST";
postData = HttpUtility.HtmlEncode(postData);
postData = HttpUtility.UrlEncode(postData);
postData = "BodyText=" + postData;
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] bytes = encoding.GetBytes(postData);
httpRequest.ContentLength = postData.Length;
httpRequest.KeepAlive = false;
httpRequest.ContentType = "application/x-www-form-urlencoded";
Stream requestStream = httpRequest.GetRequestStream( );
requestStream.Write(bytes,0,bytes.Length);
requestStream.Close( );
response = (HttpWebResponse)request.GetResponse( );

if (response != null)
{
Stream responseStream = response.GetResponseStream( );
StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);

try
{
xml = reader.ReadToEnd( );
}
finally
{
reader.Close( );
}
}

Be careful to not confuse HtmlEncode and UrlEncode. It seems that even though the content type is set to "application/x-www-form-urlencoded" that you should NOT JUST URL encode the xml string. The HttpWebRequest object will appear to process the message for some characters if the string is UrlEncoded, but for other characters, you will receive a '(500) Internal Server' error (this case is especially for xml strings that contain the < character). You should first HtmlEncode, then UrlEncode the string, then prefix the form variable. On the server side, you only have to HtmlDecode the string since the UrlDecoding is automatically done for you.

Comments: Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?