如何使用Apache客户端接受过期的ssl证书?

时间:2012-01-01 17:45:39

标签: java android ssl

我正在尝试使DefaultHttpClient()使用过期的SSL证书。

Android API 2.2

因为这一行而无法编译:

SSLSocketFactory sf = new SSLSocketFactory(sslContext);

错误:The constructor SSLSocketFactory(SSLContext)未定义

我做错了什么?

    import javax.net.ssl.SSLContext;
    import javax.net.ssl.TrustManager;
    import javax.net.ssl.X509TrustManager;
    import org.apache.http.conn.ssl.SSLSocketFactory;
    {...}

        SSLContext sslContext = SSLContext.getInstance("SSL");

        // set up a TrustManager that trusts everything
        sslContext.init(null, new TrustManager[] { new X509TrustManager() {
                    public X509Certificate[] getAcceptedIssuers() {
                            System.out.println("getAcceptedIssuers =============");
                            return null;
                    }

                    public void checkClientTrusted(X509Certificate[] certs,
                                    String authType) {
                            System.out.println("checkClientTrusted =============");
                    }

                    public void checkServerTrusted(X509Certificate[] certs,
                                    String authType) {
                            System.out.println("checkServerTrusted =============");
                    }
        } }, new SecureRandom());

        SSLSocketFactory sf = new SSLSocketFactory(sslContext);
        Scheme httpsScheme = new Scheme("https", sf, 443);
        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(httpsScheme);

        HttpParams params = new BasicHttpParams();
        ClientConnectionManager cm = new SingleClientConnManager(params, schemeRegistry);

        //DefaultHttpClient httpclient = new DefaultHttpClient();
        DefaultHttpClient httpclient = new DefaultHttpClient(cm, params);

2 个答案:

答案 0 :(得分:1)

查看SSLSocketFactory的文档,似乎没有构造函数:

SSLSocketFactory(javax.net.ssl.SSLContext)

可用的构造函数是:

SSLSocketFactory(String algorithm, KeyStore keystore, String keystorePassword, KeyStore truststore, SecureRandom random, HostNameResolver nameResolver)
SSLSocketFactory(KeyStore keystore, String keystorePassword, KeyStore truststore)
SSLSocketFactory(KeyStore keystore, String keystorePassword)
SSLSocketFactory(KeyStore truststore)

我在这里错过了什么吗?

另见javax.net.ssl.SSLContext

答案 1 :(得分:0)

你没有做错任何事(除了使用一些标准的Java代码)。

似乎Apache SSLSocketFactory类的Android实现没有实现原始Apache SSLSocketFactory类的所有构造函数

你只需要即兴发挥。