WebRequest Migration to HTTPClient - Needing Feedback to Calling Function

Smith, Jeffery Thomas (Jeff) 156 Reputation points
2025-07-28T16:12:50.4066667+00:00

Hello.. I have some code migrating from VS Studio 2013 to VS Studio 2022. Seeing that WebReq has been deprecated. Working to migrate to HTTPClient. Seems that httpclient is async only, I am guessing here... My overall code is written to call a RoboCall server, wait for a success message then pass back to the caller, success or failure. I have been doing this with WebRequest. What I have found searching Google thus far is httpclient appears to be the replacement, but I seem to be having difficulty finding a way to call this function, have it wait for a success message then return to the caller. Hoping someone can point me in the right direction. Thanks // Jeff


    Public Async Sub PostData(ByVal strMARCLId As String, ByVal strLogonId As String, ByVal strURI As String, ByVal strjsonPayload As String, ByVal strAuthString As String)

        Using client As New HttpClient()

            Try

                'https://onp.vzwnet.com/notification-sms-n-voice/notification/notifyAll' \

                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12

                client.DefaultRequestHeaders.Authorization = New AuthenticationHeaderValue("Basic", strAuthString)

                client.Timeout = TimeSpan.FromSeconds(500)

                Dim strContent As New StringContent(strjsonPayload, Encoding.UTF8, "application/json")

                '------------------ Send the POST request
                Dim response As HttpResponseMessage = Await client.PostAsync(strURI, strContent)

                '------------------ Ensure the response was successful
                response.EnsureSuccessStatusCode()

                '------------------ Read and display the response body
                Dim responseBody As String = Await response.Content.ReadAsStringAsync()

                MsgBox("Response Status: " & response.StatusCode.ToString())
                MsgBox("Response Body: " & responseBody)

                '------------------

                Dim strMessage As String = "Processsed API Request"

                Dim strSubject As String = "Processsed API Request - " & response.StatusCode.ToString() & " / " & responseBody

                GeneralMailerBLL.BLL__DataProcessor__GeneralMailer__Test_Mailert(strMARCLId, strLogonId, strSubject, strMessage)

                client.Dispose()

            Catch ex As Exception

                MsgBox("Error: " & ex.Message)

                Dim strMessage As String = "WARNING -- Unable to Process API Request - " & ex.Message & " / " & strURI & " / " & strjsonPayload & " / " & strAuthString

                Dim strSubject As String = "WARNING -- Unable to Process API Request"

                GeneralMailerBLL.BLL__DataProcessor__GeneralMailer__Test_Mailert(strMARCLId, strLogonId, strSubject, strMessage)

                client.Dispose()

            End Try

        End Using

Developer technologies | VB
{count} votes

Accepted answer
  1. Paula Payeras (Babtec GmbH) 75 Reputation points
    2025-07-29T08:23:12.0033333+00:00

    Hello, To use await, the calling method must also be declared as async.

    If you want to avoid declaring the method as async, you can convert the asynchronous call into a synchronous one like this:
    Dim result = Task.Run(Function() AsyncMethod(parameters)).Result

    However, be careful: using .Result or .Wait() can lead to deadlocks, especially in UI applications where there's a synchronization context. It can also degrade performance by blocking threads unnecessarily.
    Whenever possible, it's recommended to use async/await

    Regards

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.