Publish Article
Go Freelance

How to send XML data using HttpWebRequest?


If you are seeing this section and do not see download links, this means that you are not logged into our site. If you already are a member, click on the login link and login into site and come back to this page for downloading the control files. If you are not a member, click on registration link to become a Winista member and download the control for free.

Some time back I posted an article Use HttpWebRequest To send POST HTTP request to another web server showing how HttpWebRequest object can be used to send data to another web application. In that article we showed how data can be sent as key-value pair in FORM format. These days lot of Service Oriented Applications (SOA) expose their services through exchange of XML messages. What that means is that those applications expect client application to send the data to their listening page or URL as XML document or XML document node.

This article demonstrates both sides of the connection - sender and receiver. I will describe what a sender needs to do to make sure that data gets transmitted to the service in XML format, and read the XML response from service. And what a receiver needs to do to make sure that received is in XML format and response is sent back in XML format too.

Sender Application

The guts of the mechanism are still the same as our previous article. That is we will be using HttpWebRequest object to send the data to the recieving end. The key points that differ from the conventional operations are as follows.

  • Set ContentType property of Request to text/html.
  • Use InputStream property of Request to write XML document in it.
byte [] reqBytes = System.Text.UTF8Encoding.UTF8.GetBytes(oReqDoc.InnerXml);
oWebReq.ContentLength = reqBytes.Length;
oWebReq.Method = "POST";
oWebReq.ContentType = "text/xml";
System.IO.Stream oReqStream = oWebReq.GetRequestStream();
oReqStream.Write(reqBytes, 0, reqBytes.Length);							

Receiving Application

Since the receiving application is expecting that all callers will send data in XML format, so first thing it has to check is if the content is actually in that format. And if the content format is valid, then it will be reading the data sent by the caller from the input stream of Request object. Following are the steps that receiving application will need to perform to process the data sent by the caller.

  • Check ContentType property of Request object and make sure that it is of text/xml type.
  • Read the data from InputStream property of Request object.
  • Process the input XML data
  • And now send the response in XML format. ContentType property of Response object should be set to text/html
  • Write the response XML document in OutputStream of Response object. And make sure that you end the response after writing the response document.
if (String.Compare(this.Request.ContentType, "text/xml", true) != 0)
	throw new ApplicationException("Invalid content type specified");
Response.ContentType = "text/xml";
System.Xml.XmlDocument oRespDoc = prepareResponseDocument(strName);

Sample Project

Sample solution included with this article contains following web applications.

  • XmlConentExchange - Sender Application
  • SampleNameService - Receiver Application

To load this solution you will need Visual Studio 2005 and Visual Studio 2005 Web Application Projects add-on.


If you have any questions please post a comment in our discussion forums.

Go Freelance
Home     About us     Contact us    Copyright    Privacy Policy    Return Policy    Advertisers
Copyright © Netomatix