HttpClient HttpClient HttpClient HttpClient Constructors

Definition

Overloads

HttpClient() HttpClient() HttpClient()

初始化 HttpClient 类的新实例。 Initializes a new instance of the HttpClient class.

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

用特定的处理程序初始化 HttpClient 类的新实例。 Initializes a new instance of the HttpClient class with a specific handler.

HttpClient(HttpMessageHandler, Boolean) HttpClient(HttpMessageHandler, Boolean) HttpClient(HttpMessageHandler, Boolean) HttpClient(HttpMessageHandler, Boolean)

用特定的处理程序初始化 HttpClient 类的新实例。 Initializes a new instance of the HttpClient class with a specific handler.

Remarks

HttpClient 旨在实例化一次和重复使用整个应用程序的生命周期。HttpClient is intended to be instantiated once and re-used throughout the life of an application. 实例化的每个请求的 HttpClient 类将用完的重负载下可用的插槽数。Instantiating an HttpClient class for every request will exhaust the number of sockets available under heavy loads. 这将导致 SocketException 错误。This will result in SocketException errors. 下面是正确使用 HttpClient 示例。Below is an example using HttpClient correctly.

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

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

HttpClient() HttpClient() HttpClient()

初始化 HttpClient 类的新实例。 Initializes a new instance of the HttpClient class.

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

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

用特定的处理程序初始化 HttpClient 类的新实例。 Initializes a new instance of the HttpClient class with a specific handler.

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)

参数

handler
HttpMessageHandler HttpMessageHandler HttpMessageHandler HttpMessageHandler

用于发送请求的使用的 HTTP 处理程序堆栈。 The HTTP handler stack to use for sending requests.

Exceptions

Remarks

handler 通过调用将被释放HttpClient.Disposehandler will be disposed of by calling HttpClient.Dispose.

HttpClient(HttpMessageHandler, Boolean) HttpClient(HttpMessageHandler, Boolean) HttpClient(HttpMessageHandler, Boolean) HttpClient(HttpMessageHandler, Boolean)

用特定的处理程序初始化 HttpClient 类的新实例。 Initializes a new instance of the HttpClient class with a specific handler.

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)

参数

handler
HttpMessageHandler HttpMessageHandler HttpMessageHandler HttpMessageHandler

HttpMessageHandler 负责处理 HTTP 响应消息。 The HttpMessageHandler responsible for processing the HTTP response messages.

disposeHandler
Boolean Boolean Boolean Boolean

如果内部处理程序应由 HttpClient.Dispose 处置,则为 true,如果希望重新使用内部处理程序,则为 false true if the inner handler should be disposed of by HttpClient.Dispose, false if you intend to reuse the inner handler.

Exceptions

Applies to