Question Convert WebClient to HttpClient

jc30120

New member
Joined
Dec 1, 2022
Messages
4
Programming Experience
Beginner
This function works but I'd like to convert it to HttpClient so I can set a Timeout and also because I'm told the WebClient is obsolete :

VB.NET:
Function RelayControl() As String
    Dim ret_state As String
    Dim client As New WebClient
    
    client.Credentials = New NetworkCredential("admin", "1234")
    client.QueryString.Add("value", "false")
    
    Try
        client.UploadValues("[URL]http://192.168.1.101:80/restapi/relay/outlets/0/state/[/URL]", "PUT", client.QueryString)
        ret_state = client.DownloadString("[URL]http://192.168.1.101:80/restapi/relay/outlets/0/state/[/URL]")
    Catch e As Exception
        Return "Error"
    End Try
    
    Return ret_state
End Function

Many thanks
 
Last edited by a moderator:
I have formatted your code snippet properly. Please do so in future. Unformatted code is much harder to read, especially on small devices, primarily because there is no indenting.
 
Worse than stuck, I can't get anything to work. I have googled and read. Most things are in C and nothing seems to speak to what must be a simple solution.
 
Start by reading the Microsoft documentation for the class. Although not always, you can generally choose the language that examples are displayed in there. Any C# (not C) examples you find there can usually be converted to VB. There are various online converters that are varying degrees of effective, but I would recommend that any VB developer download and install Instant VB from Tangible Software Solutions. The free version can convert snippets and will handle things better than any online converter. You can then give it your best shot and, if it doesn't work, show us what you have actually done and tell us what actually happened.
 
I have tried multiple routines, none work. This one responds as noted.Ultimately I need to pass credentials, a datavalue (PUT), and a tmeout.
VB.NET:
Async Sub relaycontrol2()
    Dim uri As New Uri("[URL]http://192.168.1.101:80/restapi/relay/outlets/0/state/[/URL]")
    Dim credentialsCache = New CredentialCache From {
        {uri, "Negotiate", New NetworkCredential("admin", "1234")}}
    Dim handler = New HttpClientHandler() With {
       .Credentials = credentialsCache,
       .PreAuthenticate = True}
    Dim httpClient As New HttpClient(handler) With {.Timeout = New TimeSpan(0, 0, 10)}
    Dim response = Await httpClient.GetAsync(uri)
    Dim result = Await response.Content.ReadAsStringAsync()
    Label1.Text = result
End Sub

The output is:

{"error":[401,"Unauthorized"]}
 
Last edited by a moderator:
I've already told you once in this thread to format your code snippets and you have posted unformatted code again. I have formatted it for you again but this will be the last time. If you would like us to help you, please respect us enough to help us do so and format all code snippets so that we can read them comfortably.
 
With your "WebClient.Credentials = New NetworkCredential" code in post 1 request will use a supported authentication method (among Digest, Negotiate, Kerberos, NTLM, and Basic), with your last CredentialCache code you limit it to "Negotiate" method. To make it same you have to set "HttpClientHandler.Credentials =New NetworkCredential".

Default PreAuthenticate is false, also for WebClient, in last code you set it to true, so that is different from the Webclient code.

GetAsync+Content.ReadAsStringAsync can be shortened to GetStringAsync.

There is a "UploadValues" example here that you should look at: Convert WebClient to HttpClient
Example uses a dictionary for values and uploads them with FormUrlEncodedContent. For PUT you use PutAsync instead of PostAsync in that example.
 

Latest posts

Back
Top