簡體   English   中英

將Python腳本轉換為Vb.NET-涉及發布和輸入XML字符串

[英]Converting Python Script to Vb.NET - Involves Post and Input XML String

我正在嘗試將Python腳本轉換為Vb.Net。 Python腳本似乎接受一些XML輸入數據,然后將其帶到Web URL並執行“ POST”。 我嘗試了一些VB.NET代碼來執行此操作,但是我認為我的方法已關閉,因為我收到了一個錯誤消息“ BadXmlDataErr”,而且我無法很好地格式化我的輸入XML-我只在做字符串和值。 輸入的XML比那還要豐富。

這是一個XML輸入數據在Python腳本中的示例:

<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

我覺得我應該做些其他的事情。 誰能指出我正確的方向?

我想到了:

公共函數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()

結束功能

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM