HttpClientHandler 類別

定義

.NET Framework 和 .NET Core 2.0 及更舊版本中 HttpClient 使用的預設訊息處理常式。

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();
   client.Dispose();
}

備註

如需此 API 的詳細資訊,請參閱 HttpClientHandler 的補充 API 備註

建構函式

HttpClientHandler()

建立 HttpClientHandler 類別的執行個體。

屬性

AllowAutoRedirect

取得或設定值,這個值指出此處理常式是否應該接在重新導向回應之後。

AutomaticDecompression

取得或設定解壓縮方法的類型,此為 HTTP 內容回應的自動解壓縮處理常式所使用。

CheckCertificateRevocationList

取得或設定值,指出是否根據憑證授權撤銷清單來檢查憑證。

ClientCertificateOptions

取得或設定值,表示憑證是否會從憑證存放區自動選取,或呼叫端是否允許傳入特定用戶端憑證。

ClientCertificates

取得與伺服器要求相關聯的安全性憑證集合。

CookieContainer

取得或設定處理常式用來儲存伺服器 cookie 的 cookie 容器。

Credentials

取得或設定這個處理常式所用的驗證資訊。

DangerousAcceptAnyServerCertificateValidator

取得一律傳回 true 的快取委派。

DefaultProxyCredentials

使用預設 (系統) Proxy 時,取得或設定要提交至預設 Proxy 伺服器進行驗證的認證。 只有當 UseProxy 設定為 trueProxy 設定為 null 時,才使用預設 Proxy。

MaxAutomaticRedirections

取得或設定處理常式追蹤的最大重新導向數目。

MaxConnectionsPerServer

取得或設定使用 HttpClient 物件提出要求時,所允許的同時連線 數目上限 (每個伺服器端點)。 請注意,此限制是按照每個伺服器端點計算,例如值 256 允許 http://www.adatum.com/ 使用 256 個同時連線,而 http://www.adventure-works.com/ 另有 256 個同時連線。

MaxRequestContentBufferSize

取得或設定此處理常式所使用最大要求內容的緩衝區大小。

MaxResponseHeadersLength

取得或設定回應標頭的最大長度,以 KB (1024 位元組) 為單位。 比方說,如果值為 64,則最大回應標頭長度允許有 65536 個位元組。

MeterFactory

取得或設定 IMeterFactory ,以建立 實體的HttpClientHandler自訂 Meter

PreAuthenticate

取得或設定值,這個值指出處理常式是否隨著要求傳送授權標頭。

Properties

取得 HttpClient 要求之自訂屬性的可寫入字典 (也就是對應)。 字典會初始化為空字典。您可以插入及查詢自訂處理常式和特殊處理的索引鍵/值組。

Proxy

取得或設定處理常式所使用的 Proxy 資訊。

ServerCertificateCustomValidationCallback

取得或設定驗證伺服器憑證的回呼方法。

SslProtocols

取得或設定 TLS/SSL 通訊協定,其是由 HttpClientHandler 物件管理之 HttpClient 物件所使用。

SupportsAutomaticDecompression

取得值,表示處理常式是否支援自動回應內容解壓縮。

SupportsProxy

取得值,這個值指出處理常式是否支援 Proxy 設定。

SupportsRedirectConfiguration

取得值,這個值指出處理常式是否支援 AllowAutoRedirectMaxAutomaticRedirections 屬性的組態設定。

UseCookies

取得或設定值,這個值表示處理程式是否使用 CookieContainer 屬性來儲存伺服器 Cookie,並在傳送要求時使用這些 Cookie。

UseDefaultCredentials

取得或設定值,控制是否隨著處理常式的要求傳送預設認證。

UseProxy

取得或設定值,指出處理常式是否針對要求使用 Proxy。

方法

Dispose()

釋放 Unmanaged 資源,並處置 HttpMessageHandler 所使用的 Managed 資源。

(繼承來源 HttpMessageHandler)
Dispose(Boolean)

釋放 HttpClientHandler 所使用的 Unmanaged 資源,並選擇性處置 Managed 資源。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

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

根據中提供的資訊建立的HttpResponseMessageHttpRequestMessage實例。

Send(HttpRequestMessage, CancellationToken)

在衍生類別中遭到覆寫時,使用指定的要求和取消語彙基元來傳送 HTTP 要求。 否則,擲回 NotSupportedException

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

根據 中HttpRequestMessage提供的資訊建立 的HttpResponseMessage實例,做為不會封鎖的作業。

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於

另請參閱