When developing .NET 4.5 desktop apps for Windows I have been used to use System.Net.Http.HttpClient
for all communication with a backend Web API. I am now developing a Windows Store app and has noticed the existence of Windows.Web.Http.HttpClient
. I have looked for information on what the main differences are between the two clients but without any luck.
From MSDN I know that I should start using Windows.Web.Http.HttpClient
in my Windows Store app since System.Net.Http.HttpClient
might be removed from the API:
Note The System.Net.Http and System.Net.Http.Headers namespace might not be available in future versions of Windows for use by Windows Store apps. Starting with Windows 8.1 and Windows Server 2012 R2, use Windows.Web.Http.HttpClient in the Windows.Web.Http namespace and the related Windows.Web.Http.Headers and Windows.Web.Http.Filters namespaces instead for Windows Runtime apps.
But apart from this information, I have a hard time figuring out what are the main differences and what is the main benefit of using Windows.Web.Http.HttpClient
? What does it add that we don't already got in System.Net.Http.HttpClient
?
Answers backed by official documentation are greatly appreciated.
The HttpClient class instance acts as a session to send HTTP requests. An HttpClient instance is a collection of settings applied to all requests executed by that instance. In addition, every HttpClient instance uses its own connection pool, isolating its requests from requests executed by other HttpClient instances.
The default handler is HttpClientHandler, which sends the request over the network and gets the response from the server. You can insert custom message handlers into the client pipeline: ASP.NET Web API also uses message handlers on the server side. For more information, see HTTP Message Handlers.
HttpClient is a more advanced class, and was added later, than other classes like WebClient. This class is built for asynchronous use. For downloading web pages, it is better to enable and support GZIP compression. Another header can be added to HttpClient. We can use VB.NET to decompress these files.
Windows.Web.Http is a WinRT API available in all the WinRT programming languages supported: C#, VB, C++/CX and JavaScript. This enables the option to write the same code in the language of your choice.
System.Net.Http is a .NET API, and it is only available for C# and VB developers.
IInputStream
, IOutputStream
and IBuffer
. Avoiding the .NET extensions that convert System.IO.Stream
into IInputStream
or IOutputStream
and System.Array
into Windows.Storage.Streams.IBuffer
can improve performance and save resources in some cases.HttpClient
(example)HttpClient
Cache-Control
header (example)System.Net.NetworkCredential
† For Windows Universal Projects (UWP), System.Net.Http is a wrapper on top of Windows.Web.Http, as described here.
Further reading: Demystifying HttpClient APIs in the Universal Windows Platform
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With