Using HTTP Post with XML

Posted on 2007-08-09
Last Modified: 2013-11-05
Hi there

I am uploading some data in XML format and they NEED it in an HTTP Form post but not sure how to do that. Can anyone point me in the right direction
Question by:davoman
    LVL 19

    Expert Comment

    You can use a WebRequest:

                WebRequest rq = WebRequest.Create("<server url>");
                rq.Method = "POST";
                // If your XML data is not in a string, you just need to get its length,
                // set the ContentLength, and write the data to the RequestStream.
                string rqData = <your XML data>;
                rq.ContentLength = rqData.Length;
                Stream s = rq.GetRequestStream();
                StreamWriter sw = new StreamWriter(s);
                WebResponse wr = rq.GetResponse();
                // Handle response...

    You may also need to set some other headers like ContentType, etc.  Talk to the server implementers about what headers are required/expected.
    LVL 19

    Accepted Solution

    Also, sorry about the C# code.  Here's the VB version:

            Dim rq As WebRequest = WebRequest.Create(<server url>)
            rq.Method = "POST"
            Dim rqData As String = <your XML data>
            rq.ContentLength = rqData.Length
            Dim s As Stream = rq.GetRequestStream()
            Dim sw As StreamWriter = New StreamWriter(s)
    LVL 1

    Author Comment

    sorry took so long but had a problem with it not acctually posing any data doing the above
    LVL 19

    Expert Comment

    What exactly is your code and how do you know it is not posting anything?  As they say: "it works on my machine".  Do you capture the network traffic?  Or put another way, can you tell if the problem is that the data is not posted or that you are just not reading it properly in the server code?
    LVL 1

    Author Comment

    managed to adapte some other code but used this elsewhere cheers

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now