title: How to get server certificate chain then verify it's valid and trusted in Java
body: <p>I need to create an Https connection with a remote server then retrieve and verify the certificate.</p>
  
  <p>I have established the connection fine:</p>
  
  <pre><code>try {  
      url = new URL(this.SERVER_URL);  
      HttpURLConnection con = (HttpURLConnection) url.openConnection();   
      HttpsURLConnection secured = (HttpsURLConnection) con;  
      secured.connect(); 
  }  
  </code></pre>
  
  <p>But it seems <code>getServerCertificateChain()</code> method is undefined by the type <code>HttpsURLConnection</code>.</p>
  
  <p>So how do I retrieve the server certificate chain? My understanding is that <code>getServerCertificateChain()</code> should return an array of <code>X509Certificate</code> objects and that this class has methods I can use to interrogate the certificate.</p>
  
  <p>I need to verify that:</p>
  
  <ol>
  <li>the certificate is valid and trusted,</li>
  <li>check the Certificate Revocation List Distribution Point against the certificate serial number</li>
  <li>make sure it isn't expired and</li>
  <li>check that the URL in the certificate is matches another (which I already have retrieved ).</li>
  </ol>
  
  <p>I'm lost and would really appreciate any help!</p>