title: How to react on untrusted certificate exceptions while performing a HTTPS request?
body: <p>I am doing a HTTPS request using a HttpsURLConnection.</p>
  
  <p>The server I'm trying to contact has a self-signed certificate, so this logically causes the request to fail.</p>
  
  <p>However, the failure isn't signaled to my program by an exception. It just fails and I can see why in the logcat.</p>
  
  <pre><code>03-22 17:54:35.203: W/System.err(21147): javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
  03-22 17:54:35.203: W/System.err(21147):    at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:381)
  </code></pre>
  
  <p>Here is the code I use for the request:</p>
  
  <pre><code>HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
  connection.setUseCaches(false);
  connection.setAllowUserInteraction(false);
  connection.connect();
  </code></pre>
  
  <p>Ideally, I'd like my program to react like browser do: popup a dialog indicating that the certificate is not trusted and possibly add it to some key-store and retry the request.</p>
  
  <p>But since I can't figure out a way of getting the exception, I really don't know what to do here.</p>
  
  <p>Any clue ?</p>