Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
JSON HTTP POST Request In Visual Basic .NET
'Install Newtonsoft.json
'PM> Install-Package Newtonsoft.Json -Version 6.0.8
'Sample Usage
'Dim jsonPost As New JsonPost("")
'Dim dictData As New Dictionary(Of String, Object)
'dictData.Add("test_key", "test_value")
Imports Newtonsoft.Json
Imports System.Net
Imports System.Text
Public Class JsonPost
Private urlToPost As String = ""
Public Sub New(ByVal urlToPost As String)
Me.urlToPost = urlToPost
End Sub
Public Function postData(ByVal dictData As Dictionary(Of String, Object)) As Boolean
Dim webClient As New WebClient()
Dim resByte As Byte()
Dim resString As String
Dim reqString() As Byte
webClient.Headers("content-type") = "application/json"
reqString = Encoding.Default.GetBytes(JsonConvert.SerializeObject(dictData, Formatting.Indented))
resByte = webClient.UploadData(Me.urlToPost, "post", reqString)
resString = Encoding.Default.GetString(resByte)
Return True
Catch ex As Exception
End Try
Return False
End Function
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment