HTTP使用javax.net.ssl.SSLPeerUnverifiedException获取HTTPS失败

时间:2013-07-16 12:12:15

标签: java ssl httpclient ssl-certificate http-get

过去几个月我一直在使用这种基础设施,而且工作正常。我曾经使用https点击谷歌并获得结果。突然间,我开始为HTTPS URL获取SSLPeerVerifiedException。我认为没有任何改变。任何人都可以指出原因和解决方案吗?

各种版本:

Apache-HttpComponents-HttpCore = 4.1;
Apache-HttpComponents-HttpClient = 4.1.1;
JDK                            = 1.6_64;

代码段:

public void execute(HttpContext httpContext, HttpUriRequest request, HttpHost proxy,
            Credentials proxyCredentials) throws IOException {
        HttpClient httpClient = getHttpClient(proxy, proxyCredentials, true, 
                configuration.getHttpConnectionTimeout(), configuration.getHttpSocketTimeout());
        httpClient.execute(request, httpContext);
    }

   /**
    * Default constructor
    */
    public HttpClientUtil() throws IOException {
            /*
             * A TrustManager which trusts every server certificates.
             */
            TrustManager tm = new X509TrustManager() {
                @Override
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
                @Override
                public void checkServerTrusted(X509Certificate[] chain, String authType)
                        throws CertificateException { }
                @Override
                public void checkClientTrusted(X509Certificate[] chain, String authType)
                        throws CertificateException { }
            };
            try {
                SSLContext context = SSLContext.getInstance("TLS");
                context.init(new KeyManager[0], new TrustManager[]{tm}, new SecureRandom());
                connectionManager = new ThreadSafeClientConnManager();
                connectionManager.setMaxTotal(Constant.HTTP_CONNECTION_POOL_MAX_TOTAL_CONNECTIONS);
                connectionManager.setDefaultMaxPerRoute(Constant.HTTP_CONNECTION_POOL_DEFAULT_MAX_CONNECTIONS_PER_ROUTE);
                connectionManager.getSchemeRegistry().register(new Scheme(Constant.PROTOCOL_HTTPS, Constant.HTTPS_DEFAULT_PORT, new SSLSocketFactory(context)));
            } catch (Exception e) {
                throw new IOException(e);
            }
        }

private HttpClient getHttpClient(HttpHost proxy, Credentials proxyCredentials, 
            boolean followRedirects, int connectionTimeout, int soTimeout) {
        DefaultHttpClient client = new DefaultHttpClient(connectionManager);
        client.addRequestInterceptor(requestAcceptEncoding);
        client.addResponseInterceptor(httpResponseMaskInterceptor);
        client.addResponseInterceptor(responseContentEncoding);
        HttpParams params = client.getParams();
        if (proxy != null) {
            params.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
            if (proxyCredentials != null) {
                client.getCredentialsProvider().setCredentials(
                        new AuthScope(proxy.getHostName(), proxy.getPort()), proxyCredentials);
            }
        }
        HttpProtocolParams.setUserAgent(params, configuration.getUserAgent());
        HttpConnectionParams.setConnectionTimeout(params, connectionTimeout);
        HttpConnectionParams.setSoTimeout(params, soTimeout);
        HttpClientParams.setRedirecting(params, followRedirects);
        if (followRedirects) {
            client.setRedirectStrategy(redirectStrategy);
        }
        return client;
    }

堆栈追踪:

Caused by: javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated
        at com.sun.net.ssl.internal.ssl.SSLSessionImpl.getPeerCertificates(SSLSessionImpl.java:352)
        at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:128)
        at org.apache.http.conn.ssl.SSLSocketFactory.createLayeredSocket(SSLSocketFactory.java:446)
        at org.apache.http.impl.conn.DefaultClientConnectionOperator.updateSecureConnection(DefaultClientConnectionOperator.java:200)
        at org.apache.http.impl.conn.AbstractPoolEntry.layerProtocol(AbstractPoolEntry.java:277)
        at org.apache.http.impl.conn.AbstractPooledConnAdapter.layerProtocol(AbstractPooledConnAdapter.java:142)
        at org.apache.http.impl.client.DefaultRequestDirector.establishRoute(DefaultRequestDirector.java:758)
        at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:565)
        at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:415)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:820)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:754)
        at com.amazon.mobius.httpproxy.util.HttpClientUtil.execute(HttpClientUtil.java:390)
        at com.amazon.mobius.httpproxy.ec2.RequestExecutor.executeViaEC2(RequestExecutor.java:267)
        ... 45 more

任何帮助都将不胜感激。

谢谢,

0 个答案:

没有答案