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

后端 未结 2 1287
星月不相逢
星月不相逢 2020-12-01 12:13

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

相关标签:
2条回答
  • 2020-12-01 12:53

    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/

    0 讨论(0)
  • 2020-12-01 12:56

    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 讨论(0)
提交回复
热议问题