在创建 HttpClient 时,其中常用的传入参数类型就是 HttpClientHandler 和 SocketsHttpHandler 类型
从 .NET Core 2.1 开始,默认的 HttpClient 底层的网络通讯实现就是靠 System.Net.Http.SocketsHttpHandler 实现的,替代了原先的 HttpClientHandler 类型。
在 dotnet 6 采用的 HttpClient 底层使用的是 SocketsHttpHandler 类型作为默认的网络通讯。
无论你在 HttpClient 传入的参数类型是 SocketsHttpHandler 还是 HttpClientHandler 类型,在 dotnet 6 都会实际上调用到 SocketsHttpHandler 类型
Table of Contents
HttpClient - HttpClientHandler
Note
There is really no reason to use SocketsHttpHandler instead of HttpClientHandler in .Net 5 or 6 because HttpClientHandler simply redirects all calls to a internal SocketsHttpHandler instance.
HttpClientHandler 的底层实现就是使用 SocketsHttpHandler 来实现
HttpClient - SocketsHttpHandler
or
source:https://dev.to/tswiftma/switching-from-httpclienthandler-to-socketshttphandler-17h3
HttpClient - SocketsHttpHandler – ConnectCallback
ConnectCallback enables customizing new connection creation. It is called every time a new TCP connection is opened. The callback can be used to establish in-process transport, to control DNS resolution, to control general or platform-specific options of the underlying socket, or just to be notified whenever a new connection is opened. The following remarks apply to the callback:
It is passed DnsEndPoint determining the remote endpoint and HttpRequestMessage which initiated the connection creation.
Since SocketsHttpHandler provides connection pooling, the created connection might be used to handle multiple subsequent requests, not just the initial one.
A new Stream is expected to be returned.
The callback should not try to establish a TLS session. This is handled afterwards by SocketsHttpHandler.
source:https://devblogs.microsoft.com/dotnet/net-5-new-networking-improvements/
Useful links
dotnet 6 HttpClientHandler 和 SocketsHttpHandler 有什么差别
https://cloud.tencent.com/developer/article/2071585
HttpClient原理
https://www.cnblogs.com/xiaoxiaotank/p/16273773.html