-2

我有一个python代码。我需要它的 vb.net 等价物。

提前致谢。

payload2 = {"auth_token": "YOUR_AUTH_TOKEN", "status":"green", "data-title1":"index", "data-title2":"machine1", "data-title3":"package10", "data-title4":"current", "data-title5":"tmp"}
r=requests.post("http://localhost:3030/widgets/hot21",data=json.dumps(payload2))

额外信息 代码在“UiPath社区版”中被调用

最后的代码(感谢 JussiV)是

Imports System.Text
Dim payload2 As New Dictionary(Of String, String)
payload2.Add("auth_token", "YOUR_AUTH_TOKEN")
payload2.Add("status", "green")
payload2.Add("data-title1", "index")
payload2.Add("data-title2", "machine1")
payload2.Add("data-title3", "package10")
payload2.Add("data-title4", "current")
payload2.Add("data-title5", "tmp")
Dim params As String = JsonConvert.SerializeObject(payload2, Formatting.None)
Dim Uri As New Uri(String.Format("http://10.10.115.99:3030/widgets/hot21"))
Dim webClient As New WebClient()
Dim resByte As Byte()
Dim resString As String
Dim reqString() As Byte
webClient.Headers("content-type") = "application/json"
Dim senddata As Object = JsonConvert.SerializeObject(New With {Key .param = params}).ToString()
reqString = Encoding.Default.GetBytes(senddata)
resByte = webClient.UploadData(Uri, "post", reqString)
resString = Encoding.Default.GetString(resByte)

最后的错误消息是

webservice has thrown an exception
Source: Invoke code
Message: Error compiling code
error BC30035: syntax error. At line 1
error BC30561: 'Formatting' is ambiguous, imported from the namespaces or types 'Newtonsoft.Json, System.Xml'. At line 10
error BC30002: Type 'WebClient' is not defined. At line 12
error BC30451: 'Encoding' is not declared. It may be inaccessible due to its protection level. At line 18
error BC30451: 'Encoding' is not declared. It may be inaccessible due to its protection level. At line 20
Exception Type: ArgumentException
4

1 回答 1

1

在 VB.net https://www.dotnetperls.com/dictionary-vbnet中创建字典:

Dim payload2 As New Dictionary(Of String, String>
payload2.add("auth_token", "<token>")
....

然后将字典发布为 JSON

Dim params As String = JsonConvert.SerializeObject(payload2, Formatting.None)
Dim Uri As New Uri(String.Format("http://localhost:3030/widgets/hot"+indeks))
Dim webClient As New WebClient()
Dim resByte As Byte()
Dim resString As String
Dim reqString() As Byte
webClient.Headers("content-type") = "application/json"
Dim senddata As Object = JsonConvert.SerializeObject(New With {Key .param = params}).ToString()
reqString = Encoding.Default.GetBytes(senddata)
resByte = webClient.UploadData(Uri, "post", reqString)
resString = Encoding.Default.GetString(resByte)

编辑:

您至少缺少System.Net具有WebClient该类的导入。我不确定你在哪里/如何导入它,Newtonsoft.Json因为我在你的导入中没有看到它,而是基于它在某处导入的错误。

对于模棱两可的导入,请参阅此答案以解决模棱两可的导入。

于 2018-04-06T09:07:52.723 回答