javax.net.ssl.SSLHandshakeException:握手在Android 6.0及更高版本上失败了!我正在使用org.apache.http.conn.ssl.SSLSocketFactory

时间:2016-05-16 11:43:57

标签: android sslv3

我正在使用自定义SSLSocketFactory类,它从已启用的协议中删除SSLv3。我无法弄清楚如何将Truststore附加到设置为HttpsURLConnection的套接字工厂。我到目前为止看到的所有例子都使用org.apache.http.conn.ssl.SSLSocketFactory。

public static void getNewHttpClient() {
    try {
        KeyStore trustStore = KeyStore.getInstance(KeyStore
                .getDefaultType());
        trustStore.load(null, null);
        CertificateFactory cf = CertificateFactory.getInstance("X.509");

        InputStream caInput = BTApplication.getContextApplication()
                .getResources().openRawResource(R.raw.ca);
        Certificate ca;
        try {
            ca = cf.generateCertificate(caInput);
        } finally {
            caInput.close();
        }

        trustStore.setCertificateEntry("ca", ca);
        SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
        // sf.
        sf.setHostnameVerifier(SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);

        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);

        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory
                .getSocketFactory(), 80));
        registry.register(new Scheme("https", sf, 443));

        ClientConnectionManager ccm = new ThreadSafeClientConnManager(
                params, registry);
        // return
        client = new DefaultHttpClient(ccm, params);

        setDefaultRetryHandler();
    } catch (Exception e) {

    }
}

0 个答案:

没有答案