Ssl error

Hi ,

im trying to download a file from a server and im getiing this error, can someone help with this

Download from https://accpayment.Embedded/002103000001/Program/GHIElectronics.NETMF.Glide.zip
#### Exception System.Net.Sockets.SocketException - CLR_E_FAIL (1) ####
#### Message:
#### Microsoft.SPOT.Net.Security.SslNative::SecureRead [IP: 0000] ####
#### Microsoft.SPOT.Net.Security.SslStream::Read [IP: 0040] ####
#### System.Net.InputNetworkStreamWrapper::RefillInternalBuffer [IP: 0038] ####
#### System.Net.InputNetworkStreamWrapper::Read_HTTP_Line [IP: 004b] ####
#### System.Net.HttpWebRequest::ParseHTTPResponse [IP: 002e] ####
#### System.Net.HttpWebRequest::GetResponse [IP: 0035] ####
#### TriniT.KlmLounge.Program.Interface.ServerUtility::GetHttpBytes [IP: 0055] ####
#### TriniT.Klm.Loader.Program::DownloadAndLoadAssembly [IP: 004b] ####
#### TriniT.Klm.Loader.Program::Execute [IP: 00b8] ####
#### TriniT.Preload.Program.Program::LoadAndExecuteAssembly [IP: 0084] ####
#### TriniT.Preload.Program.Program::ProgramStarted [IP: 003c] ####
#### SocketException ErrorCode = -1
#### SocketException ErrorCode = -1
A first chance exception of type ‘System.Net.Sockets.SocketException’ occurred in Microsoft.SPOT.Net.Security.dll
#### SocketException ErrorCode = -1
#### SocketException ErrorCode = -1
#### Exception System.Net.WebException - 0x00000000 (1) ####
#### Message:
#### System.Net.HttpWebRequest::GetResponse [IP: 00c8] ####
#### TriniT.KlmLounge.Program.Interface.ServerUtility::GetHttpBytes [IP: 0055] ####
#### TriniT.Klm.Loader.Program::DownloadAndLoadAssembly [IP: 004b] ####
#### TriniT.Klm.Loader.Program::Execute [IP: 00b8] ####
#### TriniT.Preload.Program.Program::LoadAndExecuteAssembly [IP: 0084] ####
#### TriniT.Preload.Program.Program::ProgramStarted [IP: 003c] ####

Did you use MFDeploy to update the SSL seed?

