Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

HttpClient Class

.NET Framework 4.5

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)

'Declaration
Public Class HttpClient _
	Inherits HttpMessageInvoker

The HttpClient type exposes the following members.

  NameDescription
Public methodSupported in .NET for Windows Store appsHttpClientInitializes a new instance of the HttpClient class.
Public methodSupported in .NET for Windows Store appsHttpClient(HttpMessageHandler)Initializes a new instance of the HttpClient class with a specific handler.
Public methodSupported in .NET for Windows Store appsHttpClient(HttpMessageHandler, Boolean)Initializes a new instance of the HttpClient class with a specific handler.
Top

  NameDescription
Public propertySupported in .NET for Windows Store appsBaseAddressGets or sets the base address of Uniform Resource Identifier (URI) of the Internet resource used when sending requests.
Public propertySupported in .NET for Windows Store appsDefaultRequestHeadersGets the headers which should be sent with each request.
Public propertySupported in .NET for Windows Store appsMaxResponseContentBufferSizeGets or sets the maximum number of bytes to buffer when reading the response content.
Public propertySupported in .NET for Windows Store appsTimeoutGets or sets the number of milliseconds to wait before the request times out.
Top

  NameDescription
Public methodSupported in .NET for Windows Store appsCancelPendingRequestsCancel all pending requests on this instance.
Public methodSupported in .NET for Windows Store appsDeleteAsync(String)Send a DELETE request to the specified Uri as an asynchronous operation.
Public methodSupported in .NET for Windows Store appsDeleteAsync(Uri)Send a DELETE request to the specified Uri as an asynchronous operation.
Public methodSupported in .NET for Windows Store appsDeleteAsync(String, CancellationToken)Send a DELETE request to the specified Uri with a cancellation token as an asynchronous operation.
Public methodSupported in .NET for Windows Store appsDeleteAsync(Uri, CancellationToken)Send a DELETE request to the specified Uri with a cancellation token as an asynchronous operation.
Public methodSupported in .NET for Windows Store appsDisposeReleases the unmanaged resources and disposes of the managed resources used by the HttpMessageInvoker. (Inherited from HttpMessageInvoker.)
Protected methodSupported in .NET for Windows Store appsDispose(Boolean)Releases the unmanaged resources used by the HttpClient and optionally disposes of the managed resources. (Overrides HttpMessageInvoker.Dispose(Boolean).)
Public methodSupported in .NET for Windows Store appsEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodSupported in .NET for Windows Store appsFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported in .NET for Windows Store appsGetAsync(String)Send a GET request to the specified Uri as an asynchronous operation.
Public methodSupported in .NET for Windows Store appsGetAsync(Uri)Send a GET request to the specified Uri as an asynchronous operation.
Public methodSupported in .NET for Windows Store appsGetAsync(String, HttpCompletionOption)Send a GET request to the specified Uri with an HTTP completion option as an asynchronous operation.
Public methodSupported in .NET for Windows Store appsGetAsync(String, CancellationToken)Send a GET request to the specified Uri with a cancellation token as an asynchronous operation.
Public methodSupported in .NET for Windows Store appsGetAsync(Uri, HttpCompletionOption)Send a GET request to the specified Uri with an HTTP completion option as an asynchronous operation.
Public methodSupported in .NET for Windows Store appsGetAsync(Uri, CancellationToken)Send a GET request to the specified Uri with a cancellation token as an asynchronous operation.
Public methodSupported in .NET for Windows Store appsGetAsync(String, HttpCompletionOption, CancellationToken)Send a GET request to the specified Uri with an HTTP completion option and a cancellation token as an asynchronous operation.
Public methodSupported in .NET for Windows Store appsGetAsync(Uri, HttpCompletionOption, CancellationToken)Send a GET request to the specified Uri with an HTTP completion option and a cancellation token as an asynchronous operation.
Public methodSupported in .NET for Windows Store appsGetByteArrayAsync(String)Send a GET request to the specified Uri and return the response body as a byte array in an asynchronous operation.
Public methodSupported in .NET for Windows Store appsGetByteArrayAsync(Uri)Send a GET request to the specified Uri and return the response body as a byte array in an asynchronous operation.
Public methodSupported in .NET for Windows Store appsGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodSupported in .NET for Windows Store appsGetStreamAsync(String)Send a GET request to the specified Uri and return the response body as a stream in an asynchronous operation.
Public methodSupported in .NET for Windows Store appsGetStreamAsync(Uri)Send a GET request to the specified Uri and return the response body as a stream in an asynchronous operation.
Public methodSupported in .NET for Windows Store appsGetStringAsync(String)Send a GET request to the specified Uri and return the response body as a string in an asynchronous operation.
Public methodSupported in .NET for Windows Store appsGetStringAsync(Uri)Send a GET request to the specified Uri and return the response body as a string in an asynchronous operation.
Public methodSupported in .NET for Windows Store appsGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodSupported in .NET for Windows Store appsMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported in .NET for Windows Store appsPostAsync(String, HttpContent)Send a POST request to the specified Uri as an asynchronous operation.
Public methodSupported in .NET for Windows Store appsPostAsync(Uri, HttpContent)Send a POST request to the specified Uri as an asynchronous operation.
Public methodSupported in .NET for Windows Store appsPostAsync(String, HttpContent, CancellationToken)Send a POST request with a cancellation token as an asynchronous operation.
Public methodSupported in .NET for Windows Store appsPostAsync(Uri, HttpContent, CancellationToken)Send a POST request with a cancellation token as an asynchronous operation.
Public methodSupported in .NET for Windows Store appsPutAsync(String, HttpContent)Send a PUT request to the specified Uri as an asynchronous operation.
Public methodSupported in .NET for Windows Store appsPutAsync(Uri, HttpContent)Send a PUT request to the specified Uri as an asynchronous operation.
Public methodSupported in .NET for Windows Store appsPutAsync(String, HttpContent, CancellationToken)Send a PUT request with a cancellation token as an asynchronous operation.
Public methodSupported in .NET for Windows Store appsPutAsync(Uri, HttpContent, CancellationToken)Send a PUT request with a cancellation token as an asynchronous operation.
Public methodSupported in .NET for Windows Store appsSendAsync(HttpRequestMessage)Send an HTTP request as an asynchronous operation.
Public methodSupported in .NET for Windows Store appsSendAsync(HttpRequestMessage, HttpCompletionOption)Send an HTTP request as an asynchronous operation.
Public methodSupported in .NET for Windows Store appsSendAsync(HttpRequestMessage, CancellationToken)Send an HTTP request as an asynchronous operation. (Overrides HttpMessageInvoker.SendAsync(HttpRequestMessage, CancellationToken).)
Public methodSupported in .NET for Windows Store appsSendAsync(HttpRequestMessage, HttpCompletionOption, CancellationToken)Send an HTTP request as an asynchronous operation.
Public methodSupported in .NET for Windows Store appsToStringReturns a string that represents the current object. (Inherited from Object.)
Top

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

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1

Windows Phone 8.1, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

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