Examples

The following sample code shows how to GET content from an HTTP server as a string.

// Note: the URI constructor will throw an exception
// if the string passed is not a valid URI
var uri = new Uri("http://example.com/datalist.aspx");
var httpClient = new HttpClient();
// Always catch network exceptions for async methods
httpClient.GetStringAsync(uri).done(function () {
// get completed
}, onError);
function onError(reason) {
// Details in reason.Message and ex.HResult.
}
// Once your app is done using the HttpClient object call close to
// free up system resources (the underlying socket and memory used for the object)
// httpClient.close();

using System;
using Windows.Foundation;
using Windows.Web.Http;
// Note: the URI constructor will throw an exception
// if the string passed is not a valid URI
var uri = new Uri("http://example.com/datalist.aspx");
var httpClient = new HttpClient();
// Always catch network exceptions for async methods
try
{
var result = await httpClient.GetStringAsync(uri);
}
catch (Exception ex)
{
// Details in ex.Message and ex.HResult.
}
// Once your app is done using the HttpClient object call dispose to
// free up system resources (the underlying socket and memory used for the object)
httpclient.Dispose();

using namespace Windows::Foundation;
using namespace Windows::Web::Http;
// Note: the URI constructor will throw an exception
// if the string passed is not a valid URI
uri = ref new Uri("http://example.com/datalist.aspx");
httpClient = ref new HttpClient();
// Always catch network exceptions for async methods
try
{
httpClient->GetStringAsync(uri);
}
catch (Exception ^ ex)
{
// Details in ex.Message and ex.HResult.
}
// In C++ and CX, the system resources used by httpClient object are released
// when the object falls out of scope or by the destructor (delete operator)

Remarks

The Windows.Web.Http namespace and the related Windows.Web.Http.Headers and Windows.Web.Http.Filters namespaces provide a programming interface for Windows Store app that target HTTP and REST services. This features of this new HTTP API are designed to be in compliance with HTTP as defined in RFC 2616 by the IETF. The new HTTP API provides consistent support in JavaScript, C#, VB.NET, and C++ for developers.

This new API replaces the use of three different APIs with differing features that previously were needed for each language projection in Windows 8.

Classes in the Windows.Web.Http and related namespaces are targeted at all levels of HTTP and REST development:

Basic HTTP developers

Site-specific HTTP library developers

Advanced HTTP developers

For basic HTTP developers, the new API has a simple interface to handle the most common tasks and sensible defaults for authentication that should work in most environments. For library developers, the consistent object model and multiple language support means you can write a library once for developers in all languages. For advanced HTTP developers, the new API includes a rich set of capabilities.

The Windows.Web.Http namespace and the related namespaces provide these components:

HttpClient - the main class that sends and receives requests over HTTP.

The contents of an HTTP message corresponds to the entity body defined in RFC 2616. Several classes and an interface in the Windows.Web.Http namespace can be used for HTTP content, including:

IHttpContent - A base interface for developers to create their own content objects. It represents an HTTP entity body and content headers. This interface has methods that get and set the actual content data. It also provides properties that get and set content related headers.

Classes in the Windows.Web.Http namespace can use filters based on the classes in the Windows.Web.Http.Filters namespace. Filters provide handlers to help with common HTTP service issues. Filters can be chained together in a sequence to address more complex HTTP service issues. Several ready-to-use filters are included with Windows 8.1 to help library developers fix common HTTP service issues. These filters include handlers for monitored network connections and retry. Library developers can write their own filters for site-specific issues (for example, a site might use the 503 (Server Unavailable) response to indicate the request should be retried).

If an app that uses the Windows.Web.Http namespace and the related Windows.Web.Http.Headers and Windows.Web.Http.Filters namespaces downloads large amounts of data (50 megabytes or more), then the app should stream those downloads and not use the default buffering. If the default buffering is used the client memory usage gets very large, potentially resulting in reduced performance.

For sample code in C#/VB/C++ and XAML that shows how to use HttpClient to connect to an HTTP server, see HttpClient.