Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
212 views
in Technique[技术] by (71.8m points)

c# - System.Net.Http.HttpClient vs Windows.Web.Http.HttpClient - What are the main differences?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

Windows.Web.Http advantages

  • WinRT APIs are written in native code, which translates in better performance.
  • Windows.Web.Http is on top of a common Windows HTTP stack, and reuses resources already in use by other Windows components. System.Net.Http is a separate implementation of the HTTP protocol that is not frequently used by other Windows components. So, in some cases, you save resources by choosing Windows.Web.Http.
  • Windows.Web.Http has better integration with WinRT types, such as 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.
  • Windows.Web.Http has the new features, such as HTTP/2 support.
  • Windows.Web.Http is COM based and can be used by any programming language that understands COM.

System.Net.Http advantages

  • System.Net.Http is available since Windows 8 or .NET 4.5 and Windows.Web.Http is only available since Windows 8.1 and Windows Phone 8.1.
  • It is straight forward to port WinRT code using System.Net.Http to ASP.NET or Xamarin (Portable Class Library)
  • Windows 8 and 8.1 projects or desktop projects: ?
    • Authentication headers and credentials are isolated per HttpClient (example)
    • Cookie container isolated per HttpClient
    • Does not cache HTTP responses, so subsequent requests will never come from the cache, a common issue with servers that does not set the correct Cache-Control header (example)
    • Works with 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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...