HttpClient Class

HttpClient Class

.NET Framework (current version)
 

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

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


Public Class HttpClient
	Inherits HttpMessageInvoker

NameDescription
System_CAPS_pubmethodHttpClient()

Initializes a new instance of the HttpClient class.

System_CAPS_pubmethodHttpClient(HttpMessageHandler)

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

System_CAPS_pubmethodHttpClient(HttpMessageHandler, Boolean)

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

NameDescription
System_CAPS_pubpropertyBaseAddress

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

System_CAPS_pubpropertyDefaultRequestHeaders

Gets the headers which should be sent with each request.

System_CAPS_pubpropertyMaxResponseContentBufferSize

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

System_CAPS_pubpropertyTimeout

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

NameDescription
System_CAPS_pubmethodCancelPendingRequests()

Cancel all pending requests on this instance.

System_CAPS_pubmethodDeleteAsync(String)

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

System_CAPS_pubmethodDeleteAsync(String, CancellationToken)

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

System_CAPS_pubmethodDeleteAsync(Uri)

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

System_CAPS_pubmethodDeleteAsync(Uri, CancellationToken)

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

System_CAPS_pubmethodDispose()

Releases the unmanaged resources and disposes of the managed resources used by the HttpMessageInvoker.(Inherited from HttpMessageInvoker.)

System_CAPS_protmethodDispose(Boolean)

Releases the unmanaged resources used by the HttpClient and optionally disposes of the managed resources.(Overrides HttpMessageInvoker.Dispose(Boolean).)

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_protmethodFinalize()

Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)

System_CAPS_pubmethodGetAsync(String)

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

System_CAPS_pubmethodGetAsync(String, CancellationToken)

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

System_CAPS_pubmethodGetAsync(String, HttpCompletionOption)

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

System_CAPS_pubmethodGetAsync(String, HttpCompletionOption, CancellationToken)

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

System_CAPS_pubmethodGetAsync(Uri)

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

System_CAPS_pubmethodGetAsync(Uri, CancellationToken)

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

System_CAPS_pubmethodGetAsync(Uri, HttpCompletionOption)

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

System_CAPS_pubmethodGetAsync(Uri, HttpCompletionOption, CancellationToken)

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

System_CAPS_pubmethodGetByteArrayAsync(String)

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

System_CAPS_pubmethodGetByteArrayAsync(Uri)

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

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetStreamAsync(String)

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

System_CAPS_pubmethodGetStreamAsync(Uri)

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

System_CAPS_pubmethodGetStringAsync(String)

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

System_CAPS_pubmethodGetStringAsync(Uri)

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

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_protmethodMemberwiseClone()

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_pubmethodPostAsync(String, HttpContent)

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

System_CAPS_pubmethodPostAsync(String, HttpContent, CancellationToken)

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

System_CAPS_pubmethodPostAsync(Uri, HttpContent)

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

System_CAPS_pubmethodPostAsync(Uri, HttpContent, CancellationToken)

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

System_CAPS_pubmethodPutAsync(String, HttpContent)

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

System_CAPS_pubmethodPutAsync(String, HttpContent, CancellationToken)

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

System_CAPS_pubmethodPutAsync(Uri, HttpContent)

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

System_CAPS_pubmethodPutAsync(Uri, HttpContent, CancellationToken)

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

System_CAPS_pubmethodSendAsync(HttpRequestMessage)

Send an HTTP request as an asynchronous operation.

System_CAPS_pubmethodSendAsync(HttpRequestMessage, CancellationToken)

Send an HTTP request as an asynchronous operation.(Overrides HttpMessageInvoker.SendAsync(HttpRequestMessage, CancellationToken).)

System_CAPS_pubmethodSendAsync(HttpRequestMessage, HttpCompletionOption)

Send an HTTP request as an asynchronous operation.

System_CAPS_pubmethodSendAsync(HttpRequestMessage, HttpCompletionOption, CancellationToken)

Send an HTTP request as an asynchronous operation.

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

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

 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);
 }

Universal Windows Platform
Available since 4.5
.NET Framework
Available since 4.5
Portable Class Library
Supported in: portable .NET platforms
Windows Phone
Available since 8.1

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
Show:
© 2016 Microsoft