p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/)
-   ASP.NET 1.0 and 1.1 Professional (http://p2p.wrox.com/asp-net-1-0-1-1-professional-57/)
-   -   C# post xml using HttpWebRequest/Response (http://p2p.wrox.com/asp-net-1-0-1-1-professional/57098-c-post-xml-using-httpwebrequest-response.html)

datakix April 25th, 2007 11: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);

        try
        {
            //---------- 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
            objRequestStream.Close();

            //---------- 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();
                xmldoc.Load(objXMLReader);

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

                //Close XMLReader
                objXMLReader.Close();
            }

            //Close HttpWebResponse
            objHttpWebResponse.Close();
        }
        catch (WebException we)
        {
            //TODO: Add custom exception handling
            throw new Exception(we.Message);
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            //Close connections
            objRequestStream.Close();
            objResponseStream.Close();
            objHttpWebResponse.Close();

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

        //Return
        return XMLResponse;
    }

Bill Screen
Senior Web Developer
Universal Orlando Resort

Bill.Screen@UniversalOrlando.com

dparsons April 25th, 2007 11:12 AM

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

http://p2p.wrox.com/topic.asp?TOPIC_ID=54726

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET
http://www.wiley.com/WileyCDA/WileyT...470131470.html
================================================== =========
Why can't Programmers, program??
http://www.codinghorror.com/blog/archives/000781.html
================================================== =========

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 03:50 AM

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

http://csharp.net-informations.com/x...mltutorial.htm

yang.

darksheep July 20th, 2013 04: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 "http://10.0.0.3:8080/api?mode=qstatus&output=xml&apikey=a0ebb518a3cc9b7 9f4c261711e46s1s2"

Code:

catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally

Thanks for you time

tawfeeq October 16th, 2015 10: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 08: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 08:02 AM.

Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
2013 John Wiley & Sons, Inc.