1

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
4

2 に答える 2

1

これは本質的に私が求めていたものです:

xml.Save(req.GetRequestStream())
于 2011-02-19T06:39:51.783 に答える
0

長さを気にしたくない場合は、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
于 2011-02-19T05:07:34.880 に答える