【问题标题】:Android 4.4.2 Issue : javax.net.ssl.SSLHandshakeException: Failure in SSL library, usually a protocol errorAndroid 4.4.2 问题:javax.net.ssl.SSLHandshakeException: SSL 库失败,通常是协议错误
【发布时间】:2019-01-14 04:27:04
【问题描述】:

javax.net.ssl.SSLHandshakeException:javax.net.ssl.SSLProtocolException:SSL 握手中止:ssl=0xb8437270:SSL 库失败,通常是协议错误

它只发生在 Android 版本 > 5.0

public OkHttpClient setSSLCertificate(OkHttpClient okHttpClient){
    try{
        // Create a trust manager that does not validate certificate chains
        final TrustManager[] trustAllCerts = new TrustManager[]{
                new X509TrustManager() {
                    @Override
                    public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
                    }

                    @Override
                    public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
                    }

                    @Override
                    public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                        return new java.security.cert.X509Certificate[]{};
                    }
                }
        };

        // Install the all-trusting trust manager
        final SSLContext sslContext = SSLContext.getInstance("SSL");
        sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
        // Create an ssl socket factory with our all-trusting manager
        final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();

        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.sslSocketFactory(sslSocketFactory, (X509TrustManager) trustAllCerts[0]);
        builder.hostnameVerifier(new HostnameVerifier() {
            @Override
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        });

        okHttpClient = builder.build();

    }catch (Exception e){
        e.printStackTrace();
    }

    return okHttpClient;
}

【问题讨论】:

标签: java android ssl


【解决方案1】:

只需添加

HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;

        DefaultHttpClient client = new DefaultHttpClient();

        SchemeRegistry registry = new SchemeRegistry();
        SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
        socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
        registry.register(new Scheme("https", socketFactory, 443));
        SingleClientConnManager mgr = new SingleClientConnManager(client.getParams(), registry);
        DefaultHttpClient httpClient = new DefaultHttpClient(mgr, client.getParams());

        HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);

【讨论】:

  • SSLSocketFactory 或 SocketFactory 类没有 getSocketFactory() 方法
  • compile('org.apache.httpcomponents:httpmime:4.3.6') { exclude module: 'httpclient' } compile 'org.apache.httpcomponents:httpclient-android:4.3.5' 你确定你在 build.gradle 中添加了这个
  • 我用过 compile 'org.apache.httpcomponents:httpcore:4.4.1' compile 'org.apache.httpcomponents:httpclient:4.5'
猜你喜欢
  • 2017-01-09
  • 2012-01-31
  • 2020-04-07
  • 2018-09-05
  • 2018-07-10
  • 1970-01-01
  • 2021-06-14
  • 2015-07-07
  • 2015-05-19
相关资源
最近更新 更多