SocketsHttpHandler SocketsHttpHandler SocketsHttpHandler SocketsHttpHandler Class


提供 .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
public sealed class SocketsHttpHandler : System.Net.Http.HttpMessageHandler
type SocketsHttpHandler = class
    inherit HttpMessageHandler
Public NotInheritable Class SocketsHttpHandler
Inherits HttpMessageHandler


從 .net Core 2.1 開始, SocketsHttpHandler類別會提供較高階 HTTP 網路類別 (例如) HttpClient所使用的執行。Starting 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.

如果不需要這項變更, 您可以將應用程式設定為使用System.Net.Http.HttpClientHandler較舊的類別, 而不是透過數種方式:If this change is undesirable, 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.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.


SocketsHttpHandler() SocketsHttpHandler() SocketsHttpHandler() SocketsHttpHandler()


AllowAutoRedirect AllowAutoRedirect AllowAutoRedirect AllowAutoRedirect
AutomaticDecompression AutomaticDecompression AutomaticDecompression AutomaticDecompression

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

ConnectTimeout ConnectTimeout ConnectTimeout ConnectTimeout
CookieContainer CookieContainer CookieContainer CookieContainer

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

Credentials Credentials Credentials Credentials
DefaultProxyCredentials DefaultProxyCredentials DefaultProxyCredentials 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.

Expect100ContinueTimeout Expect100ContinueTimeout Expect100ContinueTimeout Expect100ContinueTimeout
MaxAutomaticRedirections MaxAutomaticRedirections MaxAutomaticRedirections MaxAutomaticRedirections

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

MaxConnectionsPerServer MaxConnectionsPerServer MaxConnectionsPerServer MaxConnectionsPerServer

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

MaxResponseDrainSize MaxResponseDrainSize MaxResponseDrainSize MaxResponseDrainSize

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

MaxResponseHeadersLength MaxResponseHeadersLength MaxResponseHeadersLength MaxResponseHeadersLength

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

PooledConnectionIdleTimeout PooledConnectionIdleTimeout PooledConnectionIdleTimeout PooledConnectionIdleTimeout

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

PooledConnectionLifetime PooledConnectionLifetime PooledConnectionLifetime PooledConnectionLifetime
PreAuthenticate PreAuthenticate PreAuthenticate PreAuthenticate

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

Properties Properties Properties Properties
Proxy Proxy Proxy Proxy

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

ResponseDrainTimeout ResponseDrainTimeout ResponseDrainTimeout ResponseDrainTimeout
SslOptions SslOptions SslOptions SslOptions

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

UseCookies UseCookies UseCookies UseCookies
UseProxy UseProxy UseProxy UseProxy


Dispose() Dispose() Dispose() Dispose()

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

(Inherited from HttpMessageHandler)
Dispose(Boolean) Dispose(Boolean) Dispose(Boolean) Dispose(Boolean)

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

(Inherited from HttpMessageHandler)
Equals(Object) Equals(Object) Equals(Object) Equals(Object)

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

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

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

(Inherited from Object)
GetType() GetType() GetType() GetType()

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

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

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

(Inherited from Object)
SendAsync(HttpRequestMessage, CancellationToken) SendAsync(HttpRequestMessage, CancellationToken) SendAsync(HttpRequestMessage, CancellationToken) SendAsync(HttpRequestMessage, CancellationToken)

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

(Inherited from HttpMessageHandler)
ToString() ToString() ToString() ToString()

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

(Inherited from Object)