title: Extract Server Certificates
body: <p>I want help in getting the apt piece of code to get server certificates - valid and invalid , signed by CA and self signed. Any links and references will be highly appreciated.</p>
  <p>I have a UNIX command which gives me what i want but I want the same output using Java. The command in UNIX is like this -</p>
  <pre><code>echo -n | openssl s_client -connect www.gmail.com:443 -showcerts | \
    sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' &gt; /tmp/$SERVERNAME.cert
  </code></pre>
  <p>This returns the (don't know the encryption) chain of certificates on gmail. I want my program to give the exact same information. Print the whole chain of certificates.</p>