我是VB.NET中Web服务的新手。我正在制作一个与JIRA(http://www.atlassian.com/software/jira/)交谈的桌面应用程序。他们提供了我决定使用的REST api。第一步是登录,他们说...
“要登录JIRA,您需要以JSON格式发布用户名和密码......”
{“username”:“admin”,“password”:“admin”}
到此网址
https:// addressgoeshere (我们使用的是https)
有人可以为我提供示例代码,这样我可以有一个指南和良好的开端吗?非常感谢你!
答案 0 :(得分:34)
这里是有效发布json的代码。变量res
能够为您提供对查询的响应
记得导入
使用
Imports
然后是导入名称
绕过过期的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 req As WebRequest = WebRequest.Create(uri)
req.ContentType = contentType
req.Method = method
req.ContentLength = jsonDataBytes.Length
Dim stream = req.GetRequestStream()
stream.Write(jsonDataBytes, 0, jsonDataBytes.Length)
stream.Close()
Dim response = req.GetResponse().GetResponseStream()
Dim reader As New StreamReader(response)
Dim res = reader.ReadToEnd()
reader.Close()
response.Close()
Return res
End Function
使用此功能
Dim data = Encoding.UTF8.GetBytes(jsonSring)
Dim result_post = SendRequest(uri, data, "application/json", "POST")
答案 1 :(得分:1)
对于'底层连接已关闭:'错误包含...WebRequest.Create(Url)
行之后的这两行代码 - 应该正常工作
System.Net.ServicePointManager.UseNagleAlgorithm = False
System.Net.ServicePointManager.Expect100Continue = False