【发布时间】:2014-11-09 08:42:57
【问题描述】:
我正在尝试弄清楚如何将成功的 HTTP GET 请求发送到需要 SNI 的服务器。
在SO等地方搜了一下,发现有文章说JDK7现在支持SNI,还有Apache HTTP Components。
https://issues.apache.org/jira/browse/HTTPCLIENT-1119 https://wiki.apache.org/HttpComponents/SNISupport
相关SO文章:Certificate chain different between HTTPSURLconnection and Apache (System) DefaultHttpClient
--
但是,我似乎找不到任何说明如何使其工作的文档。
这是我正在使用的代码...
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
String trustedCertsPath = System.getenv("JAVA_HOME") + "/jre/lib/security/cacerts";
FileInputStream certstream = new FileInputStream(new File(trustedCertsPath));
try {
trustStore.load(certstream, "changeit".toCharArray());
} finally {
certstream.close();
}
// Trust own CA and all self-signed certs
SSLContext sslcontext = SSLContexts.custom()
.loadTrustMaterial(trustStore, new TrustSelfSignedStrategy())
.build();
// Allow TLSv1 protocol only
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
sslcontext,
new String[] { "TLSv1" },
null,
SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
CloseableHttpClient httpclient2 = HttpClients.custom()
.setSSLSocketFactory(sslsf)
.build();
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpget = new HttpGet(uri);
CloseableHttpResponse response = httpclient.execute(httpget);
try {
HttpEntity entity = response.getEntity();
if (entity != null) {
tempFile = File.createTempFile(httpFile.getTempFilePrefix(), httpFile.getTempFilePosfix());
FileOutputStream os = new FileOutputStream(tempFile);
InputStream instream = entity.getContent();
try {
IOUtils.copy(instream, os);
} finally {
try { instream.close(); } catch (Exception e) {}
try { os.close(); } catch (Exception e) {}
}
}
} finally {
response.close();
}
当我运行它时,请求失败。
服务器在请求中需要 SNI,如果没有它,它会返回一个具有错误 CommonName 的过期证书,因此会被拒绝。
如果我使用 httpclient2 实例,即使用自定义 SSL 上下文设置以允许所有 hTTP 证书,则请求成功。但是,我不想在每天针对不同主机进行大量下载的服务器上启用此功能。
我正在使用 httpclient v 4.3.5
任何帮助表示赞赏。
谢谢。
【问题讨论】: