package com.nextjoy.projects.usercenter.util.http;

 import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.config.*;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.DefaultConnectionReuseStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.net.URI;
import java.net.URLEncoder;
import java.nio.charset.CodingErrorAction;
import java.util.*;
import java.util.Map.Entry; /**
* 基于apache httpclient 4.3X以上版本连接池管理的GET POST请求
*
* @see PoolingHttpClientConnectionManager
*
* @author Vincent
*
*/
public class HttpPoolUtils {
private final static Logger LOG = LoggerFactory.getLogger(HttpPoolUtils.class);
private static PoolingHttpClientConnectionManager connManager = null;
private static CloseableHttpClient httpclient = null;
/** 默认UA */
private static final String DEFAULT_USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36";
/** 默认编码 */
public static final String DEFAULT_ENCODING = "UTF-8";
/** 最大连接数 */
public final static int MAX_TOTAL_CONNECTIONS = 5;
/** 每个路由最大连接数 */
public final static int MAX_PER_ROUTE = 2;
/** 连接超时时间 */
public static final int CONNECT_TIMEOUT = 50000;
/** 等待数据超时时间 */
public static final int SO_TIMEOUT = 20000;
/** 连接池连接不足超时等待时间 */
public static final int CONN_MANAGER_TIMEOUT = 500; private static final RequestConfig DEFAULT_REQUESTCONFIG = RequestConfig.custom().setSocketTimeout(SO_TIMEOUT).setConnectTimeout(CONNECT_TIMEOUT)
.setConnectionRequestTimeout(CONN_MANAGER_TIMEOUT).setExpectContinueEnabled(false).build(); static {
try {
SSLContext sslContext = SSLContexts.custom().build();
// TODO 如有需要自行添加相关证书 sslContext.init... Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory> create()
.register("http", PlainConnectionSocketFactory.INSTANCE).register("https", new SSLConnectionSocketFactory(sslContext)).build(); connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
httpclient = HttpClients.custom().setConnectionManager(connManager).setRetryHandler(new DefaultHttpRequestRetryHandler(0, false))
.setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy()).setConnectionReuseStrategy(new DefaultConnectionReuseStrategy())
.setUserAgent(DEFAULT_USER_AGENT).build();
SocketConfig socketConfig = SocketConfig.custom().setTcpNoDelay(true).build();
connManager.setDefaultSocketConfig(socketConfig);
MessageConstraints messageConstraints = MessageConstraints.custom().build();
ConnectionConfig connectionConfig = ConnectionConfig.custom().setMalformedInputAction(CodingErrorAction.IGNORE)
.setUnmappableInputAction(CodingErrorAction.IGNORE).setCharset(Consts.UTF_8).setMessageConstraints(messageConstraints).build();
connManager.setDefaultConnectionConfig(connectionConfig);
connManager.setMaxTotal(MAX_TOTAL_CONNECTIONS);
connManager.setDefaultMaxPerRoute(MAX_PER_ROUTE);
} catch (Exception e) {
LOG.error("some error is init, please check it.", e);
}
} /**
* post请求
*
* @param url
* 请求URL
* @param params
* 参数
* @param contentType
* 格式
* @param userAgent
* UA
* @param encoding
* 编码
* @return
*/
public static String post(String url, Map<String, String> params, String contentType, String userAgent, String encoding) {
String data = "";
HttpPost httpPost = new HttpPost();
CloseableHttpResponse response = null;
try {
httpPost.setURI(new URI(url));
if (contentType != null && contentType != "") {
httpPost.setHeader(HttpHeaders.CONTENT_TYPE, contentType);
} else {
httpPost.setHeader(HttpHeaders.CONTENT_TYPE, "text/html");
}
if (userAgent != null && userAgent != "") {
httpPost.setHeader(HttpHeaders.USER_AGENT, userAgent);
}
RequestConfig requestConfig = RequestConfig.copy(DEFAULT_REQUESTCONFIG).build();
httpPost.setConfig(requestConfig); List<NameValuePair> nvps = new ArrayList<NameValuePair>();
if (params != null) {
for (Entry<String, String> entry : params.entrySet()) {
nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
}
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
// LOG.debug(String.format("[HttpPoolUtils Post] begin invoke url:
// %s , params: %s",url,params.toString()));
response = httpclient.execute(httpPost);
HttpEntity entity = response.getEntity();
if (entity != null) {
data = EntityUtils.toString(entity, encoding);
// LOG.debug(String.format("[HttpPoolUtils Post]Debug response,
// url :%s , response string :%s",url,data));
}
} catch (Exception e) {
LOG.error("[HttpPoolUtils Post] is error. ", e);
} finally {
if (response != null) {
try {
EntityUtils.consume(response.getEntity());
} catch (IOException e) {
e.printStackTrace();
}
}
httpPost.reset();
}
return data;
} /**
* Get请求方式
*
* @param url
* 请求URL
* @param params
* 参数
* @param contentType
* 格式
* @param userAgent
* UA
* @param encoding
* 编码
* @return
*/
public static String get(String url, Map<String, String> params, String contentType, String userAgent, String encoding) {
String data = "";
HttpGet httpGet = new HttpGet();
CloseableHttpResponse response = null;
try {
StringBuilder sb = new StringBuilder();
sb.append(url);
boolean first = true;
if (params != null) {
for (Entry<String, String> entry : params.entrySet()) {
if (first && !url.contains("?")) {
sb.append("?");
} else {
sb.append("&");
}
sb.append(entry.getKey());
sb.append("=");
String value = entry.getValue();
sb.append(URLEncoder.encode(value, "UTF-8"));
first = false;
}
} // LOG.info("[HttpPoolUtils Get] begin invoke:" + sb.toString());
httpGet.setURI(new URI(sb.toString()));
RequestConfig requestConfig = RequestConfig.copy(DEFAULT_REQUESTCONFIG).build();
httpGet.setConfig(requestConfig);
if (contentType != null && contentType != "") {
httpGet.setHeader(HttpHeaders.CONTENT_TYPE, contentType);
} else {
httpGet.setHeader(HttpHeaders.CONTENT_TYPE, "text/html");
}
if (userAgent != null && userAgent != "") {
httpGet.setHeader(HttpHeaders.USER_AGENT, userAgent);
} response = httpclient.execute(httpGet);
HttpEntity entity = response.getEntity();
if (entity != null) {
data = EntityUtils.toString(entity, encoding);
}
// LOG.debug(String.format("[HttpPoolUtils Get]Debug url:%s ,
// response data %s:",sb.toString(),data));
} catch (Exception e) {
LOG.error(String.format("[HttpPoolUtils Get]invoke get error, url:%s, para:%s", url, params.toString()), e);
} finally {
if (response != null) {
try {
EntityUtils.consume(response.getEntity());
} catch (IOException e) {
e.printStackTrace();
}
}
httpGet.reset();
}
return data;
} public static void main0(String[] args) throws Exception {
long start = System.currentTimeMillis();
Random r = new Random();
for (int i = 0; i < 20; i++) {
long startPer = System.currentTimeMillis();
String url = "https://www.baidu.com/s?wd=" + r.nextInt(5000);
String data = get(url, Collections.<String, String> emptyMap(), null, null, HttpPoolUtils.DEFAULT_ENCODING);
System.out.println("结果长度:" + data.length());
System.out.println("单次请求耗时ms:" + (System.currentTimeMillis() - startPer));
}
System.out.println("查询总耗时ms:" + (System.currentTimeMillis() - start));
} public static void main1(String[] args) throws Exception {
long start = System.currentTimeMillis();
String url = "http://samworld.samonkey.com/v2_0/media/detail/343";
Map<String, String> params = new HashMap<String, String>();
params.put("userId", "F60D72944B9E236E7C7D219851DB5C62");
params.put("deviceType", "IOS");
params.put("version", "v3.1.3");
String userAgent = "VRStore/3.1.3 (iPhone; iOS 9.3.2; Scale/3.00)";
String contentType = "application/json;charset=UTF-8";
String acceptEncoding = "gzip, deflate";
String encoding = "UTF-8";
// String data = HttpPoolUtils.get(url, params, "application/json",
// userAgent, "UTF-8");
String data = "";
HttpGet httpGet = new HttpGet();
CloseableHttpResponse response = null;
try {
StringBuilder sb = new StringBuilder();
sb.append(url);
boolean first = true;
if (params != null) {
for (Entry<String, String> entry : params.entrySet()) {
if (first && !url.contains("?")) {
sb.append("?");
} else {
sb.append("&");
}
sb.append(entry.getKey());
sb.append("=");
String value = entry.getValue();
sb.append(URLEncoder.encode(value, "UTF-8"));
first = false;
}
} // LOG.info("[HttpPoolUtils Get] begin invoke:" + sb.toString());
httpGet.setURI(new URI(sb.toString()));
RequestConfig requestConfig = RequestConfig.copy(DEFAULT_REQUESTCONFIG).build();
httpGet.setConfig(requestConfig);
if (contentType != null && contentType != "") {
httpGet.setHeader(HttpHeaders.CONTENT_TYPE, contentType);
} else {
httpGet.setHeader(HttpHeaders.CONTENT_TYPE, "text/html");
}
if (userAgent != null && userAgent != "") {
httpGet.setHeader(HttpHeaders.USER_AGENT, userAgent);
}
if (acceptEncoding != null && acceptEncoding != "") {
httpGet.setHeader(HttpHeaders.ACCEPT_ENCODING, acceptEncoding);
}
httpGet.setHeader(HttpHeaders.ACCEPT_LANGUAGE, "zh-Hans-CN;q=1, pl-PL;q=0.9");
httpGet.setHeader(HttpHeaders.ACCEPT, "*/*");
response = httpclient.execute(httpGet);
HttpEntity entity = response.getEntity();
if (entity != null) {
data = EntityUtils.toString(entity, encoding);
}
// LOG.debug(String.format("[HttpPoolUtils Get]Debug url:%s ,
// response data %s:",sb.toString(),data));
} catch (Exception e) {
LOG.error(String.format("[HttpPoolUtils Get]invoke get error, url:%s, para:%s", url, params.toString()), e);
} finally {
if (response != null) {
try {
EntityUtils.consume(response.getEntity());
} catch (IOException e) {
e.printStackTrace();
}
}
httpGet.reset();
} System.out.println(data);
System.out.println("查询总耗时ms:" + (System.currentTimeMillis() - start));
} public static void main(String[] args) throws Exception {
Map<String, String> param = new HashMap();
param.put("access_token", "70603C5A83DDC1507B00AC52E55C13EA");
param.put("unionid", "1");
String result = HttpPoolUtils.get("https://graph.qq.com/oauth2.0/me", param, null, null, HttpPoolUtils.DEFAULT_ENCODING);
System.out.println(result); long start = System.currentTimeMillis();
long startPer = System.currentTimeMillis();
String url = "http://nextjoy.cn/programMan/list_demo.php";
String data = get(url, Collections.<String, String> emptyMap(), null, null, HttpPoolUtils.DEFAULT_ENCODING);
System.out.println("结果长度:" + data.length());
System.out.println("Data:" + data);
System.out.println("单次请求耗时ms:" + (System.currentTimeMillis() - startPer));
System.out.println("查询总耗时ms:" + (System.currentTimeMillis() - start));
}
}

