java 使用Apache 4.5的方法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 使用Apache 4.5的方法相关的知识,希望对你有一定的参考价值。

/////////////////
// Create SSL Client
/////////////////

CloseableHttpClient httpclient = null;
HttpHost target = new HttpHost('www.mysite.com', 443, "https");

SSLContext sslcontext = SSLContexts.createSystemDefault();
SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(
        sslcontext, new String[] { "TLSv1", "SSLv3" }, null,
        SSLConnectionSocketFactory.getDefaultHostnameVerifier());

Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
        .register("http", PlainConnectionSocketFactory.INSTANCE)
        .register("https", sslConnectionSocketFactory)
        .build();
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(socketFactoryRegistry);

httpclient = HttpClients.custom()
.setSSLSocketFactory(sslConnectionSocketFactory)
.setConnectionManager(cm)
.build();

/////////////////
// Send POST
/////////////////

HttpPost httppost = new HttpPost('/mypath');
ByteArrayEntity postDataEntity = new ByteArrayEntity(postData.getBytes());
httpPost.setEntity(postDataEntity);
CloseableHttpResponse response = httpclient.execute(target, httpPost);

/////////////////
// Get RESPONSE
/////////////////

try {
        HttpEntity entity = response.getEntity();
        result = EntityUtils.toString(entity);
        EntityUtils.consume(entity);
} finally {
        response.close();
}

以上是关于java 使用Apache 4.5的方法的主要内容,如果未能解决你的问题,请参考以下文章

apache httpclient 4.5 的简单实用

java学习与应用(4.5)--CookieSessionJSP等

How to Install Apache Solr 4.5 on CentOS 6.4

java中如何实现在当前时间上加上4.5个小时返回一个Date类型

openDCIM-4.5安装步骤Centos7

4.5-全栈Java笔记:垃圾回收机制