【发布时间】:2012-01-13 23:29:40
【问题描述】:
自 Android ICS 以来,我们在验证从 HttpsUrlConnection 获得的证书时遇到问题。在早期版本的 android 中,这运行良好。
这就是我们正在尝试做的事情:
BrowserCompatHostnameVerifier hostNameVerifier = new BrowserCompatHostnameVerifier();
HttpsURLConnection.setDefaultHostnameVerifier(hostNameVerifier);
URL url = new URL(serverUrl);
this.urlConnection = (HttpsURLConnection) url.openConnection();
this.urlConnection.connect();
hostNameVerifier.verify(urlConnection.getURL().getHost(),
(X509Certificate) urlConnection.getServerCertificates()[0]);
抛出的异常是:
java.lang.IllegalStateException 在 libcore.net.http.HttpEngine.getCacheResponse(HttpEngine.java:412) 在 libcore.net.http.HttpsURLConnectionImpl$HttpUrlConnectionDelegate.getCacheResponse(HttpsURLConnectionImpl.java:390) 在 libcore.net.http.HttpsURLConnectionImpl.getServerCertificates(HttpsURLConnectionImpl.java:87)
有人知道哪里出了问题以及为什么它只在 ICS 之后持续存在吗?
谢谢!
----- 更新------ 现在我像这样制作了自己的 HostnameVerifier。我避免使用这样的 getServerCertificates() 方法并且它正在工作:
public class MyHostNameVerifier implements HostnameVerifier {
private String expectedHost;
public MyHostNameVerifier(String expectedHost) {
this.expectedHost = expectedHost;
}
@Override
public boolean verify(String hostname, SSLSession session) {
return expectedHost.equals(hostname);
}
}
【问题讨论】:
标签: android ssl android-4.0-ice-cream-sandwich