我试图在android中运行以下代码
URLConnection l_connection = null;
// Create connection
uzip=new UnZipData(mContext);
l_url = new URL(serverurl);
if ("https".equals(l_url.getProtocol())) {
System.out.println("<<<<<<<<<<<<< Before TLS >>>>>>>>>>>>");
sslcontext = SSLContext.getInstance("TLS");
System.out.println("<<<<<<<<<<<<< After TLS >>>>>>>>>>>>");
sslcontext.init(null,
new TrustManager[] { new CustomTrustManager()},
new java.security.SecureRandom());
HttpsURLConnection
.setDefaultHostnameVerifier(new CustomHostnameVerifier());
HttpsURLConnection.setDefaultSSLSocketFactory(sslcontext
.getSocketFactory());
l_connection = (HttpsURLConnection) l_url.openConnection();
((HttpsURLConnection) l_connection).setRequestMethod("POST");
} else {
l_connection = (HttpURLConnection) l_url.openConnection();
((HttpURLConnection) l_connection).setRequestMethod("POST");
}
/*System.setProperty("http.agent", "Android_Phone");*/
l_connection.setConnectTimeout(10000);
l_connection.setRequestProperty("Content-Language", "en-US");
l_connection.setUseCaches(false);
l_connection.setDoInput(true);
l_connection.setDoOutput(true);
System.out.println("<<<<<<<<<<<<< Before Connection >>>>>>>>>>>>");
l_connection.connect();
Run Code Online (Sandbox Code Playgroud)
在l_connection.connect(),它给出了这个SSLhandshakeException.有时候它有效,但大部分时间它都有例外.它只发生在Android 4.0模拟器上.我在Android 4.4和5.0上测试过,它运行正常.可能是什么原因造成的?请帮忙
堆栈跟踪
04-28 15:51:13.143: W/System.err(2915): javax.net.ssl.SSLHandshakeException: …Run Code Online (Sandbox Code Playgroud) ssl android sslhandshakeexception android-4.0.3-ice-cream-sandwich
我们在android中编写了客户端应用程序,它使用HttpsUrlConnection apis与https服务器连接.由于Poodle漏洞,我们需要在调用任何请求时从启用的协议列表中禁用SSLv3.
我们遵循oracle捕获的指南,如http://www.oracle.com/technetwork/java/javase/documentation/cve-2014-3566-2342133.html
并在调用url连接之前添加了以下行
java.lang.System.setProperty("https.protocols", "TLSv1");
Run Code Online (Sandbox Code Playgroud)
这个解决方案适用于普通的java程序.尝试连接仅适用于SSLv3协议的服务器时,我们收到了SSLHandShakeException.
但值得关注的是:同样的修复不适用于Android.我错过了什么或者我应该为Android尝试另一种方法吗?请建议.