How to read XML data sent in a post request

Hi,

A few days ago I posted about how we can send XML data as a post request to any URL. You can read the stuff here. After that many people have asked me on how we can read the data when data is sent as a Post Request.

Reading data when it is sent as a post request value is very simple. We use the Request.InputStream to read the incoming stream request. Here is a glimpse of code to work with the same.

byte[] b = new byte[Request.ContentLength];

Request.InputStream.Read(b, 0, Request.ContentLength);

Here we first get the length of the request and then read the same in an array of byte.

We can convert the bytes into a string by using correct encoding. (Note: I am using the UTF8 encoding for example).

string s = System.Text.UTF8Encoding.UTF8.GetString(b);

That’s all we have to do to read the data send as post. Now we can do whatever we want to do with the request value.

Vikram


Share this post   Email it |  digg it! |  reddit! |  bookmark it!

Feedback

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 
Copyright © 2006 - 2008 Vikram Lakhotia