Jump to content

VB - Help sending data to URI

BeefyMeats
Go to solution Solved by BeefyMeats,

I found the old post that was used before and its working now.

 

Here it is: https://stackoverflow.com/questions/4015324/how-to-make-http-post-web-request

 

Useless info: 

I made (copy/pasta alot of code until it worked) a site some time ago to toggle things at home, and an app to toggle them without a web browser but dont have the source anymore. My old app still works, but I need to add one more device to toggle. (no source for it anymore)

 

Useful info:

I need to send this data to toggle the device, and have code that seems like it would work, but im not sure how to store it in a variable that would work with the function

Need to send this:

{ "outletId", "1" },
{ "outletStatus", "on" }

 

Heres the code I have so far:

Thanks!

Imports System.IO
Imports System.Net
Imports System.Net.Http
Imports Newtonsoft.Json.JsonConvert
Imports Newtonsoft.Json.Linq
Imports Newtonsoft.Json.JsonSerializer
Imports Newtonsoft.Json
Imports System.Net.Http.Formatting

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim Uri As String = "http://thing/toggle.php"

        '##############################
        'somehow use data here :)
        '###############################
  
        SendRequest(Uri, Data, "text/json", "POST") 'application/json
    End Sub


    Private Function SendRequest(uri As Uri, jsonDataBytes As Byte(), contentType As String, method As String) As String
        Dim req As WebRequest = WebRequest.Create(uri)
        req.ContentType = contentType
        req.Method = method
        req.ContentLength = jsonDataBytes.Length

        Dim stream = req.GetRequestStream()
        stream.Write(jsonDataBytes, 0, jsonDataBytes.Length)
        stream.Close()

        Dim response = req.GetResponse().GetResponseStream()

        Dim reader As New StreamReader(response)
        Dim res = reader.ReadToEnd()
        reader.Close()
        response.Close()

        Return res
    End Function

End Class

 

---- edit ----

Here is what was suggested to use. Any idea how to use it properly with the data I have?

Dim data = Encoding.UTF8.GetBytes(jsonSring)
Dim result_post = SendRequest(uri, data, "application/json", "POST")

 

Link to comment
Share on other sites

Link to post
Share on other sites

Why use webrequests when you can use PostAsJsonAsync() with HttpClient extensions?

Link to comment
Share on other sites

Link to post
Share on other sites

Because I dont know what Im doing, and just keep hammering in code from Google until my projects work.

 

------edit --------

this is what I used, not sure what the difference is, but what was used in button1 seems to be "snappier/quicker"

I attempted to use what you suggested for button2, and it also works. Thanks

Imports System.IO
Imports System.Net
Imports System.Net.Http
Imports System.Net.Http.Formatting
Imports System.Text

Public Class Form1

    Private Shared ReadOnly httpClient As HttpClient = New HttpClient()

    Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If True Then
            Dim url As String = "http://thing/toggle.php"
            Dim values = New Dictionary(Of String, String) From {
                {"outletId", "2"},
                {"outletStatus", "off"}
            }

            Dim content = New FormUrlEncodedContent(values)
            Dim response = Await httpClient.PostAsync(url, content)
            Dim responseString = Await response.Content.ReadAsStringAsync()
        End If
    End Sub



    Private Async Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim url As String = "http://thing/toggle.php"
        Dim Data = New Dictionary(Of String, String) From {
                {"outletId", "2"},
                {"outletStatus", "on"}
            }

        Dim httpContent = New FormUrlEncodedContent(Data)
        Dim asdf = Await httpClient.PostAsJsonAsync(url, httpContent)
        Dim response = httpClient.PostAsync(url, httpContent).Result

    End Sub
End Class

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×