Posting XML using .NET

Well, I’m into the new xml project somewhat now and have created the xml output file and the code to post it using .NET. If you are unfamiliar with the procedure, here is a sample.

‘first we create a request
Dim req As WebRequest = HttpWebRequest.Create(

‘set the request method to POST
req.Method = “POST”

‘now encode the xmlout as byte and write to
‘the request stream for posting
Dim xmlout As String = “this is the xml text that you want to POST”
Dim encoding As New System.Text.ASCIIEncoding
Dim byte1 As Byte() = encoding.GetBytes(xmlout)

‘set the content length of the string being posted
‘create the stream amd write it and close
req.ContentLength = xmlout.Length
req.ContentType = “text/xml”

Dim postdata As Stream = req.GetRequestStream()
postdata.Write(byte1, 0, byte1.Length)

‘now get the response
Dim res As WebResponse = req.GetResponse
‘read it into a stream and close
Dim sr As New System.IO.StreamReader(res.GetResponseStream)
PostXMLOut = sr.ReadToEnd

I’ve run this against the development system and we are getting a timeout error, which the vendor is looking at. Should be pretty straightforward though…It get’s a bit more involved if you want to POST the data asynchronously, or read the response asynchronously. But we don’t, so HA!

Hope this makes sense…


, , , , , , , , , , , , ,

No comments yet.

Leave a Reply