HttpClient Constructors

Definition

Initializes a new instance of the HttpClient class.

Overloads

HttpClient()

Initializes a new instance of the HttpClient class using a HttpClientHandler that is disposed when this instance is disposed.

HttpClient(HttpMessageHandler)

Initializes a new instance of the HttpClient class with the specified handler. The handler is disposed when this instance is disposed.

HttpClient(HttpMessageHandler, Boolean)

Initializes a new instance of the HttpClient class with the provided handler, and specifies whether that handler should be disposed when this instance is disposed.

Remarks

HttpClient is intended to be instantiated once and re-used throughout the life of an application. Instantiating an HttpClient class for every request will exhaust the number of sockets available under heavy loads. This will result in SocketException errors. Below is an example using HttpClient correctly.

public class GoodController : ApiController
{
    private static readonly HttpClient HttpClient;

    static GoodController()
    {
        HttpClient = new HttpClient();
    }
}

HttpClient()

Source:
HttpClient.cs
Source:
HttpClient.cs
Source:
HttpClient.cs

Initializes a new instance of the HttpClient class using a HttpClientHandler that is disposed when this instance is disposed.

public HttpClient ();

Remarks

Using this constructor is equivalent to calling the HttpClient(new HttpClientHandler(), true) constructor.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

HttpClient(HttpMessageHandler)

Source:
HttpClient.cs
Source:
HttpClient.cs
Source:
HttpClient.cs

Initializes a new instance of the HttpClient class with the specified handler. The handler is disposed when this instance is disposed.

public HttpClient (System.Net.Http.HttpMessageHandler handler);

Parameters

handler
HttpMessageHandler

The HTTP handler stack to use for sending requests.

Exceptions

The handler is null.

Remarks

Using this constructor is equivalent to calling the HttpClient(handler, true) constructor.

The specified handler will be disposed of by calling HttpClient.Dispose.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

HttpClient(HttpMessageHandler, Boolean)

Source:
HttpClient.cs
Source:
HttpClient.cs
Source:
HttpClient.cs

Initializes a new instance of the HttpClient class with the provided handler, and specifies whether that handler should be disposed when this instance is disposed.

public HttpClient (System.Net.Http.HttpMessageHandler handler, bool disposeHandler);

Parameters

handler
HttpMessageHandler

The HttpMessageHandler responsible for processing the HTTP response messages.

disposeHandler
Boolean

true if the inner handler should be disposed of by HttpClient.Dispose; false if you intend to reuse the inner handler.

Exceptions

The handler is null.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0