开发者

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

开发者 https://www.devze.com 2023-04-04 17:44 出处:网络
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 firs

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

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

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

to this url...

https://addressgoeshere (we are using https)

Can someone provide me a sample code to do this so I can have a gui开发者_C百科de and a good start?


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

remember to import

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

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/

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/


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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消