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

  • Question

  • Im trying to make https request, this code doesnt work. I am getting error 

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

    Server has selfsigned certificate

    static void Main(string[] args)
            {
                ServicePointManager.Expect100Continue = true;
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
                ServicePointManager.ServerCertificateValidationCallback = (sender, x509Certificate, chain, sslPolicyErrors) => true;
    
                var lS = SendWebRequest(new byte[0], ApiAddress);
                lS.Wait();
    
    
                Console.ReadLine();
            }
    
    
            private static async Task<string> SendWebRequest(byte[] sendData, string tagUrl)
            {
                var lRet = string.Empty;
                try
                {
                    var request = (HttpWebRequest)System.Net.WebRequest.Create(tagUrl);
                    request.Method = "POST";
                    request.AllowAutoRedirect = true;
    
                    request.Credentials = CredentialCache.DefaultCredentials;
                    request.ContentType = "text/xml; charset=utf-8";
    
    
                    request.AllowWriteStreamBuffering = true;
                    request.KeepAlive = false;
                    //request.Timeout = timeout;
    
                    request.ServerCertificateValidationCallback = (sender, cert, chain, sslPolicyErrors) => true;
    
                    using (var sw = await request.GetRequestStreamAsync())
                    {
                        await sw.WriteAsync(sendData, 0, sendData.Length);
                    }
    
                    lRet = await request.GetResponseAsync().ContinueWith(t =>
                    {
                        try
                        {
                            using (var sr = new StreamReader(t.Result.GetResponseStream()))
                            {
                                return sr.ReadToEnd();
                            }
                        }
                        catch (Exception ex)
                        {
                            return string.Empty;
                        }
                    });
    
                }
                catch (Exception ex)
                {
                    Debugger.Log(0, "error", ex.Message);
                }
    
                return lRet;
            }

    Wednesday, June 14, 2017 7:35 AM

All replies

  • Hi lvlkoo,

    Thank you for posting here.

    For your question, the ApiAddress you do not show in your code. Hence I use the following URL https://www.google.com.sg to test the code. There is no error. I could not reproduce the error.

    Maybe it caused by the following reasons.

    1.The ApiAddress URL has some verification, you did not access to the verification and the error occurred. Maybe the URL need Permission authentication or login certification. 

    2.If your .NET framework version is not the .NET framework 4.5, you could use following code.

    ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;

    For more details, please refer to the following link.

    https://stackoverflow.com/questions/2859790/the-request-was-aborted-could-not-create-ssl-tls-secure-channel

    3.Maybe your server offer SSL3, the client needs other TLS12. You could try the following code instead.

     ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

    For more details, please refer to the thread in so.

    https://stackoverflow.com/questions/32994464/could-not-create-ssl-tls-secure-channel-despite-setting-servercertificatevalida

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Thursday, June 15, 2017 7:31 AM