javax.net.ssl​​.SSLException:读取错误:SSL = 0x56e63588:系统调用,连接重置由对等时I / O错误 [英] javax.net.ssl.SSLException: Read error: ssl=0x56e63588: I/O error during system call, Connection reset by peer

查看:2637
本文介绍了javax.net.ssl​​.SSLException:读取错误:SSL = 0x56e63588:系统调用,连接重置由对等时I / O错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用使用 org.apache.http.client.HttpClient 来从过去的2年,其一直致力于罚款HTTP服务器下载一些数据。

最近,我们切换到HTTPS进行一些安全reasons.This也工作正常与慢数据连接(2G)我下载与以下堆栈跟踪中断WiFi和高速移动数据(3G),但最常见。

  javax.net.ssl​​.SSLException:读取错误:SSL = 0x56e63588:系统调用,连接重置由对等时I / O错误
    在com.android.org.conscrypt.NativeCrypto.SSL_read(本机方法)
    在com.android.org.conscrypt.OpenSSLSocketImpl $ SSLInputStream.read(OpenSSLSocketImpl.java:690)
    在org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103)
    在org.apache.http.impl.io.AbstractSessionInputBuffer.read(AbstractSessionInputBuffer.java:120)
    在org.apache.http.impl.io.ChunkedInputStream.read(ChunkedInputStream.java:131)
    在org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:140)
    在java.io.FilterInputStream.read(FilterInputStream.java:114)

这是我的code的样子:

  HttpPost httpPost =新HttpPost(URL);
                UrlEn codedFormEntity p_entity =新UrlEn codedFormEntity(
                        参数,可以HTTP.UTF_8);
                httpPost.setEntity(p_entity);
                响应= httpClient.execute(httpPost);
                DataInputStream以在=新DataInputStream所(response.getEntity()的getContent());
                串ST =;
                结果=新的矢量<串GT;();
                而((ST =的readLine(中))!= NULL){
                result.addElement(ST);
         }


解决方案

试试这个:

 包com.telpoo.frame.net;进口java.io.IOException异常;
进口的java.net.Socket;
进口的java.net.UnknownHostException;
进口java.security.KeyManagementException;
进口java.security.KeyStore中;
进口java.security.KeyStoreException;
进口java.security.NoSuchAlgorithmException;
进口java.security.UnrecoverableKeyException;
进口java.security.cert.CertificateException;
进口java.security.cert.X509Certificate;进口javax.net.ssl​​.SSLContext;
进口javax.net.ssl​​.TrustManager;
进口javax.net.ssl​​.X509TrustManager;进口org.apache.http.HttpVersion;
进口org.apache.http.client.HttpClient;
进口org.apache.http.conn.ClientConnectionManager;
进口org.apache.http.conn.scheme.PlainSocketFactory;
进口org.apache.http.conn.scheme.Scheme;
进口org.apache.http.conn.scheme.SchemeRegistry;
进口org.apache.http.conn.ssl.SSLSocketFactory;
进口org.apache.http.impl.client.DefaultHttpClient;
进口org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
进口org.apache.http.params.BasicHttpParams;
进口org.apache.http.params.HttpParams;
进口org.apache.http.params.HttpProtocolParams;
进口org.apache.http.protocol.HTTP;公共类MySSLSocketFactory扩展的SSLSocketFactory {
    的SSLContext的SSLContext = SSLContext.getInstance(TLS);    公共MySSLSocketFactory(密钥库信任)抛出抛出:NoSuchAlgorithmException,KeyManagementException,KeyStoreException,UnrecoverableKeyException {
        超(信任);        的TrustManager TM =新X509TrustManager(){
            公共无效checkClientTrusted(x509证书[]链,字符串的authType)抛出CertificateException {
            }            公共无效checkServerTrusted(x509证书[]链,字符串的authType)抛出CertificateException {
            }            公共x509证书[] getAcceptedIssuers()为{
                返回null;
            }
        };        sslContext.init(空,新的TrustManager [] {} TM,NULL);
    }    @覆盖
    公共插座的createSocket(Socket套接字,字符串主机,端口INT,布尔自动关闭)抛出IOException异常,{UnknownHostException异常
        返回sslContext.getSocketFactory()的createSocket(插座,主机,端口自动关闭)。
    }    @覆盖
    公共插座的createSocket()抛出IOException
        返回sslContext.getSocketFactory()的createSocket()。
    }    公共静态HttpClient的getNewHttpClient(){
        尝试{
            密钥库的trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
            trustStore.load(NULL,NULL);            SSLSocketFactory的SF =新MySSLSocketFactory(的trustStore);
            sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);            的HttpParams PARAMS =新BasicHttpParams();
            HttpProtocolParams.setVersion(参数,可以HttpVersion.HTTP_1_1);
            HttpProtocolParams.setContentCharset(参数,可以HTTP.UTF_8);            SchemeRegistry注册表=新SchemeRegistry();
            registry.register(新计划(HTTP,PlainSocketFactory.getSocketFactory(),80));
            registry.register(新计划(https开头,SF,443));            ClientConnectionManager CCM =新ThreadSafeClientConnManager(参数,可以登记);            返回新DefaultHttpClient(CCM,则params);
        }赶上(例外五){
            返回新DefaultHttpClient();
        }
    }}

