Recently I came across an interesting requirement to send XML data to a particular URL using POST, means i need to post some XML data to a URL. I came to answer within a couple hrs of “googling” and R&D. Below mentioned is the sample code that I have created to explain the technique. Scenario defines here explains data sending between 2 websites, one is requestor website, which send xml POST data and other is responder website, which collect data from requestor website.
The step by step instructions are as follows.
- Open a Microsoft Visual Studio instance and from the file menu select the option to create ASP.NET website/web application. Create two websites using this option and name these websites with user-friendly names such as Requestor and Responder. For easier developer view make these websites under a single visual studio solution.
- By default there will be a default.aspx page inside each website. Just delete that page, we can create new pages for our sample
- Add a new page to the Requestor website. We can name this page as requestor.aspx. Our aim is to post XML data from requestor.aspx to the responder.aspx of the Responder website. Using the responder page we will save that XML data to a text (.txt) file. Add a new page to the Responder website name this page as requestor.aspx.
- Host Responder website on your local IIS.
- Select the Requestor website. Design the Requestor page as shown on the image below.
One main Multiline textbox for XML Data, a Textbox for entering URL to send the XML data and a Button
6.Handle the click event of the button, add the following the piece of code to that event.
protected void btnPostXml_Click(object sender, EventArgs e) { System.Net.WebRequest req = null; System.Net.WebResponse rsp = null; try { string uri = txtURI.Text; req = System.Net.WebRequest.Create ( uri ); req.Method = "POST"; req.ContentType = "text/xml"; System.IO.StreamWriter writer = new System.IO.StreamWriter ( req.GetRequestStream () ); writer.WriteLine ( txtXMLData.Text ); writer.Close (); rsp = req.GetResponse (); } catch { throw; } finally { if (req != null) req.GetRequestStream ().Close (); if (rsp != null) rsp.GetResponseStream ().Close (); } }
7.Select the Responder website. Take the Page Load event of the Responder.aspx page. Add the following piece of code to the Load event.
protected void Page_Load(object sender, EventArgs e) { Page.Response.ContentType = "text/xml"; System.IO.StreamReader reader = new System.IO.StreamReader ( Page.Request.InputStream ); String xmlData = reader.ReadToEnd (); System.IO.StreamWriter SW; SW = File.CreateText ( Server.MapPath(".")+@"\"+ Guid.NewGuid () + ".txt" ); SW.WriteLine ( xmlData ); SW.Close (); }
Here we are reading the stream of data from the page request and writing that data to a text file.
Note: On this sample i have made the ValidateRequest=”false” for the requestor to accept the XML Data on the form post.
This methodology can also be used as a CALLBACK to a URL with some data post.
Take the code sample(VS 2010 needed)
Thanks for reading this post. Please comment if you have any issues/doubt with the code.
This code does not work
The request does not reach the server
Hello Raju,
No, this code works if follow the mentioned steps properly. You can also do a test with both[responder & requestor] page on the same website. Also please check this ASP.NET sample application. Make sure you host this application under a virtual directory and change the URL between the code properly.
Use this link to download sample code.
http://www.4shared.com/file/_8VDMcFK/PostXMLData.html
This code doesn’t work properly… Please say it clearly
Hello Arya,
Please down the code from this link http://www.4shared.com/file/_8VDMcFK/PostXMLData.html .
There is Requestor and Responder pages on that web application. The Requestor page requests the “responder.aspx” page and along with that sends the XML on POST. Just host this web application on your local IIS and run requestor.aspx. You can see some XML data posted to “responder.aspx” on request and the data is saved on a .txt file with GUID name.
Please check with this downloaded code and verify . THanks
Excellent article. Your article saves me a lot of time. Your code work fine with me. Thanks very much.
I google the same topic but the codes in result search sit es do not work or have poor explanation for the recesiver side, only posted code for sender/poster side focus.
Als, I search this topic (webRequest/WebResponse) on MS Sites but in the posted article MS sites there is no any explanation about how to write codes for the receiver/posted side; they heavily focus on codes for sender/poster site.
Look at this:
http://msdn.microsoft.com/en-us/library/debx8sh9.aspx
Thanks huan
Abin,
Can you show the version to store, in the receiver side, the posted XML string to a SQL Server database table (using ADO.NET)?
Thanks, Huan
Excellent article. But I have a problem when testing on my pc. Sends me this error: A potentially dangerous Request.Form value was detected from the client (txtData=”<?xml version="1.0" …"). I use Visual Studio 2008.
Hi Luis,
Can you please try this?. This issue occurs when we inject any mark up in a POST/GET. Just go to this link.
http://www.codeproject.com/Tips/297679/A-potentially-dangerous-Request-Form-value-was-det .
See my note on the end of the article.
Note: On this sample i have made the ValidateRequest=”false” for the requestor to accept the XML Data on the form post.
Luis, try this code. http://sdrv.ms/K1DmlX. You need to VS 2010 to run. or you can take the code & Web.Config changes to your VS 2008 code
hi sir, i have 1 question, how can this Requestor website can read the xml file??…and how can the Responder website can read and display the data in xml format???
Hi Sida,
Can you please try downloading my sample code?. That is pretty self explanatory. http://sdrv.ms/K1DmlX.