Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
It might be quiet similar (so I thought) but I didn't get a way to go.
So I try to get help here - I suppose that it is quiet easy ...
I want to send a HTTP-Command to a Shelly-Device via VB.Net - I work with FORMS and Framework 4.0

The complete command is "http://192.168.xxx.xxx/relay/0?turn=off" for example.
So ... how could I easyly realize that ?

What I have tried:

tried to find a working sample with google but each is either not working or obsolete or after some uses the application freezes.
Posted

I know that a solution has been posted above but thought that my 5 cents worth might help future browsers on the question asked.

I also know that 'HttpClient' is generally considered the newer and more modern option for making HTTP requests in .NET but I have found that 'WebClient' has been around for a longer time and is part of the System.Net namespace. It is simpler and easier to use for basic scenarios but lacks some of the advanced features and flexibility offered by 'HttpClient'. Based on the complexity of your usage, this might be the easier way out if you don't need the more advanced features so if you're looking for a simpler alternative, you might consider using the 'WebClient' class, which is more straightforward for basic scenarios (Tongue in cheek here as many might disagree with me).

To use 'WebClient, you can try the following -

VB.NET
Imports System.Net

Public Class Form1

    Dim webClient As New WebClient()
    Dim baseUrl As String = "http://192.168.178.201/relay/"

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        SendRequest("0?turn=off")
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        SendRequest("0?turn=on")
    End Sub

    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
        'Dispose of your WebClient when the form is closing...
        webClient.Dispose()
    End Sub

    Private Sub SendRequest(endpoint As String)
        Try
            Dim result As String = webClient.DownloadString(baseUrl + endpoint)
            'Process the returned result here...
        Catch ex As Exception
            'Handle any exceptions if any is returned...
        End Try
    End Sub

End Class
 
Share this answer
 
Comments
Ralf Meier 15-Nov-23 18:07pm    
+5 It`s much easier as my own Solution ... 👍 (and works similar 😃)
Andre Oosthuizen 16-Nov-23 11:24am    
"Danke Schön" Ralf! glad it worked out for you.
Although your question is not describing the issue which you are facing but from the last part of your question 'application freezes'.
Try to use Asynchronous programming to prevent the UI from freezing while the HTTP request is being sent. Furthermore, ensure that your project has the necessary permissions to access the network while sending request.
 
Share this answer
 
Comments
Ralf Meier 13-Nov-23 14:41pm    
which part isn't clear for you ?
I want to send the HTTP-Command "http://192.168.xxx.xxx/relay/0?turn=off" with the click of a Button for example.
Freezes means that I could send it 2 times and it works and then teh application isn't responsive anymore ...
Don't you have a useful sample for that ?
M Imran Ansari 13-Nov-23 23:31pm    
Thank you for sharing the code snippet, I have posted a sample as solution-3.
now I tried it like this - it seems to work ... but perhaps someone has a better Solution ?
VB.NET
Imports System.Net
Imports System.Text

Public Class Form1

    Dim client As WebRequest = WebRequest.Create("http://192.168.178.201/relay/")
    Dim response As WebResponse

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        client.Method = "POST"
        client = WebRequest.Create("http://192.168.178.201/relay/0?turn=off")
        response = client.GetResponse
        response.Close()
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        client.Method = "POST"
        client = WebRequest.Create("http://192.168.178.201/relay/0?turn=on")
        response = client.GetResponse()
        response.Close()
    End Sub

End Class
 
Share this answer
 
v2
Thank you for sharing your code snippet. It's good practice to use the Using statement along with an asynchronous method for the WebResponse to ensure that the resources are properly released, even if an exception occurs. Here is the sample which you can use and modify according to your needs.
VB.NET
Imports System.Net.Http

Public Class Form1

    Dim httpClient As New HttpClient()
    Dim baseUrl As String = "http://192.168.178.201/relay/"

    Private Async Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Await SendRequestAsync("0?turn=off")
    End Sub

    Private Async Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        Await SendRequestAsync("0?turn=on")
    End Sub

    Private Async Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
        ' To Make sure to dispose of the HttpClient when the form is closing
        httpClient.Dispose()
    End Sub

    Private Async Function SendRequestAsync(endpoint As String) As Task
        Try
            Using response As HttpResponseMessage = Await httpClient.PostAsync(baseUrl + endpoint, Nothing)
                response.EnsureSuccessStatusCode()
                ' Process the response here
            End Using
        Catch ex As Exception
            ' Handle exceptions if any
        End Try
    End Function

End Class
 
Share this answer
 
Comments
Ralf Meier 14-Nov-23 2:29am    
Thanks a lot for sharing your sample - I will a little later make tries with it ...
To make it easier for me : how should a request look like which gets an answer from the device ? Would you be so kind to improve your Solution with that Method ?
Ralf Meier 14-Nov-23 2:32am    
Since there is no useful information to be found in the web I decided to make an Article (later when I'm finished) which describes how to work with the Shelly-Devices (Smart-Home Relais)
Ralf Meier 14-Nov-23 2:47am    
Oops ... from where do I get the "System.Net.Http" ?
I'm using Framework 4.0 and it seems that there is no Namespace like that in it ...
Richard Deeming 14-Nov-23 3:56am    
System.Net.Http[^] requires at least .NET Framework 4.5.

.NET Framework 4.0 has been EOL since January 2016. Unless you're writing code for Windows XP or Server 2003 - both of which are also long-dead - you should look to update to at least 4.6.2, which is still supported on Server 2008 SP2.
Ralf Meier 14-Nov-23 4:26am    
thanks for your Reply, Richard.
The using of this Framework is a kind of Lazyness at me ... so now I will switch to 4.6.2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900