Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 guide and a good start?

like image 505
max Avatar asked Sep 12 '11 07:09

max


2 Answers

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/

like image 199
Pa.M Avatar answered Nov 16 '22 19:11

Pa.M


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

like image 34
ChrisBaris Avatar answered Nov 16 '22 19:11

ChrisBaris