title: Android ICS: Problems to get the server certificate of the HttpsUrlConnection
body: <p>Since Android ICS we have problems to verify our certificates we are getting from the <code>HttpsUrlConnection</code>. In the earlier versions of android this was working well.
  This is what we are trying to do:</p>
  
  <pre><code>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]);
  </code></pre>
  
  <p>The exception which is thrown is:</p>
  
  <blockquote>
    <p>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)</p>
  </blockquote>
  
  <p>Does somebody know what could have gone wrong and why it only persists since ICS?</p>
  
  <p>Thanks!</p>
  
  <p>----- Update -------
  Now I made my own HostnameVerifier like this. I avoid the getServerCertificates()-method like this and it is working:</p>
  
  <pre><code>    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);
      }
  }
  </code></pre>