繁体   English   中英

C# HttpClient:无法创建 SSL/TLS 安全通道

[英]C# HttpClient: Could not create SSL/TLS secure channel

我想向“https://etebarkala.com”发送一个简单的请求。

  1. 从 .net 版本 4.5 到 4.8 尝试但没有成功
  2. 本网站可使用浏览器轻松打开
  3. 没有任何 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 : "");

        }
    }
}

添加这行代码,您就可以使用 go。

ServicePointManager.Expect100Continue = true; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM