HttpClient HttpClient HttpClient HttpClient Constructors

Definition

Overloads

HttpClient() HttpClient() HttpClient()

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

HttpClient(HttpMessageHandler) HttpClient(HttpMessageHandler) HttpClient(HttpMessageHandler) 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) HttpClient(HttpMessageHandler, Boolean) HttpClient(HttpMessageHandler, Boolean) 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();  
    }  
}  
  Public Class GoodController
    Inherits ApiController  

    Private Shared ReadOnly HttpClient As HttpClient

    Shared Sub New()  
        HttpClient = New HttpClient()
    End Sub
End Class

HttpClient() HttpClient() HttpClient()

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

public:
 HttpClient();
public HttpClient ();
Public Sub New ()

Remarks

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

HttpClient(HttpMessageHandler) HttpClient(HttpMessageHandler) HttpClient(HttpMessageHandler) HttpClient(HttpMessageHandler)

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);
public HttpClient (System.Net.Http.HttpMessageHandler handler);
new System.Net.Http.HttpClient : System.Net.Http.HttpMessageHandler -> System.Net.Http.HttpClient
Public Sub New (handler As HttpMessageHandler)

Parameters

handler
HttpMessageHandler HttpMessageHandler HttpMessageHandler HttpMessageHandler

The HTTP handler stack to use for sending requests.

Exceptions

Remarks

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

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

HttpClient(HttpMessageHandler, Boolean) HttpClient(HttpMessageHandler, Boolean) HttpClient(HttpMessageHandler, Boolean) 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.

public:
 HttpClient(System::Net::Http::HttpMessageHandler ^ handler, bool disposeHandler);
public HttpClient (System.Net.Http.HttpMessageHandler handler, bool disposeHandler);
new System.Net.Http.HttpClient : System.Net.Http.HttpMessageHandler * bool -> System.Net.Http.HttpClient
Public Sub New (handler As HttpMessageHandler, disposeHandler As Boolean)

Parameters

handler
HttpMessageHandler HttpMessageHandler HttpMessageHandler HttpMessageHandler

The HttpMessageHandler responsible for processing the HTTP response messages.

disposeHandler
Boolean Boolean Boolean Boolean

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

Exceptions

Applies to