java - httpclient https SSLPeerUnverifiedException 异常
伊谢尔伦
伊谢尔伦 2017-04-17 16:50:57
0
0
826

如下代码,为什么还会发生这个异常javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated

   private final static int SO_TIME_OUT = 10000; // 10s
    private final static int CON_TIME_OUT = 10000; // 10s
    private static HttpClient httpClient = null;
    static {
        PoolingClientConnectionManager connManager = new PoolingClientConnectionManager();
        connManager.setMaxTotal(20);
        connManager.setDefaultMaxPerRoute(2);
        httpClient = new DefaultHttpClient(connManager);
        HttpParams httpParams = httpClient.getParams();
        /* 从连接池中取连接的超时时间 */
      //  HttpConnectionParams.setLinger(httpParams, 10000);
        HttpConnectionParams.setStaleCheckingEnabled(httpParams, true);
        HttpConnectionParams.setConnectionTimeout(httpParams, CON_TIME_OUT);
        HttpConnectionParams.setSoTimeout(httpParams, SO_TIME_OUT);

        /* ssl 配置 */
        TrustManager easyTrustManager = new X509TrustManager() {
            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
            
            @Override
            public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                logger.info(authType);
            }
            
            @Override
            public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            }
        };
        SSLContext sslcontext = null;
        try {
            sslcontext = SSLContext.getInstance("TLS");
            sslcontext.init(null, new TrustManager[] {easyTrustManager}, new SecureRandom());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        }
        
        SSLSocketFactory sf = new SSLSocketFactory(sslcontext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

        Scheme schs = new Scheme("https", 443, sf);
        httpClient.getConnectionManager().getSchemeRegistry().register(schs);
        
        /*monitor thread*/
        new Thread(){
            public void run(){
                while(true){
                    ThreadUtils.quietlySleep(2000L);
                    httpClient.getConnectionManager().closeExpiredConnections();
                    httpClient.getConnectionManager().closeIdleConnections(10, TimeUnit.SECONDS);
                }
            }
        }.start();
    }

google 结果说自定义TrustManager,可以避免这个异常,我的程序还是会遇到这个异常,而且遇到后就无法恢复,一直报这个异常

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

全員に返信(0)
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート