Windows.Web.Http Windows.Web.Http Windows.Web.Http Windows.Web.Http Namespace

Provides a modern HTTP client API for UWP app.

Classes

HttpBufferContent HttpBufferContent HttpBufferContent HttpBufferContent

Provides HTTP content that uses a buffer.

HttpClient HttpClient HttpClient HttpClient

Sends HTTP requests and receives HTTP responses from a resource identified by a URI.

HttpCookie HttpCookie HttpCookie HttpCookie

Provides a set of properties and methods to manage an HTTP cookie.

HttpCookieCollection HttpCookieCollection HttpCookieCollection HttpCookieCollection

Provides a collection container for instances of the HttpCookie class.

HttpCookieManager HttpCookieManager HttpCookieManager HttpCookieManager

Add or delete an HttpCookie or view the cookies associated with an app.

HttpFormUrlEncodedContent HttpFormUrlEncodedContent HttpFormUrlEncodedContent HttpFormUrlEncodedContent

Provides HTTP content that uses name/value data encoded with the application/x-www-form-urlencoded MIME type.

HttpMethod HttpMethod HttpMethod HttpMethod

Retrieves standard HTTP methods such as GET and POST and creates new HTTP methods.

HttpMultipartContent HttpMultipartContent HttpMultipartContent HttpMultipartContent

Provides HTTP content that uses the multipart/* MIME type.

HttpMultipartFormDataContent HttpMultipartFormDataContent HttpMultipartFormDataContent HttpMultipartFormDataContent

Provides HTTP content that uses the multipart/form-data MIME type.

HttpRequestMessage HttpRequestMessage HttpRequestMessage HttpRequestMessage

Represents an HTTP request message including headers.

HttpResponseMessage HttpResponseMessage HttpResponseMessage HttpResponseMessage

Represents an HTTP response message including headers, the status code, and data.

HttpStreamContent HttpStreamContent HttpStreamContent HttpStreamContent

Provides HTTP content that uses a stream.

HttpStringContent HttpStringContent HttpStringContent HttpStringContent

Provides HTTP content that uses a string.

HttpTransportInformation HttpTransportInformation HttpTransportInformation HttpTransportInformation

Provides information about the underlying transport used by the HTTP connection.

Structs

HttpProgress HttpProgress HttpProgress HttpProgress

Contains status information on the progress of an HttpClient operation.

Interfaces

IHttpContent IHttpContent IHttpContent IHttpContent

Provides a base interface for an HTTP entity body and content headers.

Enums

HttpCompletionOption HttpCompletionOption HttpCompletionOption HttpCompletionOption

Indicates whether asynchronous HttpClient operations are considered completed when all of the response is read, or when just the headers are read.

HttpProgressStage HttpProgressStage HttpProgressStage HttpProgressStage

Indicates the step in the progress for an HTTP connection.

HttpResponseMessageSource HttpResponseMessageSource HttpResponseMessageSource HttpResponseMessageSource

Indicates the source of the data received in the HttpResponseMessage.

HttpStatusCode HttpStatusCode HttpStatusCode HttpStatusCode

Contains the values of status codes defined for HTTP in the response to an HTTP request.

HttpVersion HttpVersion HttpVersion HttpVersion

Represents the HTTP protocol version.

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 UWP 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:

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.
  • HttpBufferContent - HTTP content that uses a buffer.
  • HttpFormUrlEncodedContent - HTTP content that uses name/value tuples encoded with the application/x-www-form-urlencoded MIME type.
  • HttpMultipartContent - HTTP content that uses multipart/* MIME type.
  • HttpMultipartFormDataContent - HTTP content that uses the encoded multipart/form-data MIME type.
  • HttpStreamContent - HTTP content that uses a stream. This content type is used by the HTTP GET method to receive data and the HTTP POST method to upload data.
  • HttpStringContent - HTTP content that uses a string.

Classes in the Windows.Web.Http.Headers namespace represent HTTP headers defined in RFC 2616. HTTP headers are associated with HttpRequestMessage and HttpResponseMessage as properties that are retrieved or set.

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.

For sample code in JavaScript and HTML that shows how to use HttpClient to connect to an HTTP server, see Connecting to an HTTP server using Windows.Web.Http.

See Also