Since Android ICS we have problems to verify our certificates we are getting from the HttpsUrlConnection. In the earlier versions of android this was working well.
This is what we are trying to do:
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]);
The exception which is thrown is:
java.lang.IllegalStateException at libcore.net.http.HttpEngine.getCacheResponse(HttpEngine.java:412) at libcore.net.http.HttpsURLConnectionImpl$HttpUrlConnectionDelegate.getCacheResponse(HttpsURLConnectionImpl.java:390) at libcore.net.http.HttpsURLConnectionImpl.getServerCertificates(HttpsURLConnectionImpl.java:87)
Does somebody know what could have gone wrong and why it only persists since ICS?
Thanks!
----- Update ------- Now I made my own HostnameVerifier like this. I avoid the getServerCertificates()-method like this and it is working:
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);
}
}