地址是https的,用httpClient请求,有个问题,SSL证书不正确,参考网上一些给出的答案整理如下。
报错如下:
System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception.
解决方案
1、绕过https证书
var httpClientHandler = new HttpClientHandler();
httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true;
HttpClient client = new HttpClient(httpClientHandler);
2、使用SecurityProtocol定义安全协议
SecurityProtocolType枚举包含以下成员:
Ssl3: 表示使用SSL 3.0协议。
Tls: 表示使用TLS 1.0协议。
Tls11: 表示使用TLS 1.1协议。
Tls12: 表示使用TLS 1.2协议。
Tls13: 表示使用TLS 1.3协议(在某些.NET版本中可能不可用)。
SystemDefault: 表示使用操作系统的默认安全协议。
也可以通过组合这些值(使用按位或运算符|)来指定多个协议,如
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls13;
在发送请求地址代码前定义安全协议,参考代码
HttpClient httpClient = new HttpClient();
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var ga= await httpClient.GetAsync(url);
3、部署程序服务器与API服务器之间的网络通讯权限问题