Wrox Programmer Forums
ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old April 25th, 2007, 10:03 AM
Registered User
Join Date: Oct 2003
Posts: 3
Thanks: 0
Thanked 1 Time in 1 Post
Default C# post xml using HttpWebRequest/Response

For those of you scouring the web looking for a simple routine that sends/receives an XML file using HttpWebRequest/Response here ya go:

    public static XmlDocument PostXMLTransaction(string v_strURL, XmlDocument v_objXMLDoc)
        //Declare XMLResponse document
        XmlDocument XMLResponse = null;

        //Declare an HTTP-specific implementation of the WebRequest class.
        HttpWebRequest objHttpWebRequest;

        //Declare an HTTP-specific implementation of the WebResponse class
        HttpWebResponse objHttpWebResponse = null;

        //Declare a generic view of a sequence of bytes
        Stream objRequestStream = null;
        Stream objResponseStream = null;

        //Declare XMLReader
        XmlTextReader objXMLReader;

        //Creates an HttpWebRequest for the specified URL.
        objHttpWebRequest = (HttpWebRequest)WebRequest.Create(v_strURL);

            //---------- Start HttpRequest

            //Set HttpWebRequest properties
            byte[] bytes;
            bytes = System.Text.Encoding.ASCII.GetBytes(v_objXMLDoc.In nerXml);
            objHttpWebRequest.Method = "POST";
            objHttpWebRequest.ContentLength = bytes.Length;
            objHttpWebRequest.ContentType = "text/xml; encoding='utf-8'";

            //Get Stream object
            objRequestStream = objHttpWebRequest.GetRequestStream();

            //Writes a sequence of bytes to the current stream
            objRequestStream.Write(bytes, 0, bytes.Length);

            //Close stream

            //---------- End HttpRequest

            //Sends the HttpWebRequest, and waits for a response.
            objHttpWebResponse = (HttpWebResponse)objHttpWebRequest.GetResponse();

            //---------- Start HttpResponse
            if (objHttpWebResponse.StatusCode == HttpStatusCode.OK)
                //Get response stream
                objResponseStream = objHttpWebResponse.GetResponseStream();

                //Load response stream into XMLReader
                objXMLReader = new XmlTextReader(objResponseStream);

                //Declare XMLDocument
                XmlDocument xmldoc = new XmlDocument();

                //Set XMLResponse object returned from XMLReader
                XMLResponse = xmldoc;

                //Close XMLReader

            //Close HttpWebResponse
        catch (WebException we)
            //TODO: Add custom exception handling
            throw new Exception(we.Message);
        catch (Exception ex)
            throw new Exception(ex.Message);
            //Close connections

            //Release objects
            objXMLReader = null;
            objRequestStream = null;
            objResponseStream = null;
            objHttpWebResponse = null;
            objHttpWebRequest = null;

        return XMLResponse;

Bill Screen
Senior Web Developer
Universal Orlando Resort

[email protected]
The Following User Says Thank You to datakix For This Useful Post:
Old April 25th, 2007, 10:12 AM
Wrox Author
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons

Very nice. Here is a functional example of what can be achieved with using this type of code:


================================================== =========
Read this if you want to know how to get a correct reply for your question:
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET
================================================== =========
Why can't Programmers, program??
================================================== =========
Old December 7th, 2011, 02:47 PM
Registered User
Join Date: Dec 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default error in code

slight error in your code, in the line:

bytes = System.Text.Encoding.ASCII.GetBytes(v_objXMLDoc.In nerXml);

you should remove the space in InnerXml

thanks for posting this, it was just what I've been search for all evening!
Old April 4th, 2012, 02:50 AM
Posts: n/a
Default C# XML Samples

try this linke, it has some useful samples of C# xml


Old July 20th, 2013, 03:34 PM
Registered User
Join Date: Jul 2013
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Help with website

Hi I tried your example but it does not seem to work with the website or may be i am doing some thing incorrectly.

When I tried to call this code against my web site I get an exception in this area of the code. It seems like the website I am trying to call is not returning any data. If I past the same url in my web browser I see the response come back just fine. Any Idea what I could be doing wrong ?

This is the url I am using " 9f4c261711e46s1s2"

catch (Exception ex)
throw new Exception(ex.Message);
Thanks for you time
Old October 16th, 2015, 09:45 AM
Registered User
Join Date: Oct 2015
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default urgent help

Good Day

Hoping you can assist me, i have successfully implemented your method above into my solution. my aim is to send data over to a web service using HTTPResponse in xml format. after calling your method in my main method
like so " PostXMLTransaction(string v_strURL, XmlDocument v_objXMLDoc);"

i need to know what URL could i use just for testing purposes in place of v_strURL, and what type of value do i use for v_objXMLDoc.

any help will be appreciated.
Old June 14th, 2016, 07:47 AM
Registered User
Join Date: Jun 2016
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Please help me

Hi Datakix,

Please help. I am new to C#.
I am trying to use this code.
During the execution of the below line I am getting the error, "NULLReferenceException::Object reference not set to an instance of an object".

"objRequestStream = objHttpWebRequest.GetRequestStream();"

Similar Threads
Thread Thread Starter Forum Replies Last Post
post a http request/response in new window nomenu dfarkish Javascript How-To 4 August 12th, 2009 08:58 AM
POST response cached keenlearner BOOK: Professional Ajax ISBN: 978-0-471-77778-6 0 March 27th, 2007 03:12 PM
Using Webrequest and response to post data cgmenon ASP.NET 1.0 and 1.1 Professional 3 July 15th, 2006 06:11 AM
Using web request and response to post data to URl cgmenon .NET Framework 1.x 0 July 13th, 2006 10:39 AM
how to post variable on response.redirect????? Swetz Classic ASP Components 3 May 16th, 2006 06:37 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.