Android SSLException

时间:2016-12-13 09:30:57

标签: android sslexception

在Android设备中下载文件时,我收到以下错误。我无法找到根本原因,不确定是否有人遇到此网络 错误

javax.net.ssl.SSLException: Read error: ssl=0xd1823640: I/O error during system call, Connection reset by peer
     at com.android.org.conscrypt.NativeCrypto.SSL_read(Native Method)
     at com.android.org.conscrypt.OpenSSLSocketImpl$SSLInputStream.read(OpenSSLSocketImpl.java:758)
     at com.android.okhttp.okio.Okio$2.read(Okio.java:136)
     at com.android.okhttp.okio.AsyncTimeout$2.read(AsyncTimeout.java:211)
     at com.android.okhttp.okio.RealBufferedSource.read(RealBufferedSource.java:50)
     at com.android.okhttp.internal.http.HttpConnection$FixedLengthSource.read(HttpConnection.java:418)
     at com.android.okhttp.okio.RealBufferedSource$1.read(RealBufferedSource.java:371)
     at java.io.InputStream.read(InputStream.java:101)
     at com.sampleapp.Hello.doInBackground(AcrService.java:6751)
     at com.sampleapp.Hello.doInBackground(AcrService.java:6696)
     at android.os.AsyncTask$2.call(AsyncTask.java:305)
     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
     at java.lang.Thread.run(Thread.java:761)

以下是我用于HTTP URL连接的代码

try {
                File dir = new File(getExternalFilesDir(null).getAbsolutePath() + "/file");
                if (dir.exists() == false) {
                    dir.mkdirs();
                }
                URL url = new URL(urlLink );

                File outputFile = new File(dir, fileName);
                /* Open a connection to that URL. */
                try
                {
                    Log.d(TAG, "Initiating File Download");
                    SSLContext sslcontext = SSLContext.getInstance("TLSv1");

                    sslcontext.init(null,
                            null,
                            null);
                    SSLSocketFactory NoSSLv3Factory = new NoSSLv3SocketFactory(sslcontext.getSocketFactory());

                    HttpsURLConnection.setDefaultSSLSocketFactory(NoSSLv3Factory);

                    HttpsURLConnection c = (HttpsURLConnection) url.openConnection();
                    c.setRequestMethod("GET");
                    c.setDoOutput(true);
                    c.setConnectTimeout(15000);
                    c.connect();


                    FileOutputStream fos = new FileOutputStream(outputFile);
                    InputStream is = c.getInputStream();

                    byte[] buffer = new byte[4096];
                    int len1 = 0;

                    while ((len1 = is.read(buffer)) != -1)
                    {
                        fos.write(buffer, 0, len1);
                    }

                    fos.close();
                    is.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }catch (Exception e)
                {
                    e.printStackTrace();
                }


            } catch (Exception e) {
                e.printStackTrace();
            }

0 个答案:

没有答案