HttpClient HttpClient HttpClient HttpClient HttpClient Class

Definition

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

public : sealed class HttpClient : IClosable, IStringable, IHttpClient
struct winrt::Windows::Web::Http::HttpClient : IClosable, IStringable, IHttpClient
public sealed class HttpClient : IDisposable, IStringable, IHttpClient
Public NotInheritable Class HttpClient Implements IDisposable, IStringable, IHttpClient
var httpClient = new httpClient();
Attributes
Windows 10 requirements
Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

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).
#include "winrt/Windows.Foundation.h"
#include "winrt/Windows.Web.Http.h"
using namespace winrt;

Windows::Foundation::IAsyncAction HttpClientExample()
{
    Windows::Foundation::Uri uri{ L"http://www.bing.com" };
    Windows::Web::Http::HttpClient httpClient{};

    // Always catch network exceptions for async methods
    try
    {
        auto response = co_await httpClient.GetStringAsync(uri);
    }
    catch (winrt::hresult_error const& ex)
    {
        // Details in ex.message and ex.code.
    }

    // The destructor of HttpClient frees 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.

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.

Constructors

HttpClient() HttpClient() HttpClient() HttpClient() HttpClient()

Initializes a new instance of the HttpClient class.

HttpClient(IHttpFilter) HttpClient(IHttpFilter) HttpClient(IHttpFilter) HttpClient(IHttpFilter) HttpClient(IHttpFilter)

Initializes a new instance of the HttpClient class with a specific filter for handling HTTP response messages.

Properties

DefaultRequestHeaders DefaultRequestHeaders DefaultRequestHeaders DefaultRequestHeaders DefaultRequestHeaders

Gets a collection of headers that should be sent with each request.

Methods

Close() Close() Close() Close() Close()

Closes the HttpClient instance and releases allocated resources.

DeleteAsync(Uri) DeleteAsync(Uri) DeleteAsync(Uri) DeleteAsync(Uri) DeleteAsync(Uri)

Send a DELETE request to the specified Uri as an asynchronous operation.

Dispose() Dispose() Dispose() Dispose() Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

GetAsync(Uri) GetAsync(Uri) GetAsync(Uri) GetAsync(Uri) GetAsync(Uri)

Send a GET request to the specified Uri as an asynchronous operation.

GetAsync(Uri, HttpCompletionOption) GetAsync(Uri, HttpCompletionOption) GetAsync(Uri, HttpCompletionOption) GetAsync(Uri, HttpCompletionOption) GetAsync(Uri, HttpCompletionOption)

Send a GET request to the specified Uri with an HTTP completion option as an asynchronous operation.

GetBufferAsync(Uri) GetBufferAsync(Uri) GetBufferAsync(Uri) GetBufferAsync(Uri) GetBufferAsync(Uri)

Send a GET request to the specified Uri and return the response body as a buffer in an asynchronous operation.

GetInputStreamAsync(Uri) GetInputStreamAsync(Uri) GetInputStreamAsync(Uri) GetInputStreamAsync(Uri) GetInputStreamAsync(Uri)

Send a GET request to the specified Uri and return the response body as a stream in an asynchronous operation.

GetStringAsync(Uri) GetStringAsync(Uri) GetStringAsync(Uri) GetStringAsync(Uri) GetStringAsync(Uri)

Send a GET request to the specified Uri and return the response body as a string in an asynchronous operation.

PostAsync(Uri, IHttpContent) PostAsync(Uri, IHttpContent) PostAsync(Uri, IHttpContent) PostAsync(Uri, IHttpContent) PostAsync(Uri, IHttpContent)

Send a POST request to the specified Uri as an asynchronous operation.

PutAsync(Uri, IHttpContent) PutAsync(Uri, IHttpContent) PutAsync(Uri, IHttpContent) PutAsync(Uri, IHttpContent) PutAsync(Uri, IHttpContent)

Send a PUT request to the specified Uri as an asynchronous operation.

SendRequestAsync(HttpRequestMessage) SendRequestAsync(HttpRequestMessage) SendRequestAsync(HttpRequestMessage) SendRequestAsync(HttpRequestMessage) SendRequestAsync(HttpRequestMessage)

Send an HTTP request as an asynchronous operation.

SendRequestAsync(HttpRequestMessage, HttpCompletionOption) SendRequestAsync(HttpRequestMessage, HttpCompletionOption) SendRequestAsync(HttpRequestMessage, HttpCompletionOption) SendRequestAsync(HttpRequestMessage, HttpCompletionOption) SendRequestAsync(HttpRequestMessage, HttpCompletionOption)

Send an HTTP request with an HTTP completion option as an asynchronous operation.

ToString() ToString() ToString() ToString() ToString()

Returns a string that represents the current HttpClient object.

See Also