Windows 10 requirements

Examples

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

var uri = new Windows.Foundation.Uri("http://www.bing.com");
var httpClient = new Windows.Web.Http.HttpClient();
// Always catch network exceptions for async methods
httpClient.getStringAsync(uri).done(function (response) {
// do something with the string in the response variable.
}, 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;
var uri = new System.Uri("http://www.bing.com");
using (var httpClient = new Windows.Web.Http.HttpClient())
{
// Always catch network exceptions for async methods
try
{
string result = await httpClient.GetStringAsync(uri);
}
catch (Exception ex)
{
// Details in ex.Message and ex.HResult.
}
}
// Having exited the scope of the using statement, httpClient.Dispose() will be called
// automatically, thus freeing up system resources (the underlying socket, and memory
// used for the object).

using namespace Windows::Foundation;
using namespace Windows::Web::Http;
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
{
// Details in ex.Message and ex.HResult.
}
// In C++/CX, the system resources used by httpClient object are released
// when the object falls out of scope or by the destructor (delete operator).

The HttpClient class is often used by an app to download and then parse text. It is possible that the character encoding specified in the Content-Type header by an HTTP server does not match the character encoding of the HTTP response body (the XML encoding in an XML document, for example). One way to use HttpClient with text is to call the GetStringAsync method and pass the returned string to the text parser. However, this can result in errors if the Content-Type is not a type expressible as a string. A reliable way to use HttpClient with an XML parser is to call the GetBufferAsync method and parse the buffer for the "<?xml>" element. Then use the character encoding specified ("<xmlversion="1.0" encoding="UTF-8"?>", for example) to parse the HTTP response body. For other text formats, similar methods can be used where the app scans the initial part of the HTTP response body to determine the character encoding used.

Remarks

The HttpClient class instance acts as a session to send HTTP requests and receive responses. An HttpClient instance is a collection of settings that apply 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 HttpClient also acts as a class to use with filters for more specific HTTP clients. An example would be an HttpClientFilter that provides additional methods specific to a social network service (a GetFriends method, for instance).

If an app using HttpClient and related classes in the Windows.Web.Http namespace 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 will get 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.