How to Read XML from HTTP Post in ASP.NET

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?
rmartesAsked:
Who is Participating?
 
rmartesAuthor Commented:
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
		
		'Try
			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
			writer.WriteLine(Me.GetTextFromXMLFile(fileName))
			writer.Close()
			' Send the data to the webserver
		
			rsp = req.GetResponse()
		
		'Catch webEx As WebException
		
		'Catch ex As Exception
		
		'Finally
			'If req IsNot Nothing Then
				'req.GetRequestStream().Close()
			'End If
			'If rsp IsNot Nothing Then
				'rsp.GetResponseStream().Close()
			'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()
		reader.Close()
		Return ret
	End Function

Open in new window


Any clue why?
0
 
rmartesAuthor Commented:
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

0
 
rmartesAuthor Commented:
GOT IT!!!! Thanks
0
 
rmartesAuthor Commented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.