JSON HTTP POST Request In Visual Basic .NET · GitHub

Skip to content Search Gists Search Gists All gists Back to GitHub Sign in Sign up Sign in Sign up Dismiss alert {{ message }}

Instantly share code, notes, and snippets.

@six519 six519/JsonPost.vb Created April 1, 2015 07:19 Show Gist options
  • Star (21) You must be signed in to star a gist
  • Fork (9) You must be signed in to fork a gist
  • Embed Select an option
    • Embed Embed this gist in your website.
    • Share Copy sharable link for this gist.
    • Clone via HTTPS Clone using the web URL.

    No results found

    Learn more about clone URLs Clone this repository at <script src="https://gist.github.com/six519/5ed917850f402b94ee6b.js"></script>
  • Save six519/5ed917850f402b94ee6b to your computer and use it in GitHub Desktop.
Code Revisions 1 Stars 21 Forks 9 Embed Select an option
  • Embed Embed this gist in your website.
  • Share Copy sharable link for this gist.
  • Clone via HTTPS Clone using the web URL.

No results found

Learn more about clone URLs Clone this repository at <script src="https://gist.github.com/six519/5ed917850f402b94ee6b.js"></script> Save six519/5ed917850f402b94ee6b to your computer and use it in GitHub Desktop. Download ZIP JSON HTTP POST Request In Visual Basic .NET Raw JsonPost.vb This file contains hidden or 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 Show hidden 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
@Willy-Kimura Copy link

Willy-Kimura commented May 29, 2017

Thanks so much! Keep up the good work.

Uh oh!

There was an error while loading. Please reload this page.

@jezile Copy link

jezile commented Aug 10, 2017

Awesome! Thanks

Uh oh!

There was an error while loading. Please reload this page.

@fersrc Copy link

fersrc commented Mar 1, 2018

This example return the body of the response, what can i do if i also want the head of the response?

Uh oh!

There was an error while loading. Please reload this page.

@gabrimen Copy link

gabrimen commented Mar 7, 2018

Thank you so much!!!!!!!! Finally, all samples i found were missing something for the API i am trying, this worked beautiful

Uh oh!

There was an error while loading. Please reload this page.

@markazali Copy link

markazali commented Mar 28, 2018

I really appreciate this. Thanks

Uh oh!

There was an error while loading. Please reload this page.

@Kenshin84 Copy link

Kenshin84 commented Nov 13, 2018

Thank you very much! i was updating my script from python to vb and this help me very well.

Uh oh!

There was an error while loading. Please reload this page.

@studentprojects-live Copy link

studentprojects-live commented Feb 27, 2019

Good work dude. THanks a lot

Uh oh!

There was an error while loading. Please reload this page.

@DragonflyNet67 Copy link

DragonflyNet67 commented Mar 4, 2019

Great work, thanks a lot. Solved a problem for me immediately where I spent hours trying to do a post request with json post data and additional headers.

Uh oh!

There was an error while loading. Please reload this page.

@alazimariff Copy link

alazimariff commented Oct 9, 2019

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"

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" webClient.Headers.Add("apikey", apikey_favoriot) 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 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim Form1 As New Form1("http://192.168.254.104:8000") Dim dictData As New Dictionary(Of String, Object) dictData.Add("device_developer_id", "deviceDefault@FAVORIOT") dictData.Add("data", "temperature : 31") Form1.postData(dictData) End Sub

End Class

Uh oh!

There was an error while loading. Please reload this page.

@pratikjain1980 Copy link

pratikjain1980 commented Oct 6, 2022

Fantastic. Saved a lot of my time and efforts. Thank you so much

Uh oh!

There was an error while loading. Please reload this page.

@vshah2703 Copy link

vshah2703 commented May 31, 2023

Sir I want to user CRUD operation using HTTP POST method. Please guide

Uh oh!

There was an error while loading. Please reload this page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment You can’t perform that action at this time.

Từ khóa » Visual Basic Http Post Request