HTTPS Exception System.Net.Sockets.SocketException - CLR_E_FAIL (1)

i’m trying to get https communication working, currently trying to request the google search page.

i’m using this code:


 using (var request = HttpWebRequest.Create(url) as HttpWebRequest)
            {
                request.HttpsAuthentCerts = new X509Certificate[] { new X509Certificate(certificate) };
                request.KeepAlive = false;

                using (var response = request.GetResponse())  //location of the error

i have downloaded the certificate as base64 and get the following error from the debugger:
[b]

Exception System.Net.Sockets.SocketException - CLR_E_FAIL (1)

#### Message: 
#### Microsoft.SPOT.Net.Security.SslNative::SecureConnect [IP: 0000] ####
#### Microsoft.SPOT.Net.Security.SslStream::Authenticate [IP: 0060] ####
#### Microsoft.SPOT.Net.Security.SslStream::AuthenticateAsClient [IP: 000c] ####
#### System.Net.HttpWebRequest::EstablishConnection [IP: 0247] ####
#### System.Net.HttpWebRequest::SubmitRequest [IP: 0019] ####
#### System.Net.HttpWebRequest::GetResponse [IP: 000c] ####
#### MFConsoleApplication1.Program::DownloadOverSsl [IP: 0024] ####
#### MFConsoleApplication1.Program::Main [IP: 00c2] ####
#### SocketException ErrorCode = 1
#### SocketException ErrorCode = 1
#### SocketException ErrorCode = 1
#### SocketException ErrorCode = 1
#### SocketException ErrorCode = 1
#### SocketException ErrorCode = 1
#### Exception System.Net.WebException - 0x00000000 (1) ####
#### Message: 
#### System.Net.HttpWebRequest::GetResponse [IP: 00c8] ####
#### MFConsoleApplication1.Program::DownloadOverSsl [IP: 0024] ####
#### MFConsoleApplication1.Program::Main [IP: 00c2] ####

Exception thrown: ‘System.Net.WebException’ in System.Http.dll
An unhandled exception of type ‘System.Net.WebException’ occurred in System.Http.dll [/b]

Hi, did you set the ssl-seed? Did you read Support, Net Micro Framework, Documents, Networking, SSL? In some cases HttpWebRequest does not work. You can have a look on my Codeshare project Gadgetee sending e-mail via SparkPost, where I use Socket and Sslstream Classes

I have set the ssl-seed via MFDeploy, read the documentation and used the httpwebrequest just before the call to the function for ssl to load the http version of the google search page

@ Danjal - I’d like to try if I can get it working here. Can you please tell me which mainbord and network module you are using, show the initialization of the Network Interface and the other details that I can rebuild your application here.

@ RoSchmi - I’m using the G120E development board with the RS9110 wifi module. the complete application is:


public class Program
    {
private static WiFiRS9110 netif;

public static void Main()
        {
            NetworkChange.NetworkAvailabilityChanged += NetworkChange_NetworkAvailabilityChanged;
            NetworkChange.NetworkAddressChanged += NetworkChange_NetworkAddressChanged;

            netif = new WiFiRS9110(GHI.Pins.G120E.SpiBus.Spi2, GHI.Pins.G120E.Gpio.P3_30, GHI.Pins.G120E.Gpio.P2_30, GHI.Pins.G120E.Gpio.P4_31);
            netif.Open();
            netif.EnableDhcp();
            netif.EnableDynamicDns();
            var scanResult = netif.Scan();
            bool connected = false;

            for (int i = 0; i < scanResult.Length; i++)
            {
                Debug.Print(scanResult[i].Ssid);
                if (scanResult[i].Ssid.ToLower() == "network ssid")
                {
                    netif.Join(scanResult[i].Ssid, "password"); //ssid, password
                    connected = true;
                }
            }

            if (netif.ActiveNetwork != null)
            {
                Debug.Print(netif.ActiveNetwork.Ssid);
                while (netif.IPAddress == "0.0.0.0")
                {
                    Debug.Print("Waiting for DHCP");
                    Thread.Sleep(250);
                }
                Debug.Print(netif.IPAddress);
            }
            else
            {
                Debug.Print("Could not connect to network");
            }
          DownloadOverSsl("https://www.google.nl", Resources.GetBytes(Resources.BinaryResources.googlecert));
        }

 static void DownloadOverSsl(string url, byte[] certificate)
        {
            using (var request = HttpWebRequest.Create(url) as HttpWebRequest)
            {
                request.HttpsAuthentCerts = new X509Certificate[] { new X509Certificate(certificate) };
                request.KeepAlive = false;

                using (var response = request.GetResponse())
                {
                    using (var stream = response.GetResponseStream())
                    {
                        var result = string.Empty;
                        var buffer = new byte[4096];
                        var read = 0;

                        stream.ReadTimeout = 5000;

                        for (var left = response.ContentLength; left > 0;)
                        {
                            Thread.Sleep(1000);

                            try
                            {
                                read = stream.Read(buffer, 0, buffer.Length);
                            }
                            catch
                            {
                                continue;
                            }

                            left -= read;

                            result += new string(Encoding.UTF8.GetChars(buffer, 0, read));
                        }

                        Debug.Print(result);
                    }
                }
            }
        }
}

@ Danjal - o.k., thanks. I’m just trying to set it up on a Cobra III with ENC28 module.

@ Danjal - Please have a look on the CodeShare project I just posted. Would appreciate a feedback if it works for you
https://www.ghielectronics.com/community/codeshare/entry/1095

1 Like

@ RoSchmi - Thankyou, i will give it a try as soon as possible (running into some problems at the moment so it might take a few days) and let you know if it works for me as soon as possible

@ RoSchmi - after modifying the project a bit for the wifi module it works for me, thankyou for sharing the code

@ Danjal - did you mean that my code worked or yours? Did you see my post, that I noticed a bug in my code: The code does not utilize the certificate. If I propagate the certificate to the SslStream method, I get the same exception as you. But perhaps the data are ssl encrypted though. I want to look a liitle bit deeper in this issue the next days.

@ RoSchmi - that was indeed what i meant