如何使用 VB.NET 将 JSON 发布到特定的 url? [英] How to POST a JSON to a specific url using VB.NET?

查看:64
本文介绍了如何使用 VB.NET 将 JSON 发布到特定的 url?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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...

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

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

到这个网址...

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

https://addressgoeshere (we are using https)

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

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

推荐答案

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

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

记得导入

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

通过使用

Imports

然后是导入名称

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

to bypass expired ssl certificate check this: 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

使用此功能

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

--编辑--

链接的页面现在已经过期.这是一个有效的存档副本:

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/

这篇关于如何使用 VB.NET 将 JSON 发布到特定的 url?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