The request was aborted: Could not create SSL/TLS secure channel

后端 未结 30 1851
遇见更好的自我
遇见更好的自我 2020-11-22 01:21

We are unable to connect to an HTTPS server using WebRequest because of this error message:

The request was aborted: Could not create SSL/TLS secur

30条回答
  •  臣服心动
    2020-11-22 01:43

    None of the answers worked for me.

    This is what worked:

    Instead of initializing my X509Certifiacte2 like this:

       var certificate = new X509Certificate2(bytes, pass);
    

    I did it like this:

       var certificate = new X509Certificate2(bytes, pass, X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);
    

    Notice the X509KeyStorageFlags.Exportable !!

    I didn't change the rest of the code (the WebRequest itself):

    // I'm not even sure the first two lines are necessary:
    ServicePointManager.Expect100Continue = true; 
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
    
    request = (HttpWebRequest)WebRequest.Create(string.Format("https://{0}.sii.cl/cvc_cgi/dte/of_solicita_folios", server));
    request.Method = "GET";
    request.Referer = string.Format("https://hercules.sii.cl/cgi_AUT2000/autInicio.cgi?referencia=https://{0}.sii.cl/cvc_cgi/dte/of_solicita_folios", servidor);
    request.UserAgent = "Mozilla/4.0";
    request.ClientCertificates.Add(certificate);
    request.CookieContainer = new CookieContainer();
    
    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {
        // etc...
    }
    

    In fact I'm not even sure that the first two lines are necessary...

提交回复
热议问题