【发布时间】:2013-01-15 04:48:11
【问题描述】:
我正在尝试从 Andorid 4.1.1 版中的 Android 应用程序连接到 URL,并且我收到问题标题中指示的错误,但是当我尝试从 Andorid 4.0 版连接相同的 URL 时。 4 或 3.1,一切正常。
代码片段:
try {
.
.
.
URL url = new URL(urlStr);
Log.i(TAG,"[ URL ] " + urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
int size = conn.getContentLength();
int responsecode = conn.getResponseCode();
Log.d(TAG, "Responsecode: " + responsecode);
.
.
.
} catch (Exception e) {
e.printStackTrace();
}
private static void trustAllHosts() {
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[] {};
}
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
} };
try {
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection
.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
System.out.println("IOException : HTTPSRequest::trustAllHosts");
e.printStackTrace();
}
}
但在这里我清楚一件事是“也许证书是自签名证书并且没有将它们包含在 KeyStore 中。
我不明白为什么这个异常只发生在 Android Verison 4.1.1 操作系统中 谢谢。
全栈跟踪
01-31 10:26:08.348: W/System.err(3158): java.io.IOException: Hostname <URL> was not verified
01-31 10:26:08.348: W/System.err(3158): at libcore.net.http.HttpConnection.verifySecureSocketHostname(HttpConnection.java:223)
01-31 10:26:08.348: W/System.err(3158): at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:446)
01-31 10:26:08.348: W/System.err(3158): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
01-31 10:26:08.348: W/System.err(3158): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
01-31 10:26:08.348: W/System.err(3158): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
01-31 10:26:08.348: W/System.err(3158): at libcore.net.http.HttpURLConnectionImpl.getHeaderField(HttpURLConnectionImpl.java:130)
01-31 10:26:08.348: W/System.err(3158): at java.net.URLConnection.getHeaderFieldInt(URLConnection.java:544)
01-31 10:26:08.348: W/System.err(3158): at java.net.URLConnection.getContentLength(URLConnection.java:316)
01-31 10:26:08.348: W/System.err(3158): at libcore.net.http.HttpsURLConnectionImpl.getContentLength(HttpsURLConnectionImpl.java:191)
01-31 10:26:08.348: W/System.err(3158): at com.ih.util.HelpVideoServices$downloadTask.run(HelpVideoServices.java:172)
【问题讨论】:
-
网址是什么? http 还是 https?域名还是id地址?
-
您是否有多个具有不同证书的虚拟主机?我目前在 4.1.1 和 4.1.2 中看到了这个问题 - 我认为因为我正在处理的服务器有多个虚拟主机服务于 https 流量,并且可能是因为 Android 和 SNI 的问题 - 另请参阅这个错误 code.google.com/p/android/issues/detail?id=36599跨度>
-
我前几天遇到了这个问题,
-
我的应用程序无法在 Android 4.1.1 操作系统中下载某些文件,但在其他 Android 操作系统(3.x、4.0.x、4.1.2)中可以下载相同的代码。我得到了解决方案,在我的应用程序中,一种方法 trustAllHosts() 使用 TrustManager[] 类信任证书。在这个函数中,我添加了 'HttpsURLConnection.setDefaultHostnameVerifier(DO_NOT_VERIFY);' DO_NOT_VERIFY 是 HostnameVerifier 类的对象 final static HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() { public boolean verify(String hostname, SSLSession session) { return true; } };