【发布时间】:2015-01-22 14:13:46
【问题描述】:
Android 5.0 Lollipop 中的 DefaultHttpClient 似乎已损坏。它无法设置与以前版本的 Android 成功设置的某些站点的连接。
例如我尝试连接到https://uralsg.megafon.ru
//Create httpclient like in https://stackoverflow.com/questions/18523784/ssl-tls-protocols-and-cipher-suites-with-the-androidhttpclient
HttpClient client = new DefaultHttpClient(manager, params);
HttpGet httpGet = new HttpGet("https://uralsg.megafon.ru");
HttpResponse client = httpclient.execute(httpGet);
此代码在 Android 2.3-4.4 中有效,但在 Android 5.0(设备和模拟器)上失败,并出现错误 Connection closed by peer。 当然这是可以理解的,因为 Android 5.0 试图用 TLSv1.2 和现代密码连接这个旧服务器,但它不支持它们。
好的,使用SSL/TLS protocols and cipher suites with the AndroidHttpClient 中的示例代码,我们将协议和密码限制为 TLSv1 和 SSL_RSA_WITH_RC4_128_MD5。现在它失败并出现不同的错误:
javax.net.ssl.SSLHandshakeException: Handshake failed
caused by
error:140943FC:SSL routines:SSL3_READ_BYTES:sslv3 alert bad record mac
(external/openssl/ssl/s3_pkt.c:1286 0x7f74c1ef16e0:0x00000003)
at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake
当然,这段代码在 Android 2.3-4.4 上运行流畅。
我用wireshark检查了流量:
302 4002.147873000 192.168.156.30 83.149.32.13 TLSv1 138 Client Hello
303 4002.185362000 83.149.32.13 192.168.156.30 TLSv1 133 Server Hello
304 4002.186700000 83.149.32.13 192.168.156.30 TLSv1 1244 Certificate
305 4002.186701000 83.149.32.13 192.168.156.30 TLSv1 63 Server Hello Done
307 4002.188117000 192.168.156.30 83.149.32.13 TLSv1 364 Client Key Exchange, Change Cipher Spec, Encrypted Handshake Message
308 4002.240695000 83.149.32.13 192.168.156.30 TLSv1 61 Alert (Level: Fatal, Description: Bad Record MAC)
您可以看到连接已建立,但服务器发出警报,因为它可能无法解码加密的握手消息。
我没有设法在 Android 5.0 上使用 HttpClient 连接到 https://uralsg.megafon.ru。股票浏览器确实连接它。 Android 2.3-4.4 以任何方式连接本网站没有任何困难。
有什么方法可以让 HttpClient 连接这些网站?这只是一个例子,我相信有很多遗留服务器无法通过 Android 5.0 和 HttpClient 连接。
【问题讨论】:
-
我遇到了同样的问题。如果我知道解决方案,我将添加 cmets。真的很烦
标签: android ssl apache-httpclient-4.x android-5.0-lollipop handshake