HttpClient 建構函式

定義

初始化 HttpClient 類別的新執行個體。Initializes a new instance of the HttpClient class.

多載

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 類別的新執行個體。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 類別的新執行個體,並指定當處置此執行個體時是否應該處置該處理常式。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  
{  
    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()

使用當處置此執行個體時會處置的 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 類別的新執行個體。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

要用來傳送要求的 HTTP 處理常式堆疊。The HTTP handler stack to use for sending requests.

例外狀況

handlernullThe handler is null.

備註

使用這個函式相當於呼叫函式 HttpClient(handler, true)Using this constructor is equivalent to calling the HttpClient(handler, true) constructor.

handler 由呼叫 HttpClient處置指定的。The specified handler will be disposed of by calling HttpClient.Dispose.

適用於

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

負責處理 HTTP 回應訊息的 HttpMessageHandlerThe HttpMessageHandler responsible for processing the HTTP response messages.

disposeHandler
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.

例外狀況

handlernullThe handler is null.

適用於