Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
HttpClientHandler Class

HttpClientHandler Class

.NET Framework 4.6 and 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.

Public methodHttpClientHandlerCreates an instance of a HttpClientHandler class.

Public propertyAllowAutoRedirectGets or sets a value that indicates whether the handler should follow redirection responses.
Public propertyAutomaticDecompressionGets or sets the type of decompression method used by the handler for automatic decompression of the HTTP content response.
Public propertyClientCertificateOptionsGets or sets a value that indicates if the certificate is automatically picked from the certificate store or if the caller is allowed to pass in a specific client certificate.
Public propertyCookieContainerGets or sets the cookie container used to store server cookies by the handler.
Public propertyCredentialsGets or sets authentication information used by this handler.
Public propertyMaxAutomaticRedirectionsGets or sets the maximum number of redirects that the handler follows.
Public propertyMaxRequestContentBufferSizeGets or sets the maximum request content buffer size used by the handler.
Public propertyPreAuthenticateGets or sets a value that indicates whether the handler sends an Authorization header with the request.
Public propertyProxyGets or sets proxy information used by the handler.
Public propertySupportsAutomaticDecompressionGets a value that indicates whether the handler supports automatic response content decompression.
Public propertySupportsProxyGets a value that indicates whether the handler supports proxy settings.
Public propertySupportsRedirectConfigurationGets a value that indicates whether the handler supports configuration settings for the AllowAutoRedirect and MaxAutomaticRedirections properties.
Public propertyUseCookiesGets 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 propertyUseDefaultCredentialsGets or sets a value that controls whether default credentials are sent with requests by the handler.
Public propertyUseProxyGets or sets a value that indicates whether the handler uses a proxy for requests.

Public methodDispose()Releases the unmanaged resources and disposes of the managed resources used by the HttpMessageHandler. (Inherited from HttpMessageHandler.)
Protected methodDispose(Boolean)Releases the unmanaged resources used by the HttpClientHandler and optionally disposes of the managed resources. (Overrides HttpMessageHandler.Dispose(Boolean).)
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Protected methodSendAsyncCreates 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 methodToStringReturns a string that represents the current object. (Inherited from Object.)

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 
         HttpResponseMessage response = await client.GetAsync("http://www.contoso.com/");


         string responseBody = await response.Content.ReadAsStringAsync();
       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

.NET Framework

Supported in: 4.6, 4.5

.NET for Windows Phone apps

Supported in: Windows Phone 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.
© 2015 Microsoft