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

注解

从 .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)
    
  • 通过在netcore. runtimeconfig.template.json配置文件中定义 System.Net.Http.UseSocketsHttpHandler 开关: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()

属性

AllowAutoRedirect
AutomaticDecompression

获取或设置处理程序用于自动解压缩 HTTP 内容响应的解压缩方法类型。Gets or sets the type of decompression method used by the handler for automatic decompression of the HTTP content response.

ConnectTimeout
CookieContainer

获取或设置托管 cookie 容器对象。Gets or sets the managed cookie container object.

Credentials
DefaultProxyCredentials

使用默认(系统)代理时,获取或设置用于提交到默认代理服务器进行身份验证的凭据。When the default (system) proxy is used, gets or sets the credentials used to submit to the default proxy server for authentication.

Expect100ContinueTimeout
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

获取或设置响应标头的最大长度,以千字节(1024 字节)为单位。Gets or sets the maximum length, in kilobytes (1024 bytes), of the response headers.

PooledConnectionIdleTimeout

获取或设置池中的连接处于空闲状态多长时间后才能视为可重复使用。Gets or sets how long a connection can be idle in the pool to be considered reusable.

PooledConnectionLifetime
PreAuthenticate

获取或设置一个值,该值指示处理程序是否随请求发送授权标头。Gets or sets a value that indicates whether the handler sends an Authorization header with the request.

Properties
Proxy

当  UseProxy 属性为 true 时,获取或设置自定义代理。Gets or sets the custom proxy when the UseProxy property is true.

ResponseDrainTimeout
SslOptions

获取或设置用于客户端 TLS 身份验证的选项集。Gets or sets the set of options used for client TLS authentication.

UseCookies
UseProxy

方法

Dispose()

释放由 HttpMessageHandler 使用的非托管资源和托管资源。Releases the unmanaged resources and disposes of the managed resources used by the HttpMessageHandler.

(继承自 HttpMessageHandler)
Dispose(Boolean)

释放由 HttpMessageHandler 使用的非托管资源,并可根据需要释放托管资源。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)
SendAsync(HttpRequestMessage, CancellationToken)

以异步操作发送 HTTP 请求。Send an HTTP request as an asynchronous operation.

(继承自 HttpMessageHandler)
ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)

适用于