You can do so from MFDeploy by clicking “Target->Manage Device Keys->Update SSL Seed”


        /// <summary>
        /// Prints the HTTP Web page from the given URL and status data while 
        /// receiving the page.
        /// </summary>
        /// <param name="url">The URL of the page to print.</param>
        /// <param name="length"> </param>
        public static byte[] GetHttpBytes(string url, out int length)
        {
            const int requestTimeout = 5000;
            const int bufferSize = 8192;
            length = 0;
            const int threadWait = 10;
            byte[] page = new byte[0];
            if (page.Length > 0)
            {
                return page;
            }
            //int totalBytes = 0;
            try
            {
                WebResponse resp = null;

                try
                {
                    // Create an HTTP Web request.
                    HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
                    // Assign the certificates. The value must not be null if the
                    // connection is HTTPS.
                    if (request != null)
                    {
                        //request.HttpsAuthentCerts = caCerts;

                        // Set request.KeepAlive to use a persistent connection. 
                        request.KeepAlive = false; // true;
                        request.Timeout = requestTimeout;
                        request.ReadWriteTimeout = requestTimeout;

                        // Get a response from the server.
                        try
                        {
                            resp = request.GetResponse();
                        }
                        catch (Exception e)
                        {
                            DoDebug("Exception in HttpWebRequest.GetResponse(): " + e.Message);
                        }

                        // Get the network response stream to read the page data.
                        if (resp != null)
                        {
                            Stream respStream = resp.GetResponseStream();
                            byte[] byteData = new byte[bufferSize];
                            //char[] charData = new char[bufferSize];
                            int bytesRead;

                            // allow 5 seconds for reading the stream
                            respStream.ReadTimeout = requestTimeout;

                            // If we know the content length, read exactly that amount of 
                            // data; otherwise, read until there is nothing left to read.
                            if (resp.ContentLength !=
                                -1)
                            {
                                for (int dataRem = (int) resp.ContentLength; dataRem > 0;)
                                {
                                    Thread.Sleep(threadWait);
                                    bytesRead =
                                        respStream.Read(byteData, 0, byteData.Length);
                                    //DoDebug("Read: " + bytesRead);
                                    length += bytesRead;
                                    if (bytesRead == 0)
                                    {
                                        DoDebug("Error: Received " +
                                                (resp.ContentLength - dataRem) + " Out of " +
                                                resp.ContentLength);
                                        break;
                                    }
                                    dataRem -= bytesRead;


                                    byte[] pageNew = new byte[page.Length + bytesRead];
                                    Array.Copy(page, pageNew, page.Length);
                                    Array.Copy(byteData, 0, pageNew, page.Length, bytesRead);
                                    page = pageNew;

                                    /*// Convert from bytes to chars, and add to the page 
                                    // string.
                                    int byteUsed, charUsed;
                                    bool completed;
                                    //totalBytes += bytesRead;
                                    utf8Decoder.Convert(byteData, 0, bytesRead, charData, 0,
                                                        bytesRead, true, out byteUsed, out charUsed,
                                                        out completed);
                                    page = page + new String(charData, 0, charUsed);*/

                                    // Display the page download status.
                                    //DoDebug("Bytes Read Now: " + bytesRead +
                                    //          " Total: " + totalBytes);
                                }

                                //page = new String(
                                //    Encoding.UTF8.GetChars(byteData));
                            }
                            else
                            {
                                // Read until the end of the data is reached.
                                while (true)
                                {
                                    // If the Read method times out, it throws an exception, 
                                    // which is expected for Keep-Alive streams because the 
                                    // connection isn't terminated.
                                    try
                                    {
                                        Thread.Sleep(threadWait);
                                        bytesRead =
                                            respStream.Read(byteData, 0, byteData.Length);
                                        //DoDebug("Read: " + bytesRead);
                                        length += bytesRead;
                                    }
                                    catch (Exception)
                                    {
                                        bytesRead = 0;
                                    }

                                    // Zero bytes indicates the connection has been closed 
                                    // by the server.
                                    if (bytesRead == 0)
                                    {
                                        break;
                                    }

                                    //int byteUsed, charUsed;
                                    //bool completed;
                                    //totalBytes += bytesRead;
                                    //utf8Decoder.Convert(byteData, 0, bytesRead, charData, 0,
                                    //                    bytesRead, true, out byteUsed, out charUsed,
                                    //                    out completed);
                                    byte[] pageNew = new byte[page.Length + bytesRead];
                                    Array.Copy(page, pageNew, page.Length);
                                    Array.Copy(byteData, 0, pageNew, page.Length, bytesRead);
                                    page = pageNew;
                                    //page = page + new String(charData, 0, charUsed);

                                    // Display page download status.
                                    //DoDebug("Bytes Read Now: " + bytesRead +
                                    //" Total: " + totalBytes);
                                }

                                //DoDebug("Total bytes downloaded in message body : "
                                //            + totalBytes);
                            }
                        }
                    }
                }
                finally
                {
                    // Close the response stream.  For Keep-Alive streams, the 
                    // stream will remain open and will be pushed into the unused 
                    // stream list.
                    if (resp != null)
                    {
                        resp.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                DoDebug("Exception in HttpWebRequest.GetResponse(): " + ex.Message);
            }
            return page;
        }

I tried to download the files from HTTP and it worked fine

Yes i did update the SSL seed.

Did anybody manage to solve this issue?
For me https works when the response has no body, otherwise I usually (but not always) get the same exception at this line


resp = request.GetResponse();

@ RoSchmi - SSL reads tend to throw exceptions a lot. The solution is to retry the read:


var read = 0;

while (moreData) {
    try {
        read = connection.Read(...);
    }
    catch {
    }
}

We have not tested this with the HTTP classes. Since they handle the reads for you, you may have to implement your own HTTP handler using the low level socket interface.

@ John - thanks, perhaps I’l try when I get some time. As a first solution in my Codeshare Azure Storage Table Client I do the send operations with https, which seems to work fine as only headers are returned. For the query commands where I get more data in the response body (and so exceptions) I use http. I think this sufficient security for my demands.