如何使用 VB.Net 将 XML 文档发布到 HTTP

发布于 2024-10-17 21:45:37 字数 999 浏览 6 评论 0原文

我正在寻求有关将 XML 文档发布到 VB.NET 中的 URL 的帮助。这是我到目前为止所做的...

  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())

这就是事情出错的地方:

我想发布 xml,然后将结果打印到控制台。

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

秋叶绚丽 2024-10-24 21:45:37

这本质上就是我所追求的:

xml.Save(req.GetRequestStream())

This is essentially what I was after:

xml.Save(req.GetRequestStream())
花桑 2024-10-24 21:45:37

如果您不想关心长度,也可以使用 WebClient.UploadData 方法。

我以这种方式稍微修改了你的片段。

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文