How to post XML document to HTTP with VB.Net

Posted by Joshua McGinnis on Stack Overflow See other posts from Stack Overflow or by Joshua McGinnis
Published on 2011-02-19T04:01:03Z Indexed on 2011/02/19 7:25 UTC
Read the original article Hit count: 196

I'm looking for help with posting my XML document to a url in VB.NET. Here's what I have so far ...

  Public Shared xml As New System.Xml.XmlDocument()

    Public Shared Sub Main()

        Dim root As XmlElement
        root = xml.CreateElement("root")
        xml.AppendChild(root)

        Dim username As XmlElement
        username = xml.CreateElement("username")
        username.InnerText = _username
        root.AppendChild(username)

        xml.Save(Console.Out)

        Dim url = "https://mydomain.com"
        Dim req As WebRequest = WebRequest.Create(url)
        req.Method = "POST"
        req.ContentType = "application/xml"
        req.Headers.Add("Custom: API_Method")

        Console.WriteLine(req.Headers.ToString())

This is where things go awry:

I want to post the xml, and then print the results to console.

        Dim newStream As Stream = req.GetRequestStream()
        xml.Save(newStream)

        Dim response As WebResponse = req.GetResponse()
        Console.WriteLine(response.ToString())
 End Sub

© Stack Overflow or respective owner

Related posts about vb.net

Related posts about webrequest