Sample code for 30+ languages & platforms
Java Requires Chilkat v11.0.0+

Get the Server Certificate, Certificate Chain, and Root CA Certificate

See more HTTP Examples

Demonstrates how to get the HTTP server certificate, its certificate chain, and the root CA certificate.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.Cert;
import com.chilkatsoft.chilkat2.CertChain;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example assumes the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        Http http = new Http();

        // We're getting the SSL/TLS certificate, so make sure to connect to the SSL/TLS port (443).
        Cert sslCert = new Cert();
        try {
            http.getServerCert("apple.com", 443, sslCert);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        CertChain certChain = new CertChain();
        try {
            sslCert.buildCertChain(certChain);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        Cert cert = new Cert();
        int i = 0;
        int numCerts = certChain.getNumCerts();
        while (i < numCerts) {
            certChain.certAt(i, cert);
            System.out.println("SubjectDN " + i + ": " + cert.getSubjectDN());
            System.out.println("IssuerDN " + i + ": " + cert.getIssuerDN());
            i = i + 1;
        }

        // If the certificate chain reaches the root CA cert, then the last cert in the chain
        // is the root CA cert.
        if (certChain.getReachesRoot()) {
            Cert caCert = new Cert();
            certChain.certAt(numCerts - 1, caCert);
            System.out.println("CA Root Cert: " + caCert.getSubjectDN());
        }
    }
}