RSS icon
Carl Chester Lloyd

PostsAboutReading

HttpClient

August 25, 2019

using(var httpClient = new HttpClient())
{
    var result = await httpClient.GetAsync("url");
}

The above is the most widespread common usage of HttpClient around, and it is wrong. We are going to spend this article talking about the HttpClient, what came before it, what this usage is trying to do, why it is wrong, and the proper way to use it. Now onto the meat of the matter.

What is HttpClient

The HttpClient is the Microsoft approved way of both sending HTTP requests and receiving HTTP responses. So basically anytime you want to interact with an API, and generally build things on, and with the web you will be using the HttpClient. Before HttpClient there was the HttpWebRequest that was used for all the things that client is currently used for, but a little less. Client is more versatile and straightforward to use than its predecessor. HttpWebRequest has since been deprecated.

What the Using is doing

The code at the top of this article is pretty simple. It is instantiating a new instance of the HttpClient and setting it in the httpClient variable. Then the GetAsync method is called on the object with some “url”, and a result is returned.

The reason why the using statement is used, and why so many people use the using statement is because for most things like the HttpClient that is the proper usage of the using statement. You see the HttpClient implements HttpMessageInvoker which in turn implements the IDisposable interface.

The implementation of the IDisposable interface is important because it “provides a mechanism for releasing unmanaged resources”. When a new instance of HttpClient is created, and used there are resources that need to be freed up when the client is no longer in use.

The Using statement makes sure that those resources get freed up by having the Dispose method of the IDisposable get called. For an IDisposable implementing class like HttpClient you would think this would be a match made in heaven.

The Problem

The problem with this is though that whenever an httpclient is created it uses a socket. The more that get created, the more sockets get used up. Eventually this can lead to SocketExceptions because there is a wait time until those sockets become closed.

Better Usage

The better, and intended way to use HttpClient is as a single static instance for the lifetime of an application.

static readonly HttpClient client = new HttpClient();
var result = await client.GetAsync("url");

This ensures that you do not run out of available sockets when making many requests.

Extra tidbit about client vs request

While trying to use someone else’s API I found out that the HttpWebRequest does not support GET requests with a body. However, HttpClient does support this. The HttpWebRequest ends up throwing an exception if you try to send a GET with a body.


Carl Lloyd

Written by Carl Lloyd. He spends his time playing with technology, and learning new things.