这里用到的jar包:

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
  <version>4.5.2</version>
</dependency>

HttpPoolUtils 连接池管理的GET POST请求的更多相关文章

  1. 关于.NET大数据量大并发量的数据连接池管理

    转自:http://www.cnblogs.com/virusswb/archive/2010/01/08/1642055.html 我以前对.NET连接池的认识是错误的,原来以为在web.confi ...

  2. Spring学习11-Spring使用proxool连接池 管理数据源

    Spring 一.Proxool连接池简介及其配置属性概述   Proxool是一种Java数据库连接池技术.是sourceforge下的一个开源项目,这个项目提供一个健壮.易用的连接池,最为关键的是 ...

  3. Spring使用proxool连接池 管理数据源

    一.Proxool连接池简介及其配置属性概述 Proxool是一种Java数据库连接池技术.是sourceforge下的一个开源项目,这个项目提供一个健壮.易用的连接池,最为关键的是这个连接池提供监控 ...

  4. MySQL-第十五篇使用连接池管理连接

    1.数据库连接池的解决方案是: 当应用程序启动时,系统主动建立足够的数据库连接,并将这些连接组成一个连接池.每次应用程序请求数据库连接时,无需重新打开连接,而是从连接池中取出已有的连接使用,使用完后不 ...

  5. httpclient连接池在ES Restful API请求中的应用

    package com.wm.utils; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http ...

  6. Netty服务器连接池管理设计思路

    应用场景: 在RPC框架中,使用Netty作为高性能的网络通信框架时,每一次服务调用,都需要与Netty服务端建立连接的话,很容易导致Netty服务器资源耗尽.所以,想到连接池技术,将与同一个Nett ...

  7. Redis缓存连接池管理

    import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.util.Assert;import ...

  8. httpclient: 设置连接池及超时配置,请求数据:PoolingHttpClientConnectionManager

    public static void main(String[] args) throws Exception{ //httpclient连接池 //创建连接池 PoolingHttpClientCo ...

  9. 使用httpClient连接池处理get或post请求

    以前有一个自己写的: http://www.cnblogs.com/wenbronk/p/6482706.html 后来发现一个前辈写的更好的, 再此感谢一下, 确实比我写的那个好用些 1, 创建一个 ...

随机推荐

  1. MySQL之慢日志记录、分页

    1.慢日志记录 slow_query_log = OFF #是否开启慢日志记录 long_query_time = 2 #时间限制,超过此时间,则记录 slow_query_log_file = C: ...

  2. Python入门到进阶必看的权威书籍与网站

    随着人工智能全面爆发,Python[英文单词:蟒蛇],是一款近年来爆红的计算机编程语言.1989年发明,1991年发行,比目前应用最广的Java还要大7岁,有种大器晚成的感觉. 分享之前我还是要推荐下 ...

  3. 【Inno Setup】Pascal 脚本 ---- 事件函数

    转载 事件函数 Inno Setup支持以下函数和过程. 1. [安装初始化]该函数在安装程序初始化时调用,返回False 将中断安装,True则继续安装,测试代码如下: function Initi ...

  4. RabbitMQ Hello world(二)

    简介: Rabbitmq 是消息代理中间件,它接收或者发送消息.你可以把它想想宬一个邮局:当你把邮件放到邮箱时,你可以确定某一位邮递员可以准确的把邮件送到收件人手中,在这个比喻中,rabbitmq是一 ...

  5. java中的daemon thread

    java中的daemon thread java中有两种类型的thread,user threads 和 daemon threads. User threads是高优先级的thread,JVM将会等 ...

  6. JavaScript正则表达式及jQuery回顾

    JavaScript 正则表达式,用于规定在文本中检索的内容. 一.定义正则表达式: rep = /\d+/; // js定义正则.(python定义正则:re模块 rep = "\d+&q ...

  7. nodejs实现定时爬取微博热搜

    The summer is coming " 我知道,那些夏天,就像青春一样回不来. - 宋冬野 青春是回不来了,倒是要准备渡过在西安的第三个夏天了. 废话 我发现,自己对 coding 这 ...

  8. React Native中自定义导航条

    这是2017年年初开始的公司的项目,对于导航条的要求很高,Android和iOS上必须用一致的UI,按钮位置还有各种颜色都有要求,而且要适应各种奇葩要求. 尝试了一下当时React Native自带的 ...

  9. canvas 绘图api的位置问题

    很久没碰canvas了,今天因为canvas绘图的为之问题浪费了一些时间. 我们知道canvas的默认宽高是300X150嘛. 实际使用的时候当然是自定义一个高宽啦. 通常我们会习惯性地在js中通过c ...

  10. Win10美吱er吱er,Win10修改默认字体的方法

    请参考以下步骤(需要修改注册表,修改前请先备份,以便在出现问题时能够及时恢复): 例:将系统字体改为宋体 1.Windows+r,输入:regedit 2.定位以下路径:HKEY_LOCAL_MACH ...