Does HttpClient uses HttpWebRequest behind the scenes

Topics: ASP.NET Web API
May 5, 2012 at 8:56 AM

As you learnt from this question ( and this one ( unfortunately is not very asynchronous, even if you use the "asynchronous" methods. It does a blocking DNS lookup, at least. It may also block during proxy negotiation and/or the initial HTTP connection.

I wonder if HttpClient uses HttpWebRequest behind the scenes anywhere.

I believe that the HttpClient is part of .NET framework v4.5 now and this therefore might be not a good place for this discussion.

May 5, 2012 at 5:42 PM

Using Reflector, it can be seen that HttpWebRequest has been extensively used in the latest version of HttpClient (mainly inside HttpClientHandler) which was released as part of NuGet packages of System.Net.Http.dll with Web API source code.

May 6, 2012 at 2:30 AM

HttpClient uses HttpWebRequest by default, but in truth it can be used by anything which can be represented by a message handler.

May 6, 2012 at 8:25 PM

HttpClient is layered on top of HttpWebRequest but the object models are kept separate. The way it is hooked in as -- of course -- as an HttpMessageHandler: You can either use the default HttpClientHandler which exposes a "portable" subset of HttpWebRequest or WebRequestHandler which exposes most of the HttpWebReqeust details exposed in full .NET.

As Brad mentions, you can plug in any other HTTP engine below but for practical reasons we went with HttpWebRequest. It is correct that is is not a perfect situation and that HttpWebRequest blocks on DNS and other items. We are aware of this and investigating what we may be able to do but can't say anything at the moment.