p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ASP.NET 1.0 and 1.1 Professional (http://p2p.wrox.com/forumdisplay.php?f=57)
-   -   C# post xml using HttpWebRequest/Response (http://p2p.wrox.com/showthread.php?t=57098)

datakix April 25th, 2007 10:03 AM

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


dparsons April 25th, 2007 10:12 AM

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??
================================================== =========

Myke Black December 7th, 2011 02:47 PM

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!

hu_yang April 4th, 2012 02:50 AM

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



darksheep July 20th, 2013 03:34 PM

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

tawfeeq October 16th, 2015 09:45 AM

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.

santhu_lm June 14th, 2016 07:47 AM

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();"

All times are GMT -4. The time now is 01:50 AM.

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