简体   繁体   English

无法使用HttpRequest创建SSL / TLS安全通道

[英]Could not create SSL/TLS secure channel with HttpRequest

Hi I am trying to do a HttpRequest to a website and when I call GetRequestStream() I keep getting this Error. 嗨,我正在尝试对网站进行HttpRequest,当我调用GetRequestStream()我一直收到此错误。

My code: 我的代码:

        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);
        }

I already tried this Could not create SSL/TLS secure channel, despite setting ServerCertificateValidationCallback but not seems to work, also set the delegate ServerCertificateValidationCallback to always return true but it's not even reaching that part. 尽管设置了ServerCertificateValidationCallback但似乎无法正常工作,但我已经尝试过此“ 无法创建SSL / TLS安全通道”,还将委托ServerCertificateValidationCallback设置为始终返回true,但甚至没有达到该值。

This might be a cypher problem? 这可能是密码问题吗? Due that I can get a response from the page when I browse it on chrome but not with IE. 因此,当我在chrome上浏览网页时(但无法在IE中浏览),我可以从该网页获得响应。

I would recommend following: 我建议以下内容:

  1. Download server SSL certificate locally and check its chain validity to local trusted root CA. 在本地下载服务器SSL证书,并检查其到本地受信任根CA的链有效性。
  2. Check server SSL certificate info on https://www.ssllabs.com/ssltest/analyze.html https://www.ssllabs.com/ssltest/analyze.html上检查服务器SSL证书信息
  3. Make sure user account under which application operates trusts that root CA (may me you need global machine storage, not local storage). 确保运行应用程序的用户帐户信任该根CA(也许我需要全局计算机存储,而不是本地存储)。
  4. Try simple GET request from application. 尝试从应用程序获取简单的GET请求。

Hope this could give you a clue what's wrong. 希望这可以为您提供提示,以解决问题。

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

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