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

max*_*max 21 vb.net post json web-services

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

"要登录JIRA,您需要以JSON格式POST一个用户名和密码......"

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

这个网址......

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

有人可以给我一个示例代码来做这个,所以我可以有一个指南和一个良好的开端?请非常感谢!

Par*_*esh 36

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

记得导入

  • System.Net
  • System.IO
  • System.text

通过使用

Imports
Run Code Online (Sandbox Code Playgroud)

然后是导入名称

绕过过期的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
Run Code Online (Sandbox Code Playgroud)

使用此功能

Dim data = Encoding.UTF8.GetBytes(jsonSring)
Dim result_post = SendRequest(uri, data, "application/json", "POST")
Run Code Online (Sandbox Code Playgroud)