title: How can I get the public key of a secure webpage using java
body: <p>I need to get the public keys of a secured website programmatically through java. I have read <a href="https://stackoverflow.com/questions/15449343/get-the-public-key-of-a-websites-ssl-certificate">this</a>, <a href="https://stackoverflow.com/questions/20160595/how-to-get-the-public-key">this</a>, <a href="https://stackoverflow.com/questions/11108562/getting-public-key-of-a-secure-website">this</a> and <a href="https://stackoverflow.com/questions/11047103/how-can-i-get-the-public-key-of-a-webpage">this</a> and others as well. 
  But I haven't found a solution of getting it through java.</p>
  
  <p>EDIT:::</p>
  
  <p>Based on Zielu's answer I wrote the following program:</p>
  
  <pre><code>import java.security.PublicKey;
  import java.security.cert.Certificate;
  
  import javax.net.ssl.HttpsURLConnection;
  import javax.net.ssl.SSLSocket;
  import javax.net.ssl.SSLSocketFactory;
  
  public class RetrievePublicKey {
  
      private static PublicKey getKey(String hostname, int port) throws Exception {
          SSLSocketFactory factory = HttpsURLConnection.getDefaultSSLSocketFactory();        
          SSLSocket socket = (SSLSocket) factory.createSocket(hostname, port);
          socket.startHandshake();
          Certificate[] certs = socket.getSession().getPeerCertificates();
          Certificate cert = certs[0];
          PublicKey key = cert.getPublicKey();
          System.out.println(key);
          return key;
      }
      public static void main(String[] args) throws Exception {
          System.out.println(getKey("bctcl-parasuram.bctchn.local", 8443));
      }
  
  }
  </code></pre>
  
  <p>But when I run it, i get the following exception:</p>
  
  <pre><code>Exception in thread "main" javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
      at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
      at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1937)
      at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:302)
      at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:296)
      at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1478)
      at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:212)
      at sun.security.ssl.Handshaker.processLoop(Handshaker.java:957)
      at sun.security.ssl.Handshaker.process_record(Handshaker.java:892)
      at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1050)
      at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1363)
      at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1391)
      at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1375)
      at RetrievePublicKey.getKey(RetrievePublicKey.java:22)
      at RetrievePublicKey.main(RetrievePublicKey.java:30)
  Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
      at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:387)
      at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292)
      at sun.security.validator.Validator.validate(Validator.java:260)
      at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:324)
      at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:229)
      at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124)
      at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1460)
      ... 9 more
  Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
      at sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:145)
      at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:131)
      at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:280)
      at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:382)
      ... 15 more
  </code></pre>