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. go 基础 结构体

    结构体是类型中带有成员的复合类型.go语言使用结构体和结构体成员来描述真实世界的实体和实体对应的各种属性. go语言中的类型可以被实例化,使用new和&构造类型实例的类型是类型的指针. 结构体 ...

  2. iOS重构项目之路

    iOS重构项目之路 1.整理目录 按照功能模块对整个工程的目录进行分类,比如 2.整理资源文件 删除多余的图片文件,资源文件 图片资源尽量添加到Assets.xcassets中 删除项目中未引用的图片 ...

  3. debian7安装了mysql后,局域网去连接时出现10061错误

  4. PHP open_basedir配置未包含upload_tmp_dir 导致服务器不能上传文件

    在做一个上传图片的功能时候发现后台接收到的$_FILES['file']['error'] = 6,这个错误意思是找不到临时文件,或者是临时文件夹无权限,需要更改php.ini文件的 upload_t ...

  5. 利用python画出SJF调度图

    最先发布在csdn.本人原创. https://blog.csdn.net/weixin_43906799/article/details/105510046 SJF算法: 最短作业优先(SJF)调度 ...

  6. Latex-0-latex2word

    Latex-0-latex2word LatexXeLaTex Latex 转 Word 虽然latex 格式很方便,能够满足绝大部分的排版要求,但是在与人沟通的时候不可避免地需要用到其他格式文件,比 ...

  7. SQL计算算数表达式的函数自定义(加减乘除)

    一.整体思路:循环遍历表达式字符串,设置一个index从第一个字符开始检测当前数字是否可以和后面的数字进行运算,如果可以运算,将两个数挑出来运算,然后用运算的结果替换原来表达式中的这两个数和符号,计算 ...

  8. 标准库sys

    sys模块的主要函数介绍,结合官方文档说明和实例.This module provides access to some variables used or maintained by the int ...

  9. 短视频sdk:选择一个靠谱的短视频SDK 你需要了解这些

    2017 年,短视频成为了内容创业的新风口,各种短视频 App 如雨后春笋般先后上线.随着互联网内容消费升级,视频越来越像文字.图片一样,成为每一个 App 不可或缺的一部分. 为了能够更好地聚焦于业 ...

  10. 算法竞赛进阶指南--hamilton路径

    // hamilton路径 int f[1 << 20][20]; int hamilton(int n, int weight[20][20]) { memset(f, 0x3f, si ...