HttpClientHandler 類別

定義

.NET Framework 和 .NET Core 2.0 及更舊版本中 HttpClient 使用的預設訊息處理常式。The default message handler used by HttpClient in .NET Framework and .NET Core 2.0 and earlier.

public ref class HttpClientHandler : System::Net::Http::HttpMessageHandler
public class HttpClientHandler : System.Net.Http.HttpMessageHandler
type HttpClientHandler = class
    inherit HttpMessageHandler
Public Class HttpClientHandler
Inherits HttpMessageHandler
繼承
HttpClientHandler
衍生

範例

static async Task Main()
{
   // Create an HttpClientHandler object and set to use default credentials
   HttpClientHandler handler = new HttpClientHandler();
   handler.UseDefaultCredentials = true;

   // Create an HttpClient object
   HttpClient client = new HttpClient(handler);

   // Call asynchronous network methods in a try/catch block to handle exceptions
   try	
   {
      HttpResponseMessage response = await client.GetAsync("http://www.contoso.com/");

      response.EnsureSuccessStatusCode();

      string responseBody = await response.Content.ReadAsStringAsync();
      Console.WriteLine(responseBody);
   }  
   catch(HttpRequestException e)
   {
       Console.WriteLine("\nException Caught!");	
       Console.WriteLine("Message :{0} ",e.Message);
   }

   // Need to call dispose on the HttpClient and HttpClientHandler objects 
   // when done using them, so the app doesn't leak resources
   handler.Dispose(true);
   client.Dispose(true);
}

上述程式async Task Main()代碼範例會使用進入點。The preceding code example uses an async Task Main() entry point. 該功能需要C# 7.1 或更新版本。That feature requires C# 7.1 or later.

備註

衍生HttpClientHandler自它的類別和類別可讓開發人員設定各種不同的選項, 範圍從 proxy 到驗證。The HttpClientHandler class and classes derived from it enable developers to configure a variety of options ranging from proxies to authentication.

.NET Core 中的 HttpClientHandlerHttpClientHandler in .NET Core

從 .net Core 2.1 開始, System.Net.Http.SocketsHttpHandler類別HttpClientHandler (而不是) 會提供較高層級 HTTP 網路 api 所使用的執行。Starting with .NET Core 2.1, the System.Net.Http.SocketsHttpHandler class instead of HttpClientHandler provides the implementation used by higher-level HTTP networking APIs. 您可以將應用程式設定為HttpClientHandler使用, 而不是下列任何一種方式:You can configure your application to use HttpClientHandler instead in any of the following ways:

  • 藉由呼叫AppContext.SetSwitch方法, 如下所示:By calling the AppContext.SetSwitch method as follows:

    AppContext.SetSwitch("System.Net.Http.UseSocketsHttpHandler", false);
    
    AppContext.SetSwitch("System.Net.Http.UseSocketsHttpHandler", False)
    
  • 藉由在System.Net.Http.UseSocketsHttpHandler netcore. .runtimeconfig.json. json設定檔中定義參數:By defining the System.Net.Http.UseSocketsHttpHandler switch in the .netcore.runtimeconfig.json configuration file:

    "runtimeOptions": {
      "configProperties": {
          "System.Net.Http.UseSocketsHttpHandler": false
      }
    }
    
  • 定義名為DOTNET_SYSTEM_NET_HTTP_USESOCKETSHTTPHANDLER的環境變數, 並將它設定false為或0。By defining an environment variable named DOTNET_SYSTEM_NET_HTTP_USESOCKETSHTTPHANDLER and setting it to either false or 0.

建構函式

HttpClientHandler()

建立 HttpClientHandler 類別的執行個體。Creates an instance of a HttpClientHandler class.

屬性

AllowAutoRedirect

取得或設定值,這個值指出此處理常式是否應該接在重新導向回應之後。Gets or sets a value that indicates whether the handler should follow redirection responses.

AutomaticDecompression

取得或設定解壓縮方法的類型,此為 HTTP 內容回應的自動解壓縮處理常式所使用。Gets or sets the type of decompression method used by the handler for automatic decompression of the HTTP content response.

CheckCertificateRevocationList

取得或設定值,指出是否根據憑證授權撤銷清單來檢查憑證。Gets or sets a value that indicates whether the certificate is checked against the certificate authority revocation list.

ClientCertificateOptions

取得或設定值,表示憑證是否會從憑證存放區自動選取,或呼叫端是否允許傳入特定用戶端憑證。Gets or sets a value that indicates if the certificate is automatically picked from the certificate store or if the caller is allowed to pass in a specific client certificate.

ClientCertificates

取得為伺服器相關聯要求的安全憑證集合。Gets the collection of security certificates that are associated requests to the server.

CookieContainer

取得或設定處理常式用來儲存伺服器 cookie 的 cookie 容器。Gets or sets the cookie container used to store server cookies by the handler.

Credentials

取得或設定這個處理常式所用的驗證資訊。Gets or sets authentication information used by this handler.

