httpclient https SSLPeerUnverifiedException 异常

如下代码,为什么还会发生这个异常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,可以避免这个异常,我的程序还是会遇到这个异常,而且遇到后就无法恢复,一直报这个异常

阅读 3.5k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