HttpClient Class (System.Net.Http)

Switch View :
ScriptFree
.NET Framework Class Library
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.

Inheritance Hierarchy

System.Object
  System.Net.Http.HttpClient

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

Visual Basic
Public Class HttpClient _
	Implements IDisposable
C#
public class HttpClient : IDisposable
Visual C++
public ref class HttpClient : IDisposable
F#
type HttpClient =  
    class
        interface IDisposable
    end

The HttpClient type exposes the following members.

Constructors

  Name Description
Public method HttpClient Initializes a new instance of the HttpClient class.
Public method HttpClient(HttpMessageHandler) Initializes a new instance of the HttpClient class with a specific handler.
Top
Properties

  Name Description
Public property BaseAddress Gets or sets the base address of Uniform Resource Identifier (URI) of the Internet resource used when sending requests.
Public property DefaultRequestHeaders Gets the headers which should be sent with each request.
Public property MaxResponseContentBufferSize Gets or sets the maximum number of bytes to buffer when reading the response content.
Public property Timeout Gets or sets the number of milliseconds to wait before the request times out.
Top
Methods

  Name Description
Public method CancelPendingRequests Cancel all pending requests on this instance.
Public method Delete(String) Send a DELETE request to the specified Uri.
Public method Delete(Uri) Send a DELETE request to the specified Uri.
Public method DeleteAsync(String) Send a DELETE request to the specified Uri as an asynchronous operation.
Public method DeleteAsync(Uri) Send a DELETE request to the specified Uri as an asynchronous operation.
Public method Dispose Releases the unmanaged resources and disposes of the managed resources used by the HttpClient.
Protected method Dispose(Boolean) Releases the unmanaged resources used by the HttpClient and optionally disposes of the managed resources.
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method Get(String) Send a GET request to the specified Uri.
Public method Get(Uri) Send a GET request to the specified Uri.
Public method GetAsync(String) Send a GET request to the specified Uri as an asynchronous operation.
Public method GetAsync(Uri) Send a GET request to the specified Uri as an asynchronous operation.
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method Post(String, HttpContent) Send a POST request to the specified Uri.
Public method Post(Uri, HttpContent) Send a POST request to the specified Uri.
Public method PostAsync(String, HttpContent) Send a POST request to the specified Uri as an asynchronous operation.
Public method PostAsync(Uri, HttpContent) Send a POST request to the specified Uri as an asynchronous operation.
Public method Put(String, HttpContent) Send a PUT request to the specified Uri.
Public method Put(Uri, HttpContent) Send a PUT request to the specified Uri.
Public method PutAsync(String, HttpContent) Send a PUT request to the specified Uri as an asynchronous operation.
Public method PutAsync(Uri, HttpContent) Send a PUT request to the specified Uri as an asynchronous operation.
Public method Send(HttpRequestMessage) Send an HTTP request synchronously.
Public method Send(HttpRequestMessage, HttpCompletionOption) Send an HTTP request synchronously.
Public method Send(HttpRequestMessage, CancellationToken) Send an HTTP request synchronously.
Public method Send(HttpRequestMessage, HttpCompletionOption, CancellationToken) Send an HTTP request synchronously.
Public method SendAsync(HttpRequestMessage) Send an HTTP request as an asynchronous operation.
Public method SendAsync(HttpRequestMessage, HttpCompletionOption) Send an HTTP request as an asynchronous operation.
Public method SendAsync(HttpRequestMessage, CancellationToken) Send an HTTP request as an asynchronous operation.
Public method SendAsync(HttpRequestMessage, HttpCompletionOption, CancellationToken) Send an HTTP request as an asynchronous operation.
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
Top
Remarks

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.

Examples

C#

  static async void Main()
	{
    try	
    {
      // Create a New HttpClient object.
      HttpClient client = new HttpClient();
      HttpResponseMessage response = await client.GetAsync("http://www.contoso.com/");

      response.EnsureSuccessStatusCode();

      string responseBody = response.Content.ReadAsString();
      Console.WriteLine(responseBody);
    }  
    catch(HttpRequestException e)
    {
      Console.WriteLine("\nException Caught!");	
      Console.WriteLine("Message :{0} ",e.Message);
    }
  }


Version Information

.NET Framework

Supported in: 4.5
Platforms

Windows Developer Preview, Windows Server Developer Preview, Windows 7, 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.
Thread Safety

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

Reference