我想发送一个简单的请求到“https://etebarkala.com“。
1.已尝试从.net版本4.5升级到4.8,但未成功
1.使用浏览器可轻松打开此网站
1.没有任何SSL验证错误或警告
结果:请求已中止:无法创建SSL/TLS安全通道。
var request = new HttpRequestMessage(HttpMethod.Get, new Uri("https://etebarkala.com"));
request.Headers.Add("Accept-Language", "en-US");
request.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:94.0) Gecko/20100101 Firefox/94.0");
request.Headers.Add("Upgrade-Insecure-Requests", "1");
request.Headers.Add("Connection", "keep-alive");
using (HttpClientHandler handler = new HttpClientHandler())
{
handler.SslProtocols = SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12 | SslProtocols.Tls13;//| SslProtocols.Ssl3;//| SslProtocols.Ssl3;
handler.ServerCertificateCustomValidationCallback = (snder, cert, chain, error) => { return true; };
ServicePointManager.ServerCertificateValidationCallback = (snder, certificate, chain, errors) => { return true; };
ServicePointManager.DefaultConnectionLimit = 9999;
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls13 | SecurityProtocolType.Ssl3 | SecurityProtocolType.Ssl3 | SecurityProtocolType.SystemDefault;
using (HttpClient client = new HttpClient(handler))
{
try
{
HttpResponseMessage res = client.SendAsync(request).Result;
textBox1.Text = res.Content.ReadAsStringAsync().Result;
}
catch (Exception e)
{
string Result = e.Message;
textBox1.Text = "Error:" + Result
+ Environment.NewLine + (e.InnerException != null ? e.InnerException.Message : "")
+ Environment.NewLine + (e.InnerException != null && e.InnerException.InnerException != null ? e.InnerException.InnerException.Message : "");
}
}
}
2条答案
按热度按时间ki1q1bka1#
在我的例子中,访问本地机器存储中的证书的权限是个问题。我通过HttpClient的指纹从机器存储中阅读HttpClient的证书,当我调用SendAsync方法时,我得到了“* 请求被中止:无法创建SSL/TLS安全通道。*”异常。
在管理模式下运行certlm.msc,右键单击目标证书,选择所有任务-〉管理私钥,然后向您的进程正在访问其下的存储的用户添加权限。如果是IIS,则可能是NETWORK SERVICE。
1rhkuytd2#
添加这几行代码,就可以开始了。
服务点管理器.预期100继续=真;服务点管理器.安全协议=安全协议类型. Tls 12;