dejuknow
dejuknow

Reputation: 1591

Is there a way to obtain a website's certificate using Apache's HttpClient?

Is there a way to obtain a website's certificate details using Apache's HttpClient (v4)? I'm able to get a website's certificate using javax.net.ssl.HttpsURLConnection.getServerCertificates(), but I'm using Apache's HttpClient for my connections and would like to use the same library.

I need information like the certificate's issuer, expiration, algorithm, etc.

Upvotes: 0

Views: 257

Answers (1)

ok2c
ok2c

Reputation: 27613

DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.addRequestInterceptor(new HttpRequestInterceptor() {

    public void process(
            HttpRequest request, HttpContext context) throws HttpException, IOException {
        HttpRoutedConnection conn = (HttpRoutedConnection) context.getAttribute(
                ExecutionContext.HTTP_CONNECTION);
        SSLSession sslsession = conn.getSSLSession();
        if (sslsession != null) {
            // Do something useful with the SSL session details
            // and if necessary stick the result to the execution context
            // for further processing
            X509Certificate[] certs = sslsession.getPeerCertificateChain();
            for (X509Certificate cert: certs) {
                System.out.println(cert);
            }
        }
    }
});
HttpResponse response = httpclient.execute(new HttpGet("https://verisign.com/"));
EntityUtils.consume(response.getEntity());

Hope this helps.

Upvotes: 4

Related Questions