1

I wrote this simple testing function to call a site and return it's response; i use the ServerCertificateValidationCallback to try to ignore any SSL problems if they may appear.

    private static void TestURL(string url)
    {
        try
        {
            //global::System.Net.ServicePointManager.UseNagleAlgorithm = false;
            //global::System.Net.ServicePointManager.CheckCertificateRevocationList = false;
            global::System.Net.ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) =>
            {
                global::System.Console.WriteLine("Certificate Callback Fired: " + (certificate == null ? "null Certificate" : certificate.ToString()));
                return true;
            };
            global::System.Console.WriteLine("Pre Request");
            global::System.Net.HttpWebRequest Request = (global::System.Net.HttpWebRequest)global::System.Net.WebRequest.Create(url);
            //Request.Accept = "application/json";
            //Request.ContentType = "application/json";
            //Request.Method = global::System.Net.WebRequestMethods.Http.Get;
            global::System.Console.WriteLine("Request Set");
            try
            {
                global::System.Console.WriteLine("Pre Response");
                using (global::System.Net.WebResponse Response = Request.GetResponse())
                {
                    global::System.IO.Stream ResponseStream = Response.GetResponseStream();
                    using (global::System.IO.StreamReader Reader = new global::System.IO.StreamReader(ResponseStream, global::System.Text.Encoding.UTF8))
                    {
                        global::System.Console.WriteLine("Response Stream Says:");
                        global::System.Console.WriteLine(Reader.ReadToEnd());
                        global::System.Console.WriteLine();
                    }
                }
                global::System.Console.WriteLine("After Request");
            }
            catch (global::System.Net.WebException e)
            {
                if (e.Response != null)
                {
                    int HCode = ((int)((global::System.Net.HttpWebResponse)e.Response).StatusCode);
                    global::System.Console.WriteLine("Error: Response Code " + HCode.ToString());
                } else { global::System.Console.WriteLine("Error: e.Response null for: \"" + e.Message + "\""); }
            } catch { global::System.Console.WriteLine("Error: Unknow"); }
        } catch (global::System.Exception exOut) { global::System.Console.WriteLine("Error (out exception): " + exOut.Message); }
    }

The problem that i face is that ServerCertificateValidationCallback is never called to a site with https and a certificate (as it shows by accessing it via browser) and so i get an exception on the "Request.GetResponse()" call as "The request was aborted: Could not create SSL/TLS secure channel" (or in older .Net frameworks "The underlying connection was closed: An unexpected error occurred on a send").

SammuelMiranda
  • 420
  • 4
  • 29
  • Does this answer your question? [The request was aborted: Could not create SSL/TLS secure channel](https://stackoverflow.com/questions/2859790/the-request-was-aborted-could-not-create-ssl-tls-secure-channel) – quaabaam Mar 16 '21 at 18:59
  • 1
    The solution that work often is to add following line at beginning of the code : ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; – jdweng Mar 16 '21 at 19:02
  • 1
    jdweng, place this comment as answer so i can mark as answerd - indeed worked. – SammuelMiranda Mar 16 '21 at 19:13

0 Answers0