和使用它:

  URL myUrl =新的URL(网址);
            HttpClient的客户= MySSLSocketFactory.getNewHttpClient(); // myHttpClient();            HttpConnectionParams.setConnectionTimeout(client.getParams(),connectTimeout);    HttpPost后=新HttpPost(myUrl.toURI());

I have been using using org.apache.http.client.HttpClient to download some data from a HTTP server for past 2 years and its been working fine.

Recently we switched to HTTPS for some security reasons.This is also working fine with WiFi and High speed mobile data(3G).But most often with slow data connection(2G) My download interrupted with following stack trace.

 javax.net.ssl.SSLException: Read error: ssl=0x56e63588: 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:690)
    at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103)
    at org.apache.http.impl.io.AbstractSessionInputBuffer.read(AbstractSessionInputBuffer.java:120)
    at org.apache.http.impl.io.ChunkedInputStream.read(ChunkedInputStream.java:131)
    at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:140)
    at java.io.FilterInputStream.read(FilterInputStream.java:114)

This is how my Code Looks like :

                HttpPost httpPost = new HttpPost(url);
                UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(
                        params, HTTP.UTF_8);
                httpPost.setEntity(p_entity);
                response = httpClient.execute(httpPost);
                DataInputStream in = new               DataInputStream(response.getEntity().getContent());
                String st = "";
                result = new Vector<String>();
                while ((st = readLine(in)) != null) {
                result.addElement(st);
         }

解决方案

Try this:

 package com.telpoo.frame.net;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;

public class MySSLSocketFactory extends SSLSocketFactory {
    SSLContext sslContext = SSLContext.getInstance("TLS");

    public MySSLSocketFactory(KeyStore truststore) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {
        super(truststore);

        TrustManager tm = new X509TrustManager() {
            public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            }

            public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            }

            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        };

        sslContext.init(null, new TrustManager[] { tm }, null);
    }

    @Override
    public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException, UnknownHostException {
        return sslContext.getSocketFactory().createSocket(socket, host, port, autoClose);
    }

    @Override
    public Socket createSocket() throws IOException {
        return sslContext.getSocketFactory().createSocket();
    }

    public static  HttpClient getNewHttpClient() {
        try {
            KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
            trustStore.load(null, null);

            SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
            sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

            HttpParams params = new BasicHttpParams();
            HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
            HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);

            SchemeRegistry registry = new SchemeRegistry();
            registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
            registry.register(new Scheme("https", sf, 443));

            ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);

            return new DefaultHttpClient(ccm, params);
        } catch (Exception e) {
            return new DefaultHttpClient();
        }
    }

}

and use it:

URL myUrl = new URL(url);
            HttpClient client = MySSLSocketFactory.getNewHttpClient(); // myHttpClient();

            HttpConnectionParams.setConnectionTimeout(client.getParams(), connectTimeout);

    HttpPost post = new HttpPost(myUrl.toURI());

这篇关于javax.net.ssl​​.SSLException:读取错误:SSL = 0x56e63588:系统调用,连接重置由对等时I / O错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