HttpClient Class

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

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 : IDisposable

The HttpClient type exposes the following members.

Public methodHttpClient()Initializes a new instance of the HttpClient class.
Public methodHttpClient(HttpMessageHandler)Initializes a new instance of the HttpClient class with a specific handler.

Public propertyBaseAddressGets or sets the base address of Uniform Resource Identifier (URI) of the Internet resource used when sending requests.
Public propertyDefaultRequestHeadersGets the headers which should be sent with each request.
Public propertyMaxResponseContentBufferSizeGets or sets the maximum number of bytes to buffer when reading the response content.
Public propertyTimeoutGets or sets the number of milliseconds to wait before the request times out.

Public methodCancelPendingRequestsCancel all pending requests on this instance.
Public methodDeleteAsync(Void)
Public methodDeleteAsync(Void, HttpResponseMessage)
Public methodDispose()Releases the unmanaged resources and disposes of the managed resources used by the HttpClient.
Protected methodDispose(Boolean)Releases the unmanaged resources used by the HttpClient and optionally disposes of the managed resources.
Public methodEqualsDetermines 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 methodGetAsync(Void)
Public methodGetAsync(Void, HttpResponseMessage)
Public methodGetAsync(Void, HttpResponseMessage, String)
Public methodGetAsync(Void, HttpResponseMessage, Uri)
Public methodGetByteArrayAsync(Void)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetStreamAsync(Void)
Public methodGetStringAsync(Void)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodPostAsync(Void, HttpResponseMessage)
Public methodPostAsync(Void, HttpResponseMessage, String)
Public methodPostAsync(Void, HttpResponseMessage, Uri)
Public methodPutAsync(Void, HttpResponseMessage)
Public methodPutAsync(Void, HttpResponseMessage, String)
Public methodPutAsync(Void, HttpResponseMessage, Uri)
Public methodSendAsync(Void)
Public methodSendAsync(Void, HttpResponseMessage)
Public methodSendAsync(Void, HttpResponseMessage, HttpRequestMessage)
Public methodToStringReturns 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.

  static async void Main()
      // Create a New HttpClient object.
      HttpClient client = new HttpClient();

      HttpResponseMessage response = await client.GetAsync("");
      string responseBody = await response.Content.ReadAsStringAsync();
      // Above three lines can be replaced with new helper method in following line
      // string body = await client.GetStringAsync(uri);

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

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