此问题通常是由与 SSL/TLS 握手相关的配置问题引起的。以下是一些可能的
确保使用的 SSL/TLS 版本与服务器兼容。可以通过将代码中的 HttpClientHandler 实例化为最新版本来实现:new HttpClientHandler { SslProtocols = SslProtocols.Tls12 | SslProtocols.Tls13 }。
禁用 SSL/TLS 压缩。在 HttpClientHandler 上设置 CheckCertificateRevocationList = true 和 CheckCertificateRevocationList = false。
在 IIS 上禁用 SSL 2.0 和 SSL 3.0 协议。可以通过设置对应的注册表键值来实现:在 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\SSL 2.0\Client 和 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\SSL 3.0\Client 下创建一个 DWORD 值名为 DisabledByDefault 并将其设置为 1。
确保服务器证书是有效的,并且信任它的根证书已安装在客户端上。
以下是解决方法中的代码示例:
var handler = new HttpClientHandler { SslProtocols = SslProtocols.Tls12 | SslProtocols.Tls13 }; var client = new HttpClient(handler);
handler.CheckCertificateRevocationList = true; handler.CheckCertificateRevocationList = false;
Registry.SetValue(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\SSL 2.0\Client", "DisabledByDefault", 1, RegistryValueKind.DWord); Registry.SetValue(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\SSL 3.0\Client", "DisabledByDefault", 1, RegistryValueKind.DWord);