SSL 不适用于 Android 2.2(仅适用于 2.3) [英] SSL not working on Android 2.2 (only in 2.3)

查看:49
本文介绍了SSL 不适用于 Android 2.2(仅适用于 2.3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当 httpsURLConnection.getInputStream() 被调用时,我在 LogCat 上得到这个

I'm getting this on LogCat when httpsURLConnection.getInputStream() is called

SSL 握手失败:SSL 库中的失败,通常是协议错误错误:14094412:SSL 例程:SSL3_READ_BYTES:sslv3 警报错误证书(外部/openssl/ssl/s3_pkt.c:1127 0x29eb40:0x00000003)

SSL handshake failure: Failure in SSL library, usually a protocol error error:14094412:SSL routines:SSL3_READ_BYTES:sslv3 alert bad certificate (external/openssl/ssl/s3_pkt.c:1127 0x29eb40:0x00000003)

我已经在 Andorid 2.3 上测试过它并且运行良好.

I have tested it on Andorid 2.3 and it works nicely.

我的服务器需要客户端认证!可能FROYO不支持这种握手方式……不知道……

My server requires client authentication! Maybe FROYO does not support this kind of handshake... I don't know...

我也尝试使用 httpclient.在任何情况下都失败...

private void process() throws Exception {

    char[] pass = "clientpass".toCharArray();

    InputStream ksStream = getAssets().open("clientKeyStore.bks");
    KeyStore keyStore = KeyStore.getInstance("BKS");
    keyStore.load(ksStream, pass);
    KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    kmf.init(keyStore, pass);
    ksStream.close();

    X509TrustManager[] tm = new X509TrustManager[] { new X509TrustManager() {
        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }

        public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }

        public X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[0];
        }
    } };

    SSLContext context = SSLContext.getInstance("TLS");
    context.init(kmf.getKeyManagers(), tm, null);
    HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
    HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
        public boolean verify(String hostname, SSLSession session) {
            return true;
        }
    });

    URL url = new URL("https://192.168.2.101:8443/RestTomcat/resources/veiculos/KKK1234");
    HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
    BufferedReader br = new BufferedReader(new InputStreamReader(httpsURLConnection.getInputStream()));
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = br.readLine()) != null)
        sb.append(line + "
");
    br.close();

    Log.e("OUTPUT", sb.toString());
    httpsURLConnection.disconnect();
}

推荐答案

确保 2.2 设备上的日期、时间和时区设置正确.

Make sure the date, time and timezone settings are correct on the 2.2 device.

这篇关于SSL 不适用于 Android 2.2(仅适用于 2.3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