संपादित करें

इसके माध्यम से साझा किया गया


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

    Private Shared ReadOnly HttpClient As HttpClient

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

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();
public HttpClient ();
Public Sub New ()

Remarks

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

Applies to

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);
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

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

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);
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

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