ssl如何检查客户端是否在java中使用PoolightTPClientConnectionManager
我正试图从我正在使用的客户端代码在服务器上使用连接池(配置了Https SSL) PoolghttpClientConnectionManager和我有一个连接池的工作代码,但我想知道我的代码是否使用PoolghttpClientConnectionManager,或者如何确定它
如果我的代码没有使用池管理器,如何让它使用它
我的代码:
static PoolingHttpClientConnectionManager cm ;
static CloseableHttpClient httpClient;
static
{
SslConfigurator sslConfig = SslConfigurator.newInstance()
.securityProtocol("TLS")
.keyStoreFile("/path")
.keyStorePassword("passw")
.keyStoreType("JKS")
.trustStoreFile("/path");
SSLContext sslCtx = sslConfig.createSSLContext();
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslCtx,NoopHostnameVerifier.INSTANCE);
HttpClientContext clientContext = HttpClientContext.create();
final Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory> create()
.register("http", PlainConnectionSocketFactory.getSocketFactory())
.register("https", sslSocketFactory)
.build();
cm = new PoolingHttpClientConnectionManager(registry);
client = HttpClients.custom()
.setSSLSocketFactory(sslSocketFactory)
.setConnectionManager(cm)
.build();
}
public static void main(String a[]) throws ClientProtocolException, IOException, JSONException
{
JSONObject jsonResponse;
StringEntity se = new StringEntity(jsonRequest.toString());
HttpPost httpPost = new HttpPost(path);
httpPost.setEntity(se);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
httpPost.setHeader("Connection", "keep-alive");
CloseableHttpResponse response2;
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss:SSS");
int i;
for(i=0;i<10;i++)
{
response2 = client.execute(httpPost);
System.out.println(response2.getStatusLine());
HttpEntity entity2 = response2.getEntity();
String result = EntityUtils.toString(entity2);
System.out.println(result);
Date date = new Date();
System.out.println(dateFormat.format(date));
response2.close();
}
}
# 1 楼答案
在面对如此复杂的问题时,这里有一些经验法则
创建一个简单的类,比如'brokenpcm',它扩展了
org.apache.http.impl.conn.PoolingHttpClientConnectionManager
,在这个新类中,只需重写org.apache.http.impl.conn.PoolingHttpClientConnectionManager#connect
方法,基本上抛出一个java.lang.RuntimeException
当您要将这种类型的实例传递给http客户端生成器
.setConnectionManager(brokenCM)
并尝试连接时,您应该会得到运行时异常,对吗这是我用这个的一个简化版本
您的控制台将显示如下内容:
除此之外,您还应该依赖文档和其他东西,比如java的静态类型方面:)