前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >dotnet 6 HttpClientHandler 和 SocketsHttpHandler 有什么差别

dotnet 6 HttpClientHandler 和 SocketsHttpHandler 有什么差别

作者头像
林德熙
发布2022-08-12 19:48:28
7810
发布2022-08-12 19:48:28
举报
文章被收录于专栏:林德熙的博客林德熙的博客

本文来告诉大家在 dotnet 6 的 HttpClientHandler 和 SocketsHttpHandler 两个类型有什么不同

在创建 HttpClient 时,可以在 HttpClient 的构造函数传入 HttpMessageHandler 类型的参数,此参数将执行实际的逻辑。其中常用的传入参数类型就是 HttpClientHandler 和 SocketsHttpHandler 类型

那这两个类型有什么差别呢?根据官方文档 可以了解到,从 .NET Core 2.1 开始,默认的 HttpClient 底层的网络通讯实现就是靠 System.Net.Http.SocketsHttpHandler 实现的,替代了原先的 HttpClientHandler 类型

也就是说在 dotnet 6 采用的 HttpClient 底层使用的是 SocketsHttpHandler 类型作为默认的网络通讯。那原有的使用 HttpClientHandler 的代码呢?其实在底层也做了统一,使用 HttpClientHandler 也将在底层采用 SocketsHttpHandler 作为网络通讯

请看 dotnet 6 的 HttpClientHandler 的源代码

代码语言:javascript
复制
using HttpHandlerType = System.Net.Http.SocketsHttpHandler;

    public partial class HttpClientHandler : HttpMessageHandler
    {
        private readonly HttpHandlerType _underlyingHandler;

        private HttpMessageHandler Handler
            => _underlyingHandler;

        public HttpClientHandler()
        {
            _underlyingHandler = new HttpHandlerType();

            ClientCertificateOptions = ClientCertificateOption.Manual;
        }

        protected internal override HttpResponseMessage Send(HttpRequestMessage request, CancellationToken cancellationToken) =>
            Handler.Send(request, cancellationToken);

        protected internal override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) =>
            Handler.SendAsync(request, cancellationToken);

        // 忽略其他逻辑
    }

可以看到在 HttpClientHandler 的底层实现就是使用 SocketsHttpHandler 来实现的

也就是说,无论你在 HttpClient 传入的参数类型是 SocketsHttpHandler 还是 HttpClientHandler 类型,在 dotnet 6 都会实际上调用到 SocketsHttpHandler 类型。还请不需要纠结这部分的差异

任何对 HttpClientHandler 的配置都会被设置到底层的 SocketsHttpHandler 类型的 Handler 属性

唯一需要开始纠结差异的部分只是在于 SocketsHttpHandler 提供了更多的控制性,如连接超时时间,以及更新 DNS 解析时间和更多的 SSL 控制。由于 HttpClientHandler 是 .NET Framework 4.5 时就提供的类型,比 .NET Core 2.1 提供的 SocketsHttpHandler 类型可使用的 API 数量要少。如果你需要对网络有更多的控制,那还请采用 SocketsHttpHandler 类型

代码语言:javascript
复制
var socketsHttpHandler = new SocketsHttpHandler()
{
    // 这个可以设置连接的超时时间
    ConnectTimeout = TimeSpan.FromSeconds(10),
    //PooledConnectionIdleTimeout = TimeSpan.FromSeconds(100),
    // HttpClient only resolves DNS entries when a connection is created. It does not track any time to live (TTL) durations specified by the DNS server. If DNS entries change regularly, which can happen in some container scenarios, the client won't respect those updates. To solve this issue, you can limit the lifetime of the connection by setting the SocketsHttpHandler.PooledConnectionLifetime property, so that DNS lookup is required when the connection is replaced.
    PooledConnectionLifetime = TimeSpan.FromSeconds(1000),
    SslOptions = new SslClientAuthenticationOptions()
    {
        RemoteCertificateValidationCallback = (sender, certificate, chain, errors) => true, // HttpClientHandler.DangerousAcceptAnyServerCertificateValidator 忽略证书错误
    },
};

var httpClient = new HttpClient(socketsHttpHandler);

采用 SocketsHttpHandler 能够做到更多的平台独立,从而让网络行为在各个平台统一

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档