How to post XML document to HTTP with VB.Net

徘徊边缘 提交于 2019-12-07 02:42:33

This is essentially what I was after:

xml.Save(req.GetRequestStream())

If you don't want to take care about the length, it is also possible to use the WebClient.UploadData method.

I adapted your snippet slightly in this way.

Imports System.Xml
Imports System.Net
Imports System.IO

Public Module Module1

    Public xml As New System.Xml.XmlDocument()

    Public Sub Main()

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

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

        Dim url = "http://mydomain.com"
        Dim client As New WebClient

        client.Headers.Add("Content-Type", "application/xml")
        client.Headers.Add("Custom: API_Method")
        Dim sentXml As Byte() = System.Text.Encoding.ASCII.GetBytes(xml.OuterXml)
        Dim response As Byte() = client.UploadData(url, "POST", sentXml)

        Console.WriteLine(response.ToString())

    End Sub

End Module
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!