[英]Converting Python Script to Vb.NET - Involves Post and Input XML String
I'm trying to convert a Python Script to Vb.Net. 我正在尝试将Python脚本转换为Vb.Net。 The Python Script appears to accept some XML input data and then takes it to a web URL and does a "POST".
Python脚本似乎接受一些XML输入数据,然后将其带到Web URL并执行“ POST”。 I tried some VB.NET code to do it, but I think my approach is off because I got an error back "BadXmlDataErr" plus I can't really format my input XML very well - I'm only doing string and value.
我尝试了一些VB.NET代码来执行此操作,但是我认为我的方法已关闭,因为我收到了一个错误消息“ BadXmlDataErr”,而且我无法很好地格式化我的输入XML-我只在做字符串和值。 The input XML is richer than that.
输入的XML比那还要丰富。
Here is an example of what the XML input data looks like in the Python script: 这是一个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>
Here's the Vb.net code I attempted to convert that: 这是我尝试转换的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
I feel like I should be doing something else. 我觉得我应该做些其他的事情。 Can anyone point me to the right direction?
谁能指出我正确的方向?
I figured it out: 我想到了:
Public Function MyWrite(ByVal sMyUrl As String) As Boolean 公共函数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()
End Function 结束功能
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.