24

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?

InteXX
  • 6,135
  • 6
  • 43
  • 80
max
  • 241
  • 1
  • 2
  • 4

2 Answers2

40

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/

InteXX
  • 6,135
  • 6
  • 43
  • 80
Pa.M
  • 1,392
  • 1
  • 10
  • 15
  • Hi! Thanks! I finally got it working! But I have one little problem. When I tried using https it gave a **"An unhandled exception of type 'System.Net.WebException' occured in System.dll Additional information: The underlying connection was closed: An unexpected error occured on a send."** How can I make it work using https?Thanks! – max Sep 12 '11 at 08:26
  • That's kinda weird. I keep on getting that error. When I access the url in google chrome or any other browser it asks for a certificate which I just click "proceed anyway". Do you think it has something to do with it? – max Sep 12 '11 at 08:55
  • yes certificates are the culprit.. there is a way to bypass that via code, updated my answer. – Pa.M Sep 12 '11 at 09:45
  • Use `HttpWebRequest` for HTTP specific capabilities like Accept headers. – user2609980 Apr 02 '15 at 17:19
1

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

StefanM
  • 797
  • 1
  • 10
  • 17
ChrisBaris
  • 11
  • 1