The first thing to remember is that HttpClient uses the HttpMessageHandler pipeline for sending and receiving requests. The default handler implementations (HttpClientHandler and WebRequestHandler) both sit on top of HttpWebRequest which has a lot of properties for controlling how to deal with requests and responses. However, in order to stay simple, HttpClient doesn’t expose all these properties up front – it would make it unwieldy and show a lot of things that are rarely used.

If you need to access these properties then there are two ways of doing it: use either HttpClientHandler or WebRequestHandler. Both are HttpMessageHandlers hooked in as part of the HttpMessageHandler pipeline. Below we will describe when and how to use these handlers, how they differ, as well as how to combine them with your own handlers.

HttpClient

The default HttpClient is the simplest way in which you can start sending requests. A single HttpClient can be used to send as many HTTP requests as you want concurrently so in many scenarios you can just create one HttpClient and then use that for all your requests.

HttpClient has a few properties that are commonly used and which you can set up until the point where you start sending requests, namely:

If you have additional handlers which you want to wire up as well then the simplest way is to use the HttpClientFactory class. Here’s an example wiring up 3 custom message handlers:

1:// Create an HttpClient and add message handlers for the client

2: HttpClient client = HttpClientFactory.Create(

3:new SampleHandler("Client A", 2),

4:new SampleHandler("Client B", 4),

5:new SampleHandler("Client C", 6));

HttpClientHandler

HttpClientHandler is an HttpMessageHandler with a common set of properties that works across most versions of the HttpWebRequest API. This is the default handler and so is what you get if you use the default constructor. However, in order to actually get access to the various properties (see below) you have to explicitly create it and then pass it to HttpClient.

Gets or sets a value that indicates whether the handler uses a proxy for requests.

Here’s an example of how to create an HttpClientHandler, set a property, and pass it to HttpClient:

1:// Create HttpClientHandler and set UseDefaultCredentials property

2: HttpClientHandler clientHandler = new HttpClientHandler();

3: clientHandler.UseDefaultCredentials = true;

4:

5:// Create an HttpClient using the HttpClientHandler

6: HttpClient client = new HttpClient(clientHandler);

Like above, you can combine your HttpClientHandler instance with other custom handlers, here’s what it looks like:

1:// Create HttpClientHandler and set UseDefaultCredentials property

2: HttpClientHandler clientHandler = new HttpClientHandler();

3: clientHandler.UseDefaultCredentials = true;

4:

5:// Create an HttpClient and add message handlers for the client

6: HttpClient client = HttpClientFactory.Create(

7: clientHandler,

8:new SampleHandler("Client A", 2),

9:new SampleHandler("Client B", 4),

10:new SampleHandler("Client C", 6));

WebRequestHandler

WebRequestHandler derives from HttpClientHandler but adds properties that generally only are available on full .NET. The WebRequestHandler is not included in the System.Net.Http DLL but rather in System.Net.Http.WebRequest DLL so you have to explicitly include that as a reference in order to see it. Otherwise it won’t show up.

List of Properties for WebRequestHandler in addition to those of HttpClientHandler

Wouldn't it be more correct to say HttpClientHandler sits on top of HttpWebRequest rather than HttpClient. Arguably, HTTPClient has no dependency on HttpWebRequest. HttpClient could use a completely new HTTP engine if someone built it.

I have API which connects to other api instance ( hosted on different data centers) based on value selected it may connect datacenter 1 API or datacenter n API. Is HTTPClient is an option here ? How it maintains State because based on data center selection it talks to corresponding API. All data center API's talk to Main API . Right now I am using HTTPWebRequest. Exploring and understanding the power of HTTPClient. your input on this will be greatly appreciated.

Hello Henrik, I've a web forms project which calls WCF rest services (reside in the same project) from code behind using HtttpClient. Recently we've added forms authentication to our project and when we hit the service using HttpClient, we're redirected to the login page. I tried to use the CookieContainer (from WebRequestHandler.CookieContainer) to pass all the cookies from the original request to the code behind to WCF via httpclient but no good. Can you please suggest if this is possible or what i'm doing wrong? Thanks~

ClientCertificateOptions in HttpClientHandler does not get or set the collection of security certificates that are associated with this request. Rather it is a toggle between "Automatic" and "Manual". Do you know if there is a way to set the SecurityCertificate from HttpClientHandler (rather than WebRequestHandler). I'm trying to set the certificate in a Universal App.

I do see that I can do it if I switch to Microsoft.Web's HttpClient using HttpBaseProtocolFilter but I'd rather share code without a lot of #ifdefs