Click here to Skip to main content
12,248,681 members (57,777 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

78.7K views
14 bookmarked
Posted

How to send XML data to a Webpage using “POST” Method

, 27 Mar 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
The scenario defined here explains data sending between 2 websites, one is the requestor website, which sends XML POST data and the other is the responder website, which collects data from the requestor website.

Recently I came across an interesting requirement to send XML data to a particular URL using POST, meaning I need to post some XML data to a URL. I arrived at the solution within a couple hours of “Googling” and R&D. Mentioned below is the sample code that I have created to explain the technique. The scenario defined here explains data sending between 2 websites, one is the requestor website, which sends XML POST data and the other is the responder website, which collects data from the requestor website.

The step by step instructions are as follows:

  1. 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.
  2. By default, there will be a default.aspx page inside each website. Just delete that page, we can create new pages for our sample.
  3. 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.
  4. Host Responder website on your local IIS.
  5. Select the Requestor website. Design the Requestor page as shown in the image below:

    Page design of the Requestor page

    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 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: In 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.

Thanks for reading this post. Please comment if you have any issues/doubts about the code.


License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

abin jaik
Web Developer
India India
I am Abin Jaik Antony, working as a Software Developer.My technology background area extends to MOSS 2007,Microsoft BI,.NET Framework etc.
Visit my blog for more details http://www.abinjaik.com

You may also be interested in...

Comments and Discussions

 
Questionreading the XML data from a Port and sending to a url address Pin
Member 1156445229-Mar-15 5:33
memberMember 1156445229-Mar-15 5:33 
QuestionWonderful, works great, 5 stars for you.. Pin
Janilane21-Nov-12 13:14
memberJanilane21-Nov-12 13:14 
Questionxml Pin
Esam Zain25-Sep-12 12:00
memberEsam Zain25-Sep-12 12:00 
Questionuser credentials Pin
angusbeare27-Mar-12 2:32
memberangusbeare27-Mar-12 2:32 
AnswerRe: user credentials Pin
abin jaik27-Mar-12 4:06
memberabin jaik27-Mar-12 4:06 
GeneralRe: user credentials Pin
angusbeare27-Mar-12 23:22
memberangusbeare27-Mar-12 23:22 
QuestionWhat as for other data type? Pin
sawa2530-Mar-10 20:18
membersawa2530-Mar-10 20:18 
GeneralNice! Pin
Sandeep Mewara27-Mar-10 4:43
memberSandeep Mewara27-Mar-10 4:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160426.1 | Last Updated 27 Mar 2010
Article Copyright 2010 by abin jaik
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid