System.Net.Http.HttpClient Class

Provides a base class for sending HTTP requests and receiving HTTP responses from a resource identified by a URI.

See Also: HttpClient

Syntax

public class HttpClient : HttpMessageInvoker

Remarks

The HttpClient class instance acts as a session to send HTTP requests. An HttpClient instance is a collection of settings applied 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 base class for more specific HTTP clients. An example would be a FacebookHttpClient providing additional methods specific to a Facebook web service (a GetFriends method, for instance).

By default, HttpWebRequest will be used to send requests to the server. This behavior can be modified by specifying a different channel in one of the constructor overloads taking a HttpMessageHandler instance as parameter. If features like authentication or caching are required, WebRequestHandler can be used to configure settings and the instance can be passed to the constructor. The returned handler can be passed to one of the constructor overloads taking a HttpMessageHandler parameter.

If an app using HttpClient and related classes in the System.Net.Http namespace intends to download 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 substantially reduced performance.

The following methods are thread safe:

[The 'ordered' type of list has not been implemented in the ECMA stylesheet.]

Requirements

Namespace: System.Net.Http
Assembly: System.Net.Http (in System.Net.Http.dll)
Assembly Versions: 4.0.0.0

The members of System.Net.Http.HttpClient are listed below.

See Also: HttpMessageInvoker

Public Constructors

Initializes a new instance of the HttpClient class.

Initializes a new instance of the HttpClient class with a specific handler.

Initializes a new instance of the HttpClient class with a specific handler.

Public Properties

BaseAddressUri.

Gets or sets the base address of Uniform Resource Identifier (URI) of the Internet resource used when sending requests.

[read-only]
DefaultRequestHeadersHttpRequestHeaders.

Gets the headers which should be sent with each request.

MaxResponseContentBufferSizeInt64.

Gets or sets the maximum number of bytes to buffer when reading the response content.

TimeoutTimeSpan.

Gets or sets the number of milliseconds to wait before the request times out.

Public Methods

CancelPendingRequests()

Cancel all pending requests on this instance.

DeleteAsync(String) : System.Threading.Tasks.Task<HttpResponseMessage>

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

DeleteAsync(Uri) : System.Threading.Tasks.Task<HttpResponseMessage>

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

DeleteAsync(String, CancellationToken) : System.Threading.Tasks.Task<HttpResponseMessage>

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

DeleteAsync(Uri, CancellationToken) : System.Threading.Tasks.Task<HttpResponseMessage>

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

GetAsync(String) : System.Threading.Tasks.Task<HttpResponseMessage>

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

GetAsync(Uri) : System.Threading.Tasks.Task<HttpResponseMessage>

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

GetAsync(String, HttpCompletionOption) : System.Threading.Tasks.Task<HttpResponseMessage>

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

GetAsync(String, CancellationToken) : System.Threading.Tasks.Task<HttpResponseMessage>

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

GetAsync(Uri, HttpCompletionOption) : System.Threading.Tasks.Task<HttpResponseMessage>

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

GetAsync(Uri, CancellationToken) : System.Threading.Tasks.Task<HttpResponseMessage>

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

GetAsync(String, HttpCompletionOption, CancellationToken) : System.Threading.Tasks.Task<HttpResponseMessage>

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

GetAsync(Uri, HttpCompletionOption, CancellationToken) : System.Threading.Tasks.Task<HttpResponseMessage>

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

GetByteArrayAsync(String) : System.Threading.Tasks.Task<Byte[]>

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

GetByteArrayAsync(Uri) : System.Threading.Tasks.Task<Byte[]>

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

GetStreamAsync(String) : System.Threading.Tasks.Task<System.IO.Stream>

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

GetStreamAsync(Uri) : System.Threading.Tasks.Task<System.IO.Stream>

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

GetStringAsync(String) : System.Threading.Tasks.Task<string>

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

GetStringAsync(Uri) : System.Threading.Tasks.Task<string>

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

PostAsync(String, HttpContent) : System.Threading.Tasks.Task<HttpResponseMessage>

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

PostAsync(Uri, HttpContent) : System.Threading.Tasks.Task<HttpResponseMessage>

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

PostAsync(String, HttpContent, CancellationToken) : System.Threading.Tasks.Task<HttpResponseMessage>

Send a POST request with a cancellation token as an asynchronous operation.

PostAsync(Uri, HttpContent, CancellationToken) : System.Threading.Tasks.Task<HttpResponseMessage>

Send a POST request with a cancellation token as an asynchronous operation.

PutAsync(String, HttpContent) : System.Threading.Tasks.Task<HttpResponseMessage>

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

PutAsync(Uri, HttpContent) : System.Threading.Tasks.Task<HttpResponseMessage>

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

PutAsync(String, HttpContent, CancellationToken) : System.Threading.Tasks.Task<HttpResponseMessage>

Send a PUT request with a cancellation token as an asynchronous operation.

PutAsync(Uri, HttpContent, CancellationToken) : System.Threading.Tasks.Task<HttpResponseMessage>

Send a PUT request with a cancellation token as an asynchronous operation.

SendAsync(HttpRequestMessage) : System.Threading.Tasks.Task<HttpResponseMessage>

Send an HTTP request as an asynchronous operation.

SendAsync(HttpRequestMessage, HttpCompletionOption) : System.Threading.Tasks.Task<HttpResponseMessage>

Send an HTTP request as an asynchronous operation.

override
SendAsync(HttpRequestMessage, CancellationToken) : System.Threading.Tasks.Task<HttpResponseMessage>

Send an HTTP request as an asynchronous operation.

SendAsync(HttpRequestMessage, HttpCompletionOption, CancellationToken) : System.Threading.Tasks.Task<HttpResponseMessage>

Send an HTTP request as an asynchronous operation.

Protected Methods

override
Dispose(Boolean)

Releases the unmanaged resources used by the HttpClient and optionally disposes of the managed resources.