HttpClient HttpClient HttpClient HttpClient Constructors

定义

重载

HttpClient() HttpClient() HttpClient()

使用释放此实例时释放的 HttpClientHandler 初始化 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)

使用指定的处理程序初始化 HttpClient 类的新实例。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)

使用提供的处理程序初始化 HttpClient 类的新实例,并指定在释放此实例时是否应释放该处理程序。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.

注解

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

使用释放此实例时释放的 HttpClientHandler 初始化 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 ()

注解

使用此构造函数是等效于调用 HttpClient(new HttpClientHandler(), true) 构造函数。Using this constructor is equivalent to calling the HttpClient(new HttpClientHandler(), true) constructor.

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

使用指定的处理程序初始化 HttpClient 类的新实例。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)

参数

handler
HttpMessageHandler HttpMessageHandler HttpMessageHandler HttpMessageHandler

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

异常

注解

使用此构造函数是等效于调用 HttpClient(handler, true) 构造函数。Using this constructor is equivalent to calling the HttpClient(handler, true) constructor.

指定handler通过调用将被释放HttpClient.DisposeThe specified handler 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 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)

参数

handler
HttpMessageHandler HttpMessageHandler HttpMessageHandler HttpMessageHandler

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

disposeHandler
Boolean Boolean Boolean Boolean

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

异常

适用于