VB.NET无法正确发送POST请求 [英] VB.NET Can't send POST request properly
问题描述
所以我正在使用Newtonsoft.Json发送POST请求.
So I'm using Newtonsoft.Json to send POST requests.
我必须使用字典来指定我的键和值.
I have to use the Dictionary to specify my key and value.
这是POST在接收端的外观:
Here is how the POST should look like on the receiving end:
{"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的开头和结尾都放置了一个\.
So the issues: I want no spaces in there, I want it all in one line, and I want the URL in quotation marks. I tried the double "" method, and I got my quotation marks, but it also placed a \ to both the beginning and end of the URL.
我确定这很简单,但是对于我一生来说,我找不到任何有效的方法.
I'm sure it is something simple, but for the life of me I could not find anything that worked.
推荐答案
我只需创建一个Jobject(第一个{})并向其中添加内容.喜欢:
I would simply create a Jobject (first {} ) and add stuff to it. Like:
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"
]
}
您可以对字典和列表进行同样的操作,字典是键值部分,因此它总是看起来像
You could do the same with dictionary and list, dictionary is key-value part so it would always look like
"type1": "direct1",
"type2", "direct2"...
因此,您需要(一次)添加包含更多项的内容,例如获取[]的列表.
So you need to add something with more items (at once), like a list to get the [].
那些空格并不重要,可以很好地阅读,这只是显示格式的方式.
And those spaces are not important, it will be read just fine, it's just how display is formatted.
这篇关于VB.NET无法正确发送POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!