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

如下代码,为什么还会发生这个异常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)
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!