Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does all methods of HttpClient internally invoke SendAsync method?

Does all methods of HttpClient i.e. GetAsync, PostAsync etc. internally invoke SendAsync method?

like image 596
shou Avatar asked Aug 28 '26 21:08

shou


1 Answers

Yes, the HttpClient uses an HttpMessageHandler underneath to perform all HTTP requests. The HttpMessageHandler method Task<HttpResponseMessage> SendAsync(HttpRequestMessage, CancellationToken) is what is called by the HttpClient.

The default implementation of the abstract class HttpMessageHandler is HttpClientHandler.

You can pass in your own HttpMessageHandler implementation to the HttpClient constructor that takes one. Although it's highly unlikely you'd ever need to, there are applications. For example, if you wanted to log every request your HttpClient makes. You could make a LoggingHttpMessageHandler decorator for a HttpMessageHandler.

using (var handler = new HttpClientHandler())
using (var loggingHandler = new LoggingHttpClientHandler(handler, logger))
using (var client = new HttpClient(loggingHandler))
{
    // Logs "GET https://www.google.com/"
    var response = await client.GetAsync("https://www.google.com/");
    ...
}
like image 106
Timothy Shields Avatar answered Aug 30 '26 20:08

Timothy Shields



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!