【问题标题】:Could not create SSL/TLS secure channel with HttpRequest无法使用 HttpRequest 创建 SSL/TLS 安全通道
【发布时间】:2018-05-26 09:08:56
【问题描述】:

您好,我正在尝试对网站执行 HttpRequest,当我致电 GetRequestStream() 时,我不断收到此错误。

我的代码:

        byte[] data = Encoding.ASCII.GetBytes(myJson);
        // Create a request for the URL. 
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
            ServicePointManager.Expect100Continue = true;
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(
          "https://host-address.com:443");
        request.ServerCertificateValidationCallback += (sender, certificate2, chain, sslPolicyErrors) => {
            return true;
        };

        X509Certificate2Collection certificates = new X509Certificate2Collection();
       certificates.Import("C:\\Certs\\MyCertificate.pfx", "myCertPassword", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet);

        request.ClientCertificates = certificates;

        request.Method = "POST";
        request.ContentType = "text/json";
        request.ContentLength = data.Length;
        request.UserAgent = "My User Agent";
        request.ProtocolVersion = HttpVersion.Version11;
        request.KeepAlive = true;

        Console.WriteLine("Requesting connection....");
        //The exception is thrown here
        using (Stream stream = request.GetRequestStream()) //Here's where the exception happens
        {

            stream.Write(data, 0, data.Length);
        }

我已经尝试过这个 Could not create SSL/TLS secure channel, despite setting ServerCertificateValidationCallback 但似乎不起作用,还将委托 ServerCertificateValidationCallback 设置为始终返回 true,但它甚至没有达到那部分。

这可能是密码问题? 由于我在 chrome 上浏览页面时可以得到页面的响应,而不是在 IE 上浏览。

【问题讨论】:

    标签: .net ssl certificate httprequest protocols


    【解决方案1】:

    我建议如下:

    1. 在本地下载服务器 SSL 证书并将其链有效性检查到本地受信任的根 CA。
    2. 检查服务器 SSL 证书信息 https://www.ssllabs.com/ssltest/analyze.html
    3. 确保应用程序在其下运行的用户帐户信任根 CA(我可能需要全局计算机存储,而不是本地存储)。
    4. 尝试来自应用程序的简单 GET 请求。

    希望这可以让您知道出了什么问题。

    【讨论】:

      猜你喜欢
      • 2021-12-13
      • 2014-12-26
      • 2017-02-10
      • 2017-08-22
      • 2016-08-03
      • 2018-06-17
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      相关资源
      最近更新 更多