Classes in the Windows.Web.Http namespace provide a programming interface for modern HTTP client apps. The Windows.Web.Http namespace and the related Windows.Web.Http.Headers and Windows.Web.Http.Filters namespaces provide HTTP client components that allow users to make HTTP requests and receive HTTP responses from modern web services over HTTP.
For basic HTTP request operations, the Windows.Web.Http.HttpClient has a simple interface to handle the most common tasks and provides sensible defaults for authentication (AUTH) that apply to most scenarios. For more complex HTTP operations, many additional capabilities are available.
Unlike XMLHttpRequest, which allows you to specify whether the request should be made asynchronously, WinJS.xhr is always asynchronous. WinJS.xhr returns a WinJS.Promise, so that you can handle completed requests, errors, and in-progress requests by using the then or done methods.
|Windows 8.1, Windows Server 2012 R2||
This is the recommended method to connect to a HTTP server to send and receive data.
|WinJS.xhr||Windows 8, Windows Server 2012||
On Windows 8 and Windows Server 2012, this was the recommended method when XMLHttpRequest was not adequate.
For more information on how to connect to a web service for Windows Store apps written in C++/XAML and apps using the .NET Framework 4.5 and written in C# or Visual Basic, see Connecting to web services (Windows Store apps using C#/VB/C++ and XAML).
You can upload and download binary data when you use WinJS.xhr (which wraps XMLHttpRequest). This example shows how to upload a bitmap from your Pictures. The Windows Runtime method that is used to open the bitmap file returns an IRandomAccessStream, so you must use MSApp.createBlobFromRandomAccessStream to convert it to a Blob.
When you request Web resources by using WinJS.xhr, the response might be cached, which means that later requests will return the version of the resource that already exists on the client computer rather than resending the request. However, you can add a HTTP header that ensures that the request is sent again, even if it has already been cached.
When you use XMLHttpRequest, you can set time-out values directly, but you cannot do this when you use Windows.Web.Http.HttpClient or WinJS.xhr. However, there is a way to set time-outs on WinJS.Promise objects. By calling WinJS.Promise.timeout, you ensure that the request is canceled if it has not completed within the specified time.
- Other resources
- Handling exceptions in network apps
- How to configure network isolation capabilities
- How to enable loopback and debug network isolation
- HttpClient Sample
Build date: 11/26/2013