DangerousAcceptAnyServerCertificateValidator

取得一律傳回 true 的快取委派。Gets a cached delegate that always returns true.

DefaultProxyCredentials

使用預設 (系統) Proxy 時,取得或設定要提交至預設 Proxy 伺服器進行驗證的認證。When the default (system) proxy is being used, gets or sets the credentials to submit to the default proxy server for authentication. 只有當 UseProxy 設定為 trueProxy 設定為 null 時,才使用預設 Proxy。The default proxy is used only when UseProxy is set to true and Proxy is set to null.

MaxAutomaticRedirections

取得或設定處理常式追蹤的最大重新導向數目。Gets or sets the maximum number of redirects that the handler follows.

MaxConnectionsPerServer

取得或設定使用 HttpClient 物件提出要求時,所允許的同時連線 數目上限 (每個伺服器端點)。Gets or sets the maximum number of concurrent connections (per server endpoint) allowed when making requests using an HttpClient object. 請注意,此限制是按照每個伺服器端點計算,例如值 256 允許 http://www.adatum.com/ 使用 256 個同時連線,而 http://www.adventure-works.com/ 另有 256 個同時連線。Note that the limit is per server endpoint, so for example a value of 256 would permit 256 concurrent connections to http://www.adatum.com/ and another 256 to http://www.adventure-works.com/.

MaxRequestContentBufferSize

取得或設定此處理常式所使用最大要求內容的緩衝區大小。Gets or sets the maximum request content buffer size used by the handler.

MaxResponseHeadersLength

取得或設定回應標頭的最大長度,以 KB (1024 位元組) 為單位。Gets or sets the maximum length, in kilobytes (1024 bytes), of the response headers. 比方說,如果值為 64,則最大回應標頭長度允許有 65536 個位元組。For example, if the value is 64, then 65536 bytes are allowed for the maximum response headers' length.

PreAuthenticate

取得或設定值,這個值指出處理常式是否隨著要求傳送授權標頭。Gets or sets a value that indicates whether the handler sends an Authorization header with the request.

Properties

取得 HttpClient 要求之自訂屬性的可寫入字典 (也就是對應)。Gets a writable dictionary (that is, a map) of custom properties for the HttpClient requests. 字典會初始化為空字典。您可以插入及查詢自訂處理常式和特殊處理的索引鍵/值組。The dictionary is initialized empty; you can insert and query key-value pairs for your custom handlers and special processing.

Proxy

取得或設定處理常式所使用的 Proxy 資訊。Gets or sets proxy information used by the handler.

ServerCertificateCustomValidationCallback

取得或設定驗證伺服器憑證的回呼方法。Gets or sets a callback method to validate the server certificate.

SslProtocols

取得或設定 TLS/SSL 通訊協定,其是由 HttpClientHandler 物件管理之 HttpClient 物件所使用。Gets or sets the TLS/SSL protocol used by the HttpClient objects managed by the HttpClientHandler object.

SupportsAutomaticDecompression

取得值,表示處理常式是否支援自動回應內容解壓縮。Gets a value that indicates whether the handler supports automatic response content decompression.

SupportsProxy

取得值,這個值指出處理常式是否支援 Proxy 設定。Gets a value that indicates whether the handler supports proxy settings.

SupportsRedirectConfiguration

取得值,這個值指出處理常式是否支援 AllowAutoRedirectMaxAutomaticRedirections 屬性的組態設定。Gets a value that indicates whether the handler supports configuration settings for the AllowAutoRedirect and MaxAutomaticRedirections properties.

UseCookies

取得或設定值,表示處理常式是否使用 CookieContainer 屬性來儲存伺服器 Cookie 並在傳送要求時使用這些 Cookie。Gets or sets a value that indicates whether the handler uses the CookieContainer property to store server cookies and uses these cookies when sending requests.

UseDefaultCredentials

取得或設定值,控制是否隨著處理常式的要求傳送預設認證。Gets or sets a value that controls whether default credentials are sent with requests by the handler.

UseProxy

取得或設定值,指出處理常式是否針對要求使用 Proxy。Gets or sets a value that indicates whether the handler uses a proxy for requests.

方法

Dispose()

釋放 Unmanaged 資源,並處置 HttpMessageHandler 所使用的 Managed 資源。Releases the unmanaged resources and disposes of the managed resources used by the HttpMessageHandler.

(繼承來源 HttpMessageHandler)
Dispose(Boolean)

釋放 HttpClientHandler 所使用的 Unmanaged 資源,並選擇性處置 Managed 資源。Releases the unmanaged resources used by the HttpClientHandler and optionally disposes of the managed resources.

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(繼承來源 Object)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(繼承來源 Object)
SendAsync(HttpRequestMessage, CancellationToken)

根據 HttpRequestMessage 提供的資訊建立 HttpResponseMessage 的執行個體,做為不封鎖作業。Creates an instance of HttpResponseMessage based on the information provided in the HttpRequestMessage as an operation that will not block.

ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

適用於

另請參閱