How to Read XML from HTTP Post in ASP.NET

Posted on 2011-05-13
Last Modified: 2012-05-11
Hey Experts,

A third party developer is going to post an xml document via HTTP to my url / asp application. I am not sure how to retrieve it and read the xml data when it is posted.

Is there a quick and easy way to do this?
Question by:rmartes
    LVL 22

    Accepted Solution


    Author Comment

    Thanks plusone, I used the code project example, but now im getting a "badrequest" @ this line: rsp = req.GetResponse()

    Here is the code I'm using:

    Private Sub Page_Load()
    		Dim req As WebRequest = Nothing
    		Dim rsp As WebResponse = Nothing
    			Dim fileName As String = Server.MapPath("xml\webform.xml")
    			Dim uri As String = "http://localhost:500/xml_receiver.aspx"
    			req = WebRequest.Create(uri)
    			'req.Proxy = WebProxy.GetDefaultProxy(); // Enable if using proxy
    			req.Method = "POST"
    			' Post method
    			req.ContentType = "text/xml"
    			' content type
    			' Wrap the request stream with a text-based writer
    			Dim writer As New StreamWriter(req.GetRequestStream())
    			' Write the XML text into the stream
    			' Send the data to the webserver
    			rsp = req.GetResponse()
    		'Catch webEx As WebException
    		'Catch ex As Exception
    			'If req IsNot Nothing Then
    			'End If
    			'If rsp IsNot Nothing Then
    			'End If
    		'End Try
    	End Sub
    	Private Function GetTextFromXMLFile(file As String) As String
    		Dim reader As New StreamReader(file)
    		Dim ret As String = reader.ReadToEnd()
    		Return ret
    	End Function

    Open in new window

    Any clue why?

    Author Comment

    Never mind the above. I got the post to work. I am having trouble reading the xml now. The page i setup to read the xml is stalling.

    I used this code

    Page.Response.ContentType = "text/xml"
    			'Read XML posted via HTTP
    			Dim reader As StreamReader = new StreamReader(Page.Request.InputStream)
    		 	Dim xmlData As String = reader.ReadToEnd()

    Open in new window


    Author Comment

    GOT IT!!!! Thanks

    Author Closing Comment


    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
    A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    729 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

    18 Experts available now in Live!

    Get 1:1 Help Now