Click here to Skip to main content
15,887,453 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am using the following code in C# to read the SSL website the Error is coming in Error.txt -
Exception Read: The request was aborted: Could not create SSL/TLS secure channel.
:
C#
//On the top of the page
using System.Net;
using System.IO;

//My methods
        private void btnGetData_Click(object sender, EventArgs e)
        {

            StringBuilder sb = new StringBuilder();

            try
            {

                // used on each read operation
                byte[] buf = new byte[8192];

                ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);
                Uri uri = new Uri("https://www.zillow.com/homedetails/2138325511_zpid");
                WebRequest webRequest = WebRequest.Create(uri);
                WebResponse webResponse = webRequest.GetResponse();
                //ReadFrom(webResponse.GetResponseStream());
                Stream response = webResponse.GetResponseStream();


                string tempString = null;
                int count = 0;
                do
                {
                    // fill the buffer with data
                    count = response.Read(buf, 0, buf.Length);

                    // make sure we read some data
                    if (count != 0)
                    {
                        // translate from bytes to ASCII text
                        tempString = Encoding.ASCII.GetString(buf, 0, count);

                        // continue building the string
                        sb.Append(tempString);
                    }
                }
                while (count > 0); // any more data to read?
            }
            catch (Exception ex2)
            {
                try
                {

                    StreamWriter sw = new StreamWriter(Environment.CurrentDirectory + "\\Error.txt");

                    //Write a line of text
                    sw.WriteLine("Exception Read: " + ex2.Message);

                    //Close the file
                    sw.Close();
                    this.Close();
                    return;
                }
                catch(Exception exinner2)
                {
                    Console.WriteLine("Exception Read: " + exinner2.Message);
                    this.Close();
                    return;
                }
            }

            // write to text file page source
            try
            {

                //Pass the filepath and filename to the StreamWriter Constructor
                StreamWriter sw = new StreamWriter(Environment.CurrentDirectory + "\\Pageoutput.txt");

                //Write a line of text
                sw.WriteLine(sb.ToString());

                //Close the file
                sw.Close();
                MessageBox.Show("Data inserted Successfully");
            }
            catch (Exception ex)
            {
                try
                {

                    StreamWriter sw = new StreamWriter(Environment.CurrentDirectory + "\\Error.txt");

                    //Write a line of text
                    sw.WriteLine("Exception Write: " + ex.Message);

                    //Close the file
                    sw.Close();
                }
                catch (Exception exinner)
                {
                    Console.WriteLine("Exception Write: " + exinner.Message);
                }
            }

        }

        public bool AcceptAllCertifications(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certification, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
        {
            return true;
        }


What I have tried:

I had tried with trial and error method from many articles on the web but not fruitful.
Posted
Updated 28-Oct-18 23:05pm
v3

1 solution

There could be any number of reasons this is happening.

0) make sure your cert5ificates are valid.

1) make sure IIS is configured properly.

3) modify your code if the first two things I mentioned are okay. This might help, but if your certs aren't valid or IIS is improperly configured, this will simply hide the real problem.

C#
ServicePointManager.Expect100Continue = true;
ServicePointManager.DefaultConnectionLimit = 9999;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;


Google/Bing is your friend.

The request was aborted: Could not create SSL/TLS secure channel. - Bing[^]
 
Share this answer
 
v2
Comments
Richard Deeming 26-Oct-18 8:28am    
#3 would be my bet - the server only supports TLS 1.2, which isn't enabled by default in .NET applications prior to 4.7.

For .NET 4.5, it's possible to turn it on via the registry[^]; but turning it on in code is a safer option.

Transport Layer Security (TLS) best practices with the .NET Framework[^]
ali sbeiti 6-Jul-19 3:44am    
Thanks dear! I changed project's .net framework to 4.6 and added the above code and it worked.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900