如何使用 VB.NET 将 JSON 发布到特定 url?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7384534/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:59:05  来源:igfitidea点击:

How to POST a JSON to a specific url using VB.NET?

vb.netweb-servicesjsonpost

提问by max

I'm a newbie about web services in VB.NET. I'm making a desktop application that will talk to JIRA (http://www.atlassian.com/software/jira/). They provided a REST api that I decided to use. The first step is to login which they say that...

我是 VB.NET 中 Web 服务的新手。我正在制作一个可以与 JIRA ( http://www.atlassian.com/software/jira/)对话的桌面应用程序。他们提供了我决定使用的 REST api。第一步是登录,他们说...

"To log in to JIRA, you need to POST a username and password in JSON format..."

“要登录JIRA,你需要以JSON格式POST一个用户名和密码……”

{"username" : "admin", "password" : "admin"}

{“用户名”:“管理员”,“密码”:“管理员”}

to this url...

到这个网址...

https://addressgoeshere(we are using https)

https://addressgoeshere(我们使用的是 https)

Can someone provide me a sample code to do this so I can have a guide and a good start?

有人可以为我提供一个示例代码来执行此操作,以便我可以有一个指南和一个良好的开端吗?

回答by Parvesh

Here is the code to post json effectively. The variable resis able to give you the responce to your query

这是有效发布json的代码。该变量res能够为您提供对查询的响应

remember to import

记得导入

  • System.Net
  • System.IO
  • System.text
  • 系统网
  • 系统IO
  • 系统文本

by using

通过使用

Imports

and then the import names

然后是导入名称

to bypass expired ssl certificate check this: http://blog.jameshiggs.com/2008/05/01/c-how-to-accept-an-invalid-ssl-certificate-programmatically/

要绕过过期的 ssl 证书,请检查:http: //blog.jameshiggs.com/2008/05/01/c-how-to-accept-an-invalid-ssl-certificate-programmatically/

Private Function SendRequest(uri As Uri, jsonDataBytes As Byte(), contentType As String, method As String) As String
  Dim response As String
  Dim request As WebRequest

  request = WebRequest.Create(uri)
  request.ContentLength = jsonDataBytes.Length
  request.ContentType = contentType
  request.Method = method

  Using requestStream = request.GetRequestStream
    requestStream.Write(jsonDataBytes, 0, jsonDataBytes.Length)
    requestStream.Close()

    Using responseStream = request.GetResponse.GetResponseStream
      Using reader As New StreamReader(responseStream)
        response = reader.ReadToEnd()
      End Using
    End Using
  End Using

  Return response
End Function

to use this function

使用这个功能

Dim data = Encoding.UTF8.GetBytes(jsonSring)
Dim result_post = SendRequest(uri, data, "application/json", "POST")

--EDIT--

- 编辑 -

The linked page has expired by now. Here is a working archived copy:

链接的页面现在已经过期。这是一个工作存档副本:

https://web.archive.org/web/20110924191356/http://blog.jameshiggs.com/2008/05/01/c-how-to-accept-an-invalid-ssl-certificate-programmatically/

https://web.archive.org/web/20110924191356/http://blog.jameshiggs.com/2008/05/01/c-how-to-accept-an-invalid-ssl-certificate-programmatically/

回答by ChrisBaris

For 'The underlying connection was closed:' error include these 2 lines of code after the line ...WebRequest.Create(Url)-it should work

对于“底层连接已关闭:”错误在该行之后包含这两行代码...WebRequest.Create(Url)- 它应该可以工作

System.Net.ServicePointManager.UseNagleAlgorithm = FalseSystem.Net.ServicePointManager.Expect100Continue = False

System.Net.ServicePointManager.UseNagleAlgorithm = FalseSystem.Net.ServicePointManager.Expect100Continue = False