HttpWebRequest to HttpClient post Request

Marcello

with HttpWebRequest i use this code to post request and wait to get an async response

Private Async Sub SendHttpWebReq(ByVal jsonString as String)
    ' create the HttpWebRequest
    Dim httpWebReq = CType(Net.WebRequest.Create("http://www.foo.foo"), Net.HttpWebRequest)
    ' set the HttpWebRequest
    httpWebReq.Method = "POST"
    httpWebReq.KeepAlive = True
    httpWebReq.ContentType = "application/json"
    With httpWebReq.Headers
       .Add(Net.HttpRequestHeader.AcceptCharSet, "ISO-8859-1,utf-8")
       .Add("X-User", user)
    End With
    ' transform the json String in array of Byte
    Dim byteArray as Byte() = Encoding.UTF8.GetBytes(jsonString)
    ' set the HttpWebRequest Content length
    httWebReq.ContentLEngth = byteArray.Length
    ' create a HttpWebRequest stream
    Dim dataStream as IO.Stream = httpWebReq.GetRequestStream()
    ' send the request in a stream
    dataStream.Write(byteArray, 0, byteArray.Length)
    dataStream.Close()
    ' create the HttpWebRequest response
    Dim Response as Net.WebResponse = Await httpWebReq.GetResponseAsync()
    Dim ResponseStream as IO.Stream = Response.GetResponseStream()

End Sub

I tried to 'translate' the code to HttpClient in this mode

Dim HttpClientReq as HttpClient
With HttpClientReq
    .BaseAddress = New Uri("Http://www.foo.foo")
    .DefaultRequestHeaders.Accept.Clear()
    .DefaultRequestHeaders.Accept.Add(New Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")
    .DefaultRequestHeaders.Add("X-User", user)
End With

Private Async Sub SendHttpClientReq(Byval jsonString as String)
    Dim Request As Net.Http.HttpRequestMessage
    Request.Content = New Net.Http.StringContent(jsonString, System.Text.Encoding.UTF8, "application/json")
    Dim Response as Net.Http.HttpResponseMessage = Await HttpClientReq.PostAsync(HttpClientReq.BaseAddress)

End sub

But i need to understand how to approach to HttpClient.

While in the HttpWebRequest: i

  • create an instance of the class
  • set the parameters (and headers)
  • post a request
  • create a response that wait for the server response
  • hooking the response to the request, i receive the server response

With HttpClient: i

  • create the instance of the class
  • set the parameters (and headers)
  • post a request
  • i don't see any method that let me hook a response to a request, so i don't know how to wait to receive the response.

Any suggestion?

Also in c#

Nkosi

Net.Http.HttpClient has a primarily task based API. As for hooking a response to a request, No need. Just await the resposne and use it as desired once returned.

Dim client As HttpClient
With client
    .BaseAddress = New Uri("Http://www.foo.foo")
    .DefaultRequestHeaders.Accept.Clear()
    .DefaultRequestHeaders.Accept.Add(New Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")
    .DefaultRequestHeaders.Add("X-User", user)
End With

'...'

Private Async Function PostAsync(Byval jsonString as String) As Task

    Dim content As New Net.Http.StringContent(jsonString, System.Text.Encoding.UTF8, "application/json")
    Dim response As Net.Http.HttpResponseMessage = Await client.PostAsync("", content)

    Dim result As String = Await response.Content.ReadAsStringAsync()

    'do something with the result.'

End Function

Avoid async Subs/void. In this case a function returning a Task would be required to properly await the async functions.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Convert HttpWebRequest to HttpClient with POST method

GZip POST request with HTTPClient in Java

F# POST request with HttpClient

HttpClient Post request with Json body

Post from request body with HttpClient

HttpWebRequest post paramaters for AWS file upload request, leading to 400 error

Error in VB.NET JSON POST request - HTTPWEBREQUEST

Refactor HttpWebRequest to HttpClient?

Issue with HttpClient Post request URL, no changes in Java

Multipart Post request in Apache Httpclient 4.5.6 not working

How to POST JSON request using Apache HttpClient?

HTTPClient Post Returns Bad Request 400

Send a json on a post request with HttpClient and C#

Custom header in HttpClient POST request not working

HttpClient or Http POST with header request not hitting to the server

Receiving access denied on HttpClient Post request

Pass Files in C# HttpClient Post request

How to convert HttpWebRequest with POST method of a legacy program to HttpClient, using c#?

Apache HttpClient 4.5 redirect POST request to GET request

HttpClient Post request not sending post parameters in C#

C# HttpClient or HttpWebRequest class

HttpWebRequest chunked/async POST

HttpWebRequest POST method Exception

Post Data for a site in httpwebrequest

HttpWebRequest - Post with __EVENTTARGET and __EVENTARGUMENT

HttpWebRequest not sending cookies on POST

Returning byte[] data on POST request in does not return the same data I need when calling it through HttpWebRequest

commons httpclient - Adding query string parameters to GET/POST request

HTTPClient POST request not working on Angular 5 / Angular Universal