0

我想向“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 : "");

        }
    }
}
4

1 回答 1

-2

添加这行代码,你就可以开始了。

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

于 2021-11-23T19:00:07.777 回答