Java HttpClient SSL:初始化默认SSL上下文失败

时间:2016-03-03 22:09:23

标签: java ssl

我正在尝试执行http请求。但是,在执行时出现oshiyuka下面。我想由于证书问题,无法创建SSLContentFactory。谁曾见过这样的事情? JRE 1.5

Loaded class org.apache.http.conn.scheme.LayeredSchemeSocketFactory
Loaded class org.apache.http.conn.scheme.SocketFactory
Loaded class org.apache.http.conn.scheme.LayeredSocketFactory 
Loaded class org.apache.http.conn.ssl.SSLSocketFactory
Loaded class org.apache.http.conn.ConnectTimeoutException 
Loaded class org.apache.http.conn.ssl.X509HostnameVerifier
Loaded class org.apache.http.conn.ssl.AbstractVerifier 
Loaded class org.apache.http.conn.ssl.AllowAllHostnameVerifier 
Loaded class org.apache.http.conn.ssl.BrowserCompatHostnameVerifier 
Loaded class org.apache.http.conn.ssl.StrictHostnameVerifier
Loaded class org.apache.http.impl.conn.SchemeRegistryFactory 
Loaded class org.apache.http.conn.scheme.SchemeRegistry
Loaded class org.apache.http.conn.scheme.Scheme 
Loaded class org.apache.http.conn.scheme.PlainSocketFactory

*** START APPLICATION TRACE ***

Defaultjava.lang.IllegalStateException: Failure initializing default SSL context

*** END APPLICATION TRACE ***

清单:

private static DefaultHttpClient createHttpClient()
        throws NoSuchAlgorithmException, KeyManagementException {

    DefaultHttpClient defaultHttpClient = new DefaultHttpClient();

    defaultHttpClient.getParams().setParameter(
            ConnRoutePNames.DEFAULT_PROXY, proxy);

    defaultHttpClient = makeTrustfulHttpClient(defaultHttpClient);
    defaultHttpClient.addRequestInterceptor(
            new DiadocPreemptiveAuthRequestInterceptor(), 0);
    return defaultHttpClient;
}

private static DefaultHttpClient makeTrustfulHttpClient(HttpClient base)
        throws NoSuchAlgorithmException, KeyManagementException {



    SSLSocketFactory ssf = getTrustfulSslSocketFactory();
    ClientConnectionManager ccm = base.getConnectionManager();
    ccm.getSchemeRegistry().register(new Scheme("https", 443, ssf));
    return new DefaultHttpClient(ccm, base.getParams());
}

private static SSLSocketFactory getTrustfulSslSocketFactory()
        throws NoSuchAlgorithmException, KeyManagementException {


    SSLContext ctx = SSLContext.getInstance("TLS");
    X509TrustManager tm = new X509TrustManager() {
        public void checkClientTrusted(X509Certificate[] xcs, String string)
                throws CertificateException {
        }

        public void checkServerTrusted(X509Certificate[] xcs, String string)
                throws CertificateException {
        }

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

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


    return new SSLSocketFactory(ctx,
            SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);


}

1 个答案:

答案 0 :(得分:0)

解决方案:

private SecureConnectionFactory makeSecureConnectionFactory()
        throws NoSuchAlgorithmException, KeyManagementException {


    return SecureConnectionFactory.getDefault();
}