Does HttpClient uses HttpWebRequest behind the scenes

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

As you learnt from this question (http://stackoverflow.com/questions/10386244/unresponsiveness-with-async-event-handlers-in-wpf-in-net-4-5) and this one (http://social.msdn.microsoft.com/Forums/en-US/async/thread/9f75f92c-9a51-4921-bad5-97c2b15870bdHttpWebRequest 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 6: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 3: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 9: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.