Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Professional
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old April 25th, 2007, 10:03 AM
Registered User
 
Join Date: Oct 2003
Location: , , .
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);

        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
Reply With Quote
The Following User Says Thank You to datakix For This Useful Post:
  #2 (permalink)  
Old April 25th, 2007, 10:12 AM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,105
Thanks: 1
Thanked 63 Times in 63 Posts
Send a message via AIM to dparsons
Default

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
================================================== =========
Reply With Quote
  #3 (permalink)  
Old December 7th, 2011, 01:47 PM
Registered User
Points: 3, Level: 1
Points: 3, Level: 1 Points: 3, Level: 1 Points: 3, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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!
Reply With Quote
  #4 (permalink)  
Old April 4th, 2012, 02:50 AM
Authorized User
Points: 102, Level: 2
Points: 102, Level: 2 Points: 102, Level: 2 Points: 102, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2008
Location: , , .
Posts: 30
Thanks: 0
Thanked 1 Time in 1 Post
Default C# XML Samples

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

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

yang.
Reply With Quote
  #5 (permalink)  
Old July 20th, 2013, 03:34 PM
Registered User
Points: 3, Level: 1
Points: 3, Level: 1 Points: 3, Level: 1 Points: 3, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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 "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
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
post a http request/response in new window nomenu dfarkish Javascript How-To 5 February 18th, 2010 12:20 PM
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



All times are GMT -4. The time now is 12:04 PM.


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