SocketsHttpHandler 類別

定義

提供 .NET Core 2.1 及更新版本中 HttpClient 使用的預設訊息處理常式。Provides the default message handler used by HttpClient in .NET Core 2.1 and later.

public ref class SocketsHttpHandler sealed : System::Net::Http::HttpMessageHandler
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public sealed class SocketsHttpHandler : System.Net.Http.HttpMessageHandler
public sealed class SocketsHttpHandler : System.Net.Http.HttpMessageHandler
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
type SocketsHttpHandler = class
    inherit HttpMessageHandler
type SocketsHttpHandler = class
    inherit HttpMessageHandler
Public NotInheritable Class SocketsHttpHandler
Inherits HttpMessageHandler
繼承
SocketsHttpHandler
屬性

備註

從 .NET Core 2.1 開始, SocketsHttpHandler 類別會提供較高層級的 HTTP 網路類別(例如)所使用的實作為 HttpClientStarting with .NET Core 2.1, the SocketsHttpHandler class provides the implementation used by higher-level HTTP networking classes such as HttpClient. 使用可 SocketsHttpHandler 提供許多優點:The use of SocketsHttpHandler offers a number of advantages:

  • 與之前的實作相比,能提供顯著的效能提升。A significant performance improvement when compared with the previous implementation.

  • 消除平臺相依性,可簡化部署和服務。The elimination of platform dependencies, which simplifies deployment and servicing. 例如,不再相依 libcurl 于適用于 macOS 的 .Net core 和適用于 Linux 的 .Net core。For example, libcurl is no longer a dependency on .NET Core for macOS and .NET Core for Linux.

  • 跨所有 .NET 平臺的一致行為。Consistent behavior across all .NET platforms.

如果不想要這項變更,而且您是在 .NET Core 2.1-3.1 上,您可以將應用程式設定為使用較舊的 System.Net.Http.HttpClientHandler 類別,而不是數種方式:If this change is undesirable and you are on .NET Core 2.1-3.1, you can configure your application to use the older System.Net.Http.HttpClientHandler class instead in a number of 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.js 中定義參數: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.

從 .NET 5 開始,無法使用這些設定選項。These configuration options are not available starting with .NET 5.

建構函式

SocketsHttpHandler()

建立 SocketsHttpHandler 類別的執行個體。Creates an instance of a SocketsHttpHandler 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.

ConnectCallback

取得或設定用來開啟新連線的自訂回呼。Gets or sets a custom callback used to open new connections.

ConnectTimeout

取得或設定連線建立逾時前等候的時間長度。Gets or sets the timespan to wait before the connection establishing times out.

CookieContainer

取得或設定受管理的 Cookie 容器物件。Gets or sets the managed cookie container object.

Credentials

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

DefaultProxyCredentials

在使用預設 (系統) Proxy 時,取得或設定用來提交到預設 Proxy 伺服器進行驗證的認證。When the default (system) proxy is used, gets or sets the credentials used to submit to the default proxy server for authentication.

EnableMultipleHttp2Connections

取得或設定值,這個值表示當所有現有連線上的並行資料流程都已達到最大數目時,是否可建立連至同一部伺服器的其他 HTTP/2 連線。Gets or sets a value that indicates whether additional HTTP/2 connections can be established to the same server when the maximum number of concurrent streams is reached on all existing connections.

Expect100ContinueTimeout

取得或設定伺服器 HTTP 100 Continue 回應的逾時值。Gets or sets the time-out value for server HTTP 100 Continue response.

IsSupported

取得值,這個值表示目前平台是否支援處理常式。Gets a value that indicates whether the handler is supported on the current platform.

KeepAlivePingDelay

取得或設定 keep alive ping 延遲。Gets or sets the keep alive ping delay.

KeepAlivePingPolicy

取得或設定 keep alive ping 行為。Gets or sets the keep alive ping behaviour.

KeepAlivePingTimeout

取得或設定 keep alive ping 逾時。Gets or sets the keep alive ping timeout.

MaxAutomaticRedirections

取得或設定允許的 HTTP 重新導向的數目上限。Gets or sets the maximum number of allowed HTTP redirects.

MaxConnectionsPerServer

取得或設定單一伺服器允許的同時 TCP 連線數目上限。Gets or sets the maximum number of simultaneous TCP connections allowed to a single server.

MaxResponseDrainSize

取得或設定可從回應中清空的資料的數量上限 (以位元組為單位)。Gets or sets the maximum amount of data that can be drained from responses in bytes.

MaxResponseHeadersLength

取得或設定回應標頭的最大長度,以 KB (1024 位元組) 為單位。Gets or sets the maximum length, in kilobytes (1024 bytes), of the response headers.

PlaintextStreamFilter
PooledConnectionIdleTimeout

取得或設定集區中的連線須閒置多久時間,才會視為可重複使用。Gets or sets how long a connection can be idle in the pool to be considered reusable.

PooledConnectionLifetime

取得或設定連線需在集區中多久時間,才會視為可重複使用。Gets or sets how long a connection can be in the pool to be considered reusable.

PreAuthenticate

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

Properties
Proxy

在 UseProxy 屬性為 true 時,取得或設定自訂 Proxy。Gets or sets the custom proxy when the UseProxy property is true.

RequestHeaderEncodingSelector

取得或設定回呼,這個回呼會選取 Encoding 以編碼要求標頭值。Gets or sets a callback that selects the Encoding to encode request header values.

ResponseDrainTimeout

取得或設定要等候資料從回應中清空的時間長度。Gets or sets the timespan to wait for data to be drained from responses.

ResponseHeaderEncodingSelector

取得或設定回呼,這個回呼會選取 Encoding 以解碼回應標頭值。Gets or sets a callback that selects the Encoding to decode response header values.

SslOptions

取得或設定一組用於用戶端 TLS 驗證的選項。Gets or sets the set of options used for client TLS authentication.

UseCookies

取得或設定值,指出此處理常式是否應該使用 Cookie。Gets or sets a value that indicates whether the handler should use cookies.

UseProxy

取得或設定值,指出處理常式是否應該使用 Proxy。Gets or sets a value that indicates whether the handler should use a proxy.

方法

Dispose()

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

(繼承來源 HttpMessageHandler)
Dispose(Boolean)

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

(繼承來源 HttpMessageHandler)
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 的淺層複製。Creates a shallow copy of the current Object.

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

在衍生類別中遭到覆寫時,使用指定的要求和取消語彙基元來傳送 HTTP 要求。When overridden in a derived class, sends an HTTP request with the specified request and cancellation token. 否則,擲回 NotSupportedExceptionOtherwise, throws a NotSupportedException.

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

以非同步作業的方式,傳送 HTTP 要求。Send an HTTP request as an asynchronous operation.

(繼承來源 HttpMessageHandler)
ToString()

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

(繼承來源 Object)

適用於