One of the next most interesting APIs added to Windows 8.1 that I also write in Chapter 4 of my Second Edition Preview is the Windows.Web.Http.HttpClient API (and associated classes). On the surface–and the API is designed to be very simple and easy to use for common cases–it seems redundant with other available APIs like WinJS.xhr (i.e. XMLHttpRequest) or the HttpClient API in .NET. In that sense it at least provides a robust HTTP API for C++ developers.

Digging down a little deeper, however, this new API goes well beyond the existing ones and should, in time, obsolete the other methods. For one thing, it provides cache control and access to cookies unlike the existing APIs. In comparison to the .NET API, this new WinRT API works with the WinINet cache, so benefits from any caching that happens via Internet Explorer, other apps, or the pre-caching API that’s now in Windows.Networking.BackgroundTransfer.ContentPrefetcher class.

The new API also provides a low-level hooking mechanism called filters that allows you to place handling code beneath the level of app logic. The HttpClient sample that’s now available shows how to create filters for authentication, 503 retries, and network cost awareness, meaning that the rest of the app can ignore the details of such concerns. That is, instead of having every HTTP request in your app have to think about cost awareness, you let the filter do that. The rest of the code either just gets the data or doesn’t, which makes it a lot simpler to write.

For additional details, also see Peter Smith’s talk from //build 2013: Five Great Reasons to Use the New HttpClient API to Connect to Web Services.


Comments are closed