有没有办法使用Apache的HttpClient(v4)获取网站的证书详细信息?我可以使用javax.net.ssl.HttpsURLConnection.getServerCertificates()
获取网站的证书,但我正在使用Apache的HttpClient进行连接,并希望使用相同的库。
我需要证书的发行者,到期日,算法等信息。
答案 0 :(得分: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());
希望这有帮助。