我正在使用 ASP.NET Core 2.2,我在其中使用来自非安全门户的安全 API(例如https://google.com )。但是在请求 API 时出现以下错误:
System.Net.Http.HttpRequestException:无法建立 SSL 连接,请参阅内部异常。
我已经尝试过ServerCertificateCustomValidationCallback
,等ServerCertificateCustomValidationCallback
,ServicePointManager.SecurityProtocol
但它没有用。
当我使用时ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3;
,我收到错误:
System.NotSupportedException:不支持请求的安全协议”
我已经尝试过的示例代码::
1.
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls |
SecurityProtocolType.Tls11 |
SecurityProtocolType.Tls12 |
SecurityProtocolType.Ssl3;
2.
using (var httpClientHandler = new HttpClientHandler())
{
httpClientHandler.ServerCertificateCustomValidationCallback =
(message, cert, chain, errors) => { return true; };
using (var client = new HttpClient(httpClientHandler))
{ // somecode }
}
3.
var handler = new HttpClientHandler
{
ServerCertificateCustomValidationCallback =
(httpRequestMessage, cert, cetChain, policyErrors) =>
{
return true;
},
SslProtocols = SslProtocols.Tls12 |
SslProtocols.Tls11 |
SslProtocols.Tls |
SslProtocols.Ssl3 |
SslProtocols.Ssl2 |
SslProtocols.Default |
SslProtocols.None
};