当前位置:   article > 正文

请求被中止: 未能创建 SSL/TLS 安全通道_“securityprotocoltype”未包含“tls11”的定义

“securityprotocoltype”未包含“tls11”的定义

if (this.RequestHeaders == null) { this.RequestHeaders._Referer = url.urlTxt; } Sharing.setTips("已发出请求"); text = HTTP.GetUrlHtml(url.urlTxt, this.RequestHeaders, this.Timeout, this.cookie); if (text.IndexOf("#EXTM3U") < 0) { Sharing.setTips("返回的数据错误\r\n原因:" + HTTP._LastError); return list; } 报错HTTP._LastError = "请求被中止: 未能创建 SSL/TLS 安全通道。"

应用程序在尝试通过 HTTPS 协议进行网络请求时遇到了问题。具体而言,这通常是由于以下原因之一引起的:

  1. TLS 版本不兼容: 服务器使用的 SSL/TLS 版本可能与你的客户端不兼容。例如,服务器要求使用的是 TLS 1.2,但你的客户端只支持较旧的 TLS 版本(如 TLS 1.0 或 TLS 1.1)。

  2. 加密套件不匹配: 客户端和服务器之间的加密套件不匹配,导致无法协商一个共同的安全协议。

  3. 证书问题: 客户端可能没有正确的根证书或中间证书来验证服务器的 SSL 证书,或者服务器的证书可能已经过期或无效。

为了解决这个问题,你可以尝试以下几个步骤:

  • 更新 TLS 版本: 确保你的应用程序使用的是支持的最新 TLS 版本。可以尝试设置 ServicePointManager.SecurityProtocol,包括 SecurityProtocolType.Tls12,来明确指定使用 TLS 1.2。

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
  • 检查证书: 确保你的客户端能够正确验证服务器的 SSL 证书。有时候需要将服务器的根证书或中间证书导入到客户端的信任证书存储中。

  • 调试 SSL/TLS 握手问题: 可以使用网络调试工具(如 Wireshark)来分析客户端和服务器之间的 SSL/TLS 握手过程,以便找出具体的问题点。

在最新版本的 .NET Framework 中,SecurityProtocolType 枚举确实不再包含 Tls11Tls12 等具体的 TLS 版本定义。相反,你应该直接使用整数值来表示对应的 TLS 版本。

在 .NET Framework 中,这些整数值的定义如下:

  • Tls 对应于 SecurityProtocolType.Tls,值为 192
  • Tls11 对应于 SecurityProtocolType.Tls11,值为 768
  • Tls12 对应于 SecurityProtocolType.Tls12,值为 3072

所以,为了解决你的问题,你可以直接在代码中使用这些整数值来设置 ServicePointManager.SecurityProtocol 属性。

  1. public static void Main()
  2. {
  3. // 设置使用 TLS 1.1 和 TLS 1.2
  4. ServicePointManager.SecurityProtocol = (SecurityProtocolType)768 | (SecurityProtocolType)3072;
  5. // 执行 HTTPS 请求
  6. string url = "https://example.com";
  7. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  8. using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
  9. {
  10. // 读取响应数据
  11. using (Stream stream = response.GetResponseStream())
  12. {
  13. StreamReader reader = new StreamReader(stream);
  14. string responseData = reader.ReadToEnd();
  15. Console.WriteLine(responseData);
  16. }
  17. }
  18. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/神奇cpp/article/detail/830607
推荐阅读
相关标签
  

闽ICP备14008679号