【问题标题】:Converting Python Script to Vb.NET - Involves Post and Input XML String将 Python 脚本转换为 Vb.NET - 涉及发布和输入 XML 字符串
【发布时间】:2014-08-14 15:21:26
【问题描述】:

我正在尝试将 Python 脚本转换为 Vb.Net。 Python 脚本似乎接受一些 XML 输入数据,然后将其带到 Web URL 并执行“POST”。我尝试了一些 VB.NET 代码来执行此操作,但我认为我的方法已关闭,因为我收到了一个错误返回“BadXmlDataErr”,而且我无法真正很好地格式化我的输入 XML - 我只是在做字符串和值。输入的 XML 比这更丰富。

以下是 Python 脚本中 XML 输入数据的示例:

<obj is="MyOrg:realCommand_v1/" >
<int name="priority" val="1" />
<real name="value" val="9.5" />
<str name="user" val="MyUserName" />
<reltime name="overrideTime" val="PT60S"/>
</obj>

这是我尝试转换的 Vb.net 代码:

    Dim reqparm As New Specialized.NameValueCollection
    reqparm.Add("priority", "1")
    reqparm.Add("value", "9.5")
    reqparm.Add("user", "MyUserName")
    reqparm.Add("overrideTime", "PT60S")
    Using client As New Net.WebClient
        Dim sTheUrl As String = "[My URL]"
        Dim responsebytes = client.UploadValues(sTheUrl, "POST", MyReqparm)
        Dim responsebody = (New System.Text.UTF8Encoding).GetString(responsebytes)
    End Using

我觉得我应该做点别的事情。谁能指出我正确的方向?

【问题讨论】:

    标签: python asp.net xml vb.net


    【解决方案1】:

    我想通了:

    公共函数 MyWrite(ByVal sMyUrl As String) As Boolean

    ' Create a request using a URL that can receive a post. 
    Dim request As WebRequest = WebRequest.Create(sTheUrl)
    ' Set the Method property of the request to POST.
    request.Method = "POST"
    ' Create POST data and convert it to a byte array.
    Dim postData As String = "[This is where you insert your XML String]"
    Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
    ' Set the ContentType property of the WebRequest.
    request.ContentType = "application/x-www-form-urlencoded"
    ' Set the ContentLength property of the WebRequest.
    request.ContentLength = byteArray.Length
    ' Get the request stream.
    Dim dataStream As Stream = request.GetRequestStream()
    ' Write the data to the request stream.
    dataStream.Write(byteArray, 0, byteArray.Length)
    ' Close the Stream object.
    dataStream.Close()
    ' Get the response.
    Dim response As WebResponse = request.GetResponse()
    ' Display the status.
    Console.WriteLine(CType(response, HttpWebResponse).StatusDescription)
    ' Get the stream containing content returned by the server.
    dataStream = response.GetResponseStream()
    ' Open the stream using a StreamReader for easy access.
    Dim reader As New StreamReader(dataStream)
    ' Read the content.
    Dim responseFromServer As String = reader.ReadToEnd()
    ' Display the content.
    Console.WriteLine(responseFromServer)
    ' Clean up the streams.
    reader.Close()
    dataStream.Close()
    response.Close()
    

    结束函数

    【讨论】:

      猜你喜欢
      • 2021-05-16
      • 2021-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多