好吧这在某些方面有点元,我希望以一种可以与任何连接管理器一起工作的方式做到这一点.我假设你正在运行最新的HttpClient(4.2)
因此,您需要做的是将HttpResponseInterceptor添加到客户端.
((AbstractHttpClient)client).addResponseInterceptor(new HttpResponseInterceptor() {
@Override
public void process(HttpResponse response, HttpContext context) throws HttpException, IOException {
HttpRoutedConnection routedConnection= (HttpRoutedConnection)context.getAttribute(ExecutionContext.HTTP_CONNECTION);
if( routedConnection.isSecure() ) {
Certificate[] certificates= routedConnection.getSSLSession().getPeerCertificates();
// Assume that PEER_CERTIFICATES is a constant you've defined
context.setAttribute(PEER_CERTIFICATES, certificates);
}
}
});
完成后,通过此客户端发出的任何请求都将检查连接是否标记为“安全”,然后尝试获取对等证书.
在这个例子中,我只是放入与对等连接相关联的整个证书数组.
此时,要执行,您将执行类似于以下操作:
HttpContext context= new BasicHttpContext();
HttpGet get= new HttpGet(....);
client.execute(get, context);
// should contain the array of Certificate - these are more likely X509Certificate instances
Certificate[] peerCertificates= (Certificate[])context.getAttribute(PEER_CERTIFICATES);certificates
// do whatever logic to complete and consume the request
希望这将得到你所需要的 – 如果有人有超出此建议,他们将不胜感激.
编辑这也可以作为HttpRequestInterceptor完成,并且具有与已建立连接相同的效果.