【发布时间】:2019-12-07 03:51:54
【问题描述】:
在 Java 中,我需要让 Apache CloseableHttpClient 使用 TLSv1.2。目前它看起来像使用 HTTP/1.1。如何在下面的代码中指定协议版本?
public static void main(String [] args){
try {
SSLContext sslContext = SSLContext.getInstance("TLS");
@SuppressWarnings("static-access")
CloseableHttpClient httpclient = HttpClientBuilder.create()
.setSSLSocketFactory(new SSLConnectionSocketFactory(sslContext.getDefault(), new String[] { "TLSv1.2" }, null, SSLConnectionSocketFactory.getDefaultHostnameVerifier()))
.build();
HttpHost target = new HttpHost("https://www.sometest.com", 443, "https");
String call = "/vivisimo/cgi-bin/velocity";
HttpGet getRequest = new HttpGet(call);
System.out.println("Protocol Version:" + getRequest.getProtocolVersion());
} catch (Exception e) {
e.printStackTrace();
}
}
【问题讨论】:
-
你了解TLS和HTTP的区别吗?
-
@JoshuaHedges 请检查添加到答案中的运行代码,以演示如何强制使用特定的 TLS 版本。