如何使用HttpClient创建HTTPS获取请求?我做了很多搜索,似乎没有什么对我有用 - 网上很多解决方案也取决于弃用的api调用:(
目前,我已按照以下链接中的代码(章节:获取服务器证书):http://memorynotfound.com/apache-httpclient-get-server-certificates/
我的代码如下:
HttpResponseInterceptor certificateInterceptor = new HttpResponseInterceptor() {
@Override
public void process(HttpResponse httpResponse, HttpContext context) throws HttpException, IOException {
ManagedHttpClientConnection routedConnection = (ManagedHttpClientConnection)context.getAttribute(HttpCoreContext.HTTP_CONNECTION);
SSLSession sslSession = routedConnection.getSSLSession();
if (sslSession != null) {
// get the server certificates from the {@Link SSLSession}
Certificate[] certificates = sslSession.getPeerCertificates();
// add the certificates to the context, where we can later grab it from
this.context.setAttribute(PEER_CERTIFICATES, certificates);
}
}
};
this.httpClient = HttpClients
.custom()
.addInterceptorLast(certificateInterceptor)
.build();
然而,这会导致以下错误:
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
为什么我会收到此异常,如何解决?任何帮助将不胜感激!