HttpClient Constructor

.NET Framework (current version)
 

Initializes a new instance of the HttpClient class.

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

NameDescription
System_CAPS_pubmethodHttpClient()

Initializes a new instance of the HttpClient class.

System_CAPS_pubmethodHttpClient(HttpMessageHandler)

Initializes a new instance of the HttpClient class with a specific handler.

System_CAPS_pubmethodHttpClient(HttpMessageHandler, Boolean)

Initializes a new instance of the HttpClient class with a specific handler.

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
{
    // OK
    private static readonly HttpClient HttpClient;

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

Return to top
Show: