Export (0) Print
Expand All

HttpClientHandler Class

.NET Framework 4.5

The default message handler used by HttpClient.

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

public class HttpClientHandler : HttpMessageHandler

The HttpClientHandler type exposes the following members.

  NameDescription
Public methodSupported in .NET for Windows Store appsHttpClientHandlerCreates an instance of a HttpClientHandler class.
Top

  NameDescription
Public propertySupported in .NET for Windows Store appsAllowAutoRedirectGets or sets a value that indicates whether the handler should follow redirection responses.
Public propertySupported in .NET for Windows Store appsAutomaticDecompressionGets or sets the type of decompression method used by the handler for automatic decompression of the HTTP content response.
Public propertySupported in .NET for Windows Store appsClientCertificateOptionsGets or sets the collection of security certificates that are associated with this handler.
Public propertySupported in .NET for Windows Store appsCookieContainerGets or sets the cookie container used to store server cookies by the handler.
Public propertySupported in .NET for Windows Store appsCredentialsGets or sets authentication information used by this handler.
Public propertySupported in .NET for Windows Store appsMaxAutomaticRedirectionsGets or sets the maximum number of redirects that the handler follows.
Public propertySupported in .NET for Windows Store appsMaxRequestContentBufferSizeGets or sets the maximum request content buffer size used by the handler.
Public propertySupported in .NET for Windows Store appsPreAuthenticateGets or sets a value that indicates whether the handler sends an Authorization header with the request.
Public propertySupported in .NET for Windows Store appsProxyGets or sets proxy information used by the handler.
Public propertySupported in .NET for Windows Store appsSupportsAutomaticDecompressionGets a value that indicates whether the handler supports automatic response content decompression.
Public propertySupported in .NET for Windows Store appsSupportsProxyGets a value that indicates whether the handler supports proxy settings.
Public propertySupported in .NET for Windows Store appsSupportsRedirectConfigurationGets a value that indicates whether the handler supports configuration settings for the AllowAutoRedirect and MaxAutomaticRedirections properties.
Public propertySupported in .NET for Windows Store appsUseCookiesGets or sets a value that indicates whether the handler uses the CookieContainer property to store server cookies and uses these cookies when sending requests.
Public propertySupported in .NET for Windows Store appsUseDefaultCredentialsGets or sets a value that controls whether default credentials are sent with requests by the handler.
Public propertySupported in .NET for Windows Store appsUseProxyGets or sets a value that indicates whether the handler uses a proxy for requests.
Top

  NameDescription
Public methodSupported in .NET for Windows Store appsDispose()Releases the unmanaged resources and disposes of the managed resources used by the HttpMessageHandler. (Inherited from HttpMessageHandler.)
Protected methodSupported in .NET for Windows Store appsDispose(Boolean)Releases the unmanaged resources used by the HttpClientHandler and optionally disposes of the managed resources. (Overrides HttpMessageHandler.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 appsGetHashCodeServes as the default hash function. (Inherited from Object.)
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.)
Protected methodSupported in .NET for Windows Store appsSendAsyncCreates an instance of HttpResponseMessage based on the information provided in the HttpRequestMessage as an operation that will not block. (Overrides HttpMessageHandler.SendAsync(HttpRequestMessage, CancellationToken).)
Public methodSupported in .NET for Windows Store appsToStringReturns a string that represents the current object. (Inherited from Object.)
Top

The HttpClientHandler class and classes derived from it enable developers to configure a variety of options ranging from proxies to authentication.

   static async void Main()
	 {
      // Create an HttpClientHandler object and set to use default credentials
      HttpClientHandler handler = new HttpClientHandler();
      handler.UseDefaultCredentials = true;

      // Create an HttpClient object
      HttpClient client = new HttpClient(handler);

      // 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();
         Console.WriteLine(responseBody);
       }  
       catch(HttpRequestException e)
       {
          Console.WriteLine("\nException Caught!");	
          Console.WriteLine("Message :{0} ",e.Message);
       }

       // Need to call dispose on the HttpClient and HttpClientHandler objects  
       // when done using them, so the app doesn't leak resources
       handler.Dispose(true);
       client.Dispose(true);
   }

.NET Framework

Supported in: 4.5

.NET for Windows Store apps

Supported in: Windows 8

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