在C# 使用 HttpClient或 WebClient发送请求时
提示 :
请求被中止: 未能创建 SSL/TLS 安全通道
或
发送请求时出错
的问题
解决办法:在发送请求地址代码前加如下代码
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;( | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;)
问题解决!
如:
HttpClient httpClient = new HttpClient();
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var res = await httpClient.GetAsync(url);
var contentStr = await res.Content.ReadAsStringAsync();
或者:
handler = new HttpClientHandler() ;
handler.AllowAutoRedirect = true;
handler.UseCookies = true;
handler.CookieContainer = cookies;
handler.ClientCertificateOptions = ClientCertificateOption.Automatic;
httpClient = new HttpClient(handler);