Http​Client Class

Definition

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

public class HttpClient : System.Net.Http.HttpMessageInvoker
Inheritance

Inherited Members

Examples

 static async void Main()
{
 
    // Create a New HttpClient object.
    HttpClient client = new HttpClient();
  
    // Call asynchronous network methods in a try/catch block to handle exceptions
    try	
    {
       HttpResponseMessage response = await client.GetAsync("http://www.contoso.com/");
       response.EnsureSuccessStatusCode();
       string responseBody = await response.Content.ReadAsStringAsync();
       // Above three lines can be replaced with new helper method below
       // string responseBody = await client.GetStringAsync(uri);

       Console.WriteLine(responseBody);
    }  
    catch(HttpRequestException e)
    {
       Console.WriteLine("\nException Caught!");	
       Console.WriteLine("Message :{0} ",e.Message);
    }

    // Need to call dispose on the HttpClient object
    // when done using it, so the app doesn't leak resources
    client.Dispose(true);
 }

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:

  1. CancelPendingRequests
  2. DeleteAsync
  3. GetAsync
  4. GetByteArrayAsync
  5. GetStreamAsync
  6. GetStringAsync
  7. PostAsync
  8. PutAsync
  9. SendAsync

    HttpClient is intended to be instantiated once and re-used throughout the life of an application. Instantiating an HttpClient class for every request will exhaust the number of sockets available under heavy loads. This will result in SocketException errors. Below is an example using HttpClient correctly.

public class GoodController : ApiController  
{  
    // OK  
    private static readonly HttpClient HttpClient;  

    static GoodController()  
    {  
        HttpClient = new HttpClient();  
    }  
}  

Constructors

Http​Client()

Initializes a new instance of the HttpClient class.

Http​Client(​Http​Message​Handler)

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

Http​Client(​Http​Message​Handler, ​Boolean)

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

Properties

Base​Address

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

Default​Request​Headers

Gets the headers which should be sent with each request.

Max​Response​Content​Buffer​Size

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

Timeout

Gets or sets the timespan to wait before the request times out.

Methods

Cancel​Pending​Requests()

Cancel all pending requests on this instance.

Delete​Async(​String, ​Cancellation​Token)

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

Delete​Async(​Uri, ​Cancellation​Token)

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

Delete​Async(​String)

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

Delete​Async(​Uri)

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

Dispose(​Boolean)

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

Get​Async(​Uri, ​Http​Completion​Option, ​Cancellation​Token)

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

Get​Async(​String)

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

Get​Async(​Uri)

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

Get​Async(​String, ​Http​Completion​Option)

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

Get​Async(​String, ​Cancellation​Token)

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

Get​Async(​Uri, ​Http​Completion​Option)

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

Get​Async(​Uri, ​Cancellation​Token)

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

Get​Async(​String, ​Http​Completion​Option, ​Cancellation​Token)

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

Get​Byte​Array​Async(​Uri)

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

Get​Byte​Array​Async(​String)

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

Get​Stream​Async(​String)

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

Get​Stream​Async(​Uri)

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

Get​String​Async(​String)

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

Get​String​Async(​Uri)

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

Post​Async(​String, ​Http​Content, ​Cancellation​Token)

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

Post​Async(​Uri, ​Http​Content, ​Cancellation​Token)

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

Post​Async(​String, ​Http​Content)

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

Post​Async(​Uri, ​Http​Content)

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

Put​Async(​String, ​Http​Content)

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

Put​Async(​Uri, ​Http​Content)

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

Put​Async(​String, ​Http​Content, ​Cancellation​Token)

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

Put​Async(​Uri, ​Http​Content, ​Cancellation​Token)

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

Send​Async(​Http​Request​Message, ​Http​Completion​Option, ​Cancellation​Token)

Send an HTTP request as an asynchronous operation.

Send​Async(​Http​Request​Message)

Send an HTTP request as an asynchronous operation.

Send​Async(​Http​Request​Message, ​Http​Completion​Option)

Send an HTTP request as an asynchronous operation.

Send​Async(​Http​Request​Message, ​Cancellation​Token)

Send an HTTP request as an asynchronous operation.