【问题标题】:VB.NET Can't send POST request properlyVB.NET 无法正确发送 POST 请求
【发布时间】:2019-06-26 18:46:08
【问题描述】:

所以我使用 Newtonsoft.Json 发送 POST 请求。

我必须使用字典来指定我的键和值。

这是 POST 在接收端的样子:

{"type":"direct","packages":["http://whatever:9999/something.pkg"]}

我的代码:

    dictData.Add("type", "direct")
    dictData.Add("packages", "[http://whatever:9999/something.pkg]")
    jsonPost.postData(dictData)

还有输出:

 {
  "type": "direct",
  "packages": "[http://whatever:9999/something.pkg]"
   }

所以问题是:我不希望有空格,我希望所有内容都在一行中,并且我希望 URL 用引号引起来。我尝试了双“”方法,得到了我的引号,但它还在 URL 的开头和结尾放置了一个 \。

我确信这很简单,但在我的一生中,我找不到任何有用的东西。

【问题讨论】:

  • 尝试将包值作为数组或字符串列表而不是标量字符串传递,例如dictData.Add("packages", {"http://whatever:9999/something.pkg"})。括号的位置表明它应该被视为一个列表(只有一个成员)而不是一个标量。

标签: json string vb.net dictionary post


【解决方案1】:

我会简单地创建一个 Jobject(第一个 {} )并向其中添加内容。喜欢:

Dim JSON_Post As New JObject
JSON_Post.Add("type", "direct")
Dim JSON_Array As New JArray
JSON_Array.Add("http://whatever:9999/something.pkg")
'Or JSON_Post.Add("packages", New JArray From {"http://whatever:9999/something.pkg"})
JSON_Post.Add("packages", JSON_Array)
Debug.Print(JSON_Post.ToString)

'Output
{
  "type": "direct",
  "packages": [
    "http://whatever:9999/something.pkg"
  ]
}

你可以对字典和列表做同样的事情,字典是键值部分,所以它总是看起来像

"type1": "direct1",
"type2", "direct2"...

因此,您需要(一次)添加包含更多项目的内容,例如获取 [] 的列表。

这些空格并不重要,它会被很好地读取,它只是显示的格式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-03
    • 2015-12-22
    • 2017-10-30
    • 1970-01-01
    • 2018-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多