Created
April 1, 2015 07:19
-
-
Save six519/5ed917850f402b94ee6b to your computer and use it in GitHub Desktop.
JSON HTTP POST Request In Visual Basic .NET
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'Install Newtonsoft.json | |
'----------------------- | |
' | |
'PM> Install-Package Newtonsoft.Json -Version 6.0.8 | |
'Sample Usage | |
'------------ | |
'Dim jsonPost As New JsonPost("http://192.168.254.104:8000") | |
'Dim dictData As New Dictionary(Of String, Object) | |
'dictData.Add("test_key", "test_value") | |
'jsonPost.postData(dictData) | |
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 | |
Try | |
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) | |
Console.WriteLine(resString) | |
webClient.Dispose() | |
Return True | |
Catch ex As Exception | |
Console.WriteLine(ex.Message) | |
End Try | |
Return False | |
End Function | |
End Class |
Fantastic. Saved a lot of my time and efforts. Thank you so much
Sir
I want to user CRUD operation using HTTP POST method. Please guide
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i encountered a problem to using button to post the json to the website. this curl post can manage to post data to the website.
curl -X POST --header 'Content-Type: application/json'
--header 'Accept: application/json'
--header 'apiKey: YOUR API KEY HERE'
-d '{
"device_developer_id": "deviceDefault@FAVORIOT",
"data": { "temperature": "31","humidity": "70"}
}'
'https://apiv2.favoriot.com/v2/streams'
However in vb.net is :
'Install Newtonsoft.json
'-----------------------
'
'PM> Install-Package Newtonsoft.Json -Version 6.0.8
'Sample Usage
'------------
'Dim jsonPost As New JsonPost("http://192.168.254.104:8000")
'Dim dictData As New Dictionary(Of String, Object)
'dictData.Add("test_key", "test_value")
'jsonPost.postData(dictData)
' favoriot part
' ============
' link : https://apiv2.favoriot.com/v2/streams
Imports Newtonsoft.Json
Imports System.Net
Imports System.Text
Public Class Form1
Private urlToPost As String = ""
Private apikey_favoriot As String = "YOUR API KEY HERE"
End Class