0

有没有办法使用 Apache 的 HttpClient (v4) 获取网站的证书详细信息?我可以使用 获取网站证书javax.net.ssl.HttpsURLConnection.getServerCertificates(),但我正在使用 Apache 的HttpClient进行连接,并希望使用相同的库。

我需要证书的颁发者、到期时间、算法等信息。

4

1 回答 1

4
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());

希望这可以帮助。

于 2012-07-25T08:56:54.763 回答