基于apache httpclient 4.3X以上版本连接池管理的GET POST请求

  1. 1 package com.nextjoy.projects.usercenter.util.http;
  2. 2
  3. 3 import org.apache.http.Consts;
  4. 4 import org.apache.http.HttpEntity;
  5. 5 import org.apache.http.HttpHeaders;
  6. 6 import org.apache.http.NameValuePair;
  7. 7 import org.apache.http.client.config.RequestConfig;
  8. 8 import org.apache.http.client.entity.UrlEncodedFormEntity;
  9. 9 import org.apache.http.client.methods.CloseableHttpResponse;
  10. 10 import org.apache.http.client.methods.HttpGet;
  11. 11 import org.apache.http.client.methods.HttpPost;
  12. 12 import org.apache.http.config.*;
  13. 13 import org.apache.http.conn.socket.ConnectionSocketFactory;
  14. 14 import org.apache.http.conn.socket.PlainConnectionSocketFactory;
  15. 15 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
  16. 16 import org.apache.http.impl.DefaultConnectionReuseStrategy;
  17. 17 import org.apache.http.impl.client.CloseableHttpClient;
  18. 18 import org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy;
  19. 19 import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
  20. 20 import org.apache.http.impl.client.HttpClients;
  21. 21 import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
  22. 22 import org.apache.http.message.BasicNameValuePair;
  23. 23 import org.apache.http.ssl.SSLContexts;
  24. 24 import org.apache.http.util.EntityUtils;
  25. 25 import org.slf4j.Logger;
  26. 26 import org.slf4j.LoggerFactory;
  27. 27
  28. 28 import javax.net.ssl.SSLContext;
  29. 29 import java.io.IOException;
  30. 30 import java.net.URI;
  31. 31 import java.net.URLEncoder;
  32. 32 import java.nio.charset.CodingErrorAction;
  33. 33 import java.util.*;
  34. 34 import java.util.Map.Entry;
  35. 35
  36. 36 /**
  37. 37 * 基于apache httpclient 4.3X以上版本连接池管理的GET POST请求
  38. 38 *
  39. 39 * @see PoolingHttpClientConnectionManager
  40. 40 *
  41. 41 * @author Vincent
  42. 42 *
  43. 43 */
  44. 44 public class HttpPoolUtils {
  45. 45 private final static Logger LOG = LoggerFactory.getLogger(HttpPoolUtils.class);
  46. 46 private static PoolingHttpClientConnectionManager connManager = null;
  47. 47 private static CloseableHttpClient httpclient = null;
  48. 48 /** 默认UA */
  49. 49 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";
  50. 50 /** 默认编码 */
  51. 51 public static final String DEFAULT_ENCODING = "UTF-8";
  52. 52 /** 最大连接数 */
  53. 53 public final static int MAX_TOTAL_CONNECTIONS = 5;
  54. 54 /** 每个路由最大连接数 */
  55. 55 public final static int MAX_PER_ROUTE = 2;
  56. 56 /** 连接超时时间 */
  57. 57 public static final int CONNECT_TIMEOUT = 50000;
  58. 58 /** 等待数据超时时间 */
  59. 59 public static final int SO_TIMEOUT = 20000;
  60. 60 /** 连接池连接不足超时等待时间 */
  61. 61 public static final int CONN_MANAGER_TIMEOUT = 500;
  62. 62
  63. 63 private static final RequestConfig DEFAULT_REQUESTCONFIG = RequestConfig.custom().setSocketTimeout(SO_TIMEOUT).setConnectTimeout(CONNECT_TIMEOUT)
  64. 64 .setConnectionRequestTimeout(CONN_MANAGER_TIMEOUT).setExpectContinueEnabled(false).build();
  65. 65
  66. 66 static {
  67. 67 try {
  68. 68 SSLContext sslContext = SSLContexts.custom().build();
  69. 69 // TODO 如有需要自行添加相关证书 sslContext.init...
  70. 70
  71. 71 Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory> create()
  72. 72 .register("http", PlainConnectionSocketFactory.INSTANCE).register("https", new SSLConnectionSocketFactory(sslContext)).build();
  73. 73
  74. 74 connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
  75. 75 httpclient = HttpClients.custom().setConnectionManager(connManager).setRetryHandler(new DefaultHttpRequestRetryHandler(0, false))
  76. 76 .setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy()).setConnectionReuseStrategy(new DefaultConnectionReuseStrategy())
  77. 77 .setUserAgent(DEFAULT_USER_AGENT).build();
  78. 78 SocketConfig socketConfig = SocketConfig.custom().setTcpNoDelay(true).build();
  79. 79 connManager.setDefaultSocketConfig(socketConfig);
  80. 80 MessageConstraints messageConstraints = MessageConstraints.custom().build();
  81. 81 ConnectionConfig connectionConfig = ConnectionConfig.custom().setMalformedInputAction(CodingErrorAction.IGNORE)
  82. 82 .setUnmappableInputAction(CodingErrorAction.IGNORE).setCharset(Consts.UTF_8).setMessageConstraints(messageConstraints).build();
  83. 83 connManager.setDefaultConnectionConfig(connectionConfig);
  84. 84 connManager.setMaxTotal(MAX_TOTAL_CONNECTIONS);
  85. 85 connManager.setDefaultMaxPerRoute(MAX_PER_ROUTE);
  86. 86 } catch (Exception e) {
  87. 87 LOG.error("some error is init, please check it.", e);
  88. 88 }
  89. 89 }
  90. 90
  91. 91 /**
  92. 92 * post请求
  93. 93 *
  94. 94 * @param url
  95. 95 * 请求URL
  96. 96 * @param params
  97. 97 * 参数
  98. 98 * @param contentType
  99. 99 * 格式
  100. 100 * @param userAgent
  101. 101 * UA
  102. 102 * @param encoding
  103. 103 * 编码
  104. 104 * @return
  105. 105 */
  106. 106 public static String post(String url, Map<String, String> params, String contentType, String userAgent, String encoding) {
  107. 107 String data = "";
  108. 108 HttpPost httpPost = new HttpPost();
  109. 109 CloseableHttpResponse response = null;
  110. 110 try {
  111. 111 httpPost.setURI(new URI(url));
  112. 112 if (contentType != null && contentType != "") {
  113. 113 httpPost.setHeader(HttpHeaders.CONTENT_TYPE, contentType);
  114. 114 } else {
  115. 115 httpPost.setHeader(HttpHeaders.CONTENT_TYPE, "text/html");
  116. 116 }
  117. 117 if (userAgent != null && userAgent != "") {
  118. 118 httpPost.setHeader(HttpHeaders.USER_AGENT, userAgent);
  119. 119 }
  120. 120 RequestConfig requestConfig = RequestConfig.copy(DEFAULT_REQUESTCONFIG).build();
  121. 121 httpPost.setConfig(requestConfig);
  122. 122
  123. 123 List<NameValuePair> nvps = new ArrayList<NameValuePair>();
  124. 124 if (params != null) {
  125. 125 for (Entry<String, String> entry : params.entrySet()) {
  126. 126 nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
  127. 127 }
  128. 128 }
  129. 129 httpPost.setEntity(new UrlEncodedFormEntity(nvps));
  130. 130 // LOG.debug(String.format("[HttpPoolUtils Post] begin invoke url:
  131. 131 // %s , params: %s",url,params.toString()));
  132. 132 response = httpclient.execute(httpPost);
  133. 133 HttpEntity entity = response.getEntity();
  134. 134 if (entity != null) {
  135. 135 data = EntityUtils.toString(entity, encoding);
  136. 136 // LOG.debug(String.format("[HttpPoolUtils Post]Debug response,
  137. 137 // url :%s , response string :%s",url,data));
  138. 138 }
  139. 139 } catch (Exception e) {
  140. 140 LOG.error("[HttpPoolUtils Post] is error. ", e);
  141. 141 } finally {
  142. 142 if (response != null) {
  143. 143 try {
  144. 144 EntityUtils.consume(response.getEntity());
  145. 145 } catch (IOException e) {
  146. 146 e.printStackTrace();
  147. 147 }
  148. 148 }
  149. 149 httpPost.reset();
  150. 150 }
  151. 151 return data;
  152. 152 }
  153. 153
  154. 154 /**
  155. 155 * Get请求方式
  156. 156 *
  157. 157 * @param url
  158. 158 * 请求URL
  159. 159 * @param params
  160. 160 * 参数
  161. 161 * @param contentType
  162. 162 * 格式
  163. 163 * @param userAgent
  164. 164 * UA
  165. 165 * @param encoding
  166. 166 * 编码
  167. 167 * @return
  168. 168 */
  169. 169 public static String get(String url, Map<String, String> params, String contentType, String userAgent, String encoding) {
  170. 170 String data = "";
  171. 171 HttpGet httpGet = new HttpGet();
  172. 172 CloseableHttpResponse response = null;
  173. 173 try {
  174. 174 StringBuilder sb = new StringBuilder();
  175. 175 sb.append(url);
  176. 176 boolean first = true;
  177. 177 if (params != null) {
  178. 178 for (Entry<String, String> entry : params.entrySet()) {
  179. 179 if (first && !url.contains("?")) {
  180. 180 sb.append("?");
  181. 181 } else {
  182. 182 sb.append("&");
  183. 183 }
  184. 184 sb.append(entry.getKey());
  185. 185 sb.append("=");
  186. 186 String value = entry.getValue();
  187. 187 sb.append(URLEncoder.encode(value, "UTF-8"));
  188. 188 first = false;
  189. 189 }
  190. 190 }
  191. 191
  192. 192 // LOG.info("[HttpPoolUtils Get] begin invoke:" + sb.toString());
  193. 193 httpGet.setURI(new URI(sb.toString()));
  194. 194 RequestConfig requestConfig = RequestConfig.copy(DEFAULT_REQUESTCONFIG).build();
  195. 195 httpGet.setConfig(requestConfig);
  196. 196 if (contentType != null && contentType != "") {
  197. 197 httpGet.setHeader(HttpHeaders.CONTENT_TYPE, contentType);
  198. 198 } else {
  199. 199 httpGet.setHeader(HttpHeaders.CONTENT_TYPE, "text/html");
  200. 200 }
  201. 201 if (userAgent != null && userAgent != "") {
  202. 202 httpGet.setHeader(HttpHeaders.USER_AGENT, userAgent);
  203. 203 }
  204. 204
  205. 205 response = httpclient.execute(httpGet);
  206. 206 HttpEntity entity = response.getEntity();
  207. 207 if (entity != null) {
  208. 208 data = EntityUtils.toString(entity, encoding);
  209. 209 }
  210. 210 // LOG.debug(String.format("[HttpPoolUtils Get]Debug url:%s ,
  211. 211 // response data %s:",sb.toString(),data));
  212. 212 } catch (Exception e) {
  213. 213 LOG.error(String.format("[HttpPoolUtils Get]invoke get error, url:%s, para:%s", url, params.toString()), e);
  214. 214 } finally {
  215. 215 if (response != null) {
  216. 216 try {
  217. 217 EntityUtils.consume(response.getEntity());
  218. 218 } catch (IOException e) {
  219. 219 e.printStackTrace();
  220. 220 }
  221. 221 }
  222. 222 httpGet.reset();
  223. 223 }
  224. 224 return data;
  225. 225 }
  226. 226
  227. 227 public static void main0(String[] args) throws Exception {
  228. 228 long start = System.currentTimeMillis();
  229. 229 Random r = new Random();
  230. 230 for (int i = 0; i < 20; i++) {
  231. 231 long startPer = System.currentTimeMillis();
  232. 232 String url = "https://www.baidu.com/s?wd=" + r.nextInt(5000);
  233. 233 String data = get(url, Collections.<String, String> emptyMap(), null, null, HttpPoolUtils.DEFAULT_ENCODING);
  234. 234 System.out.println("结果长度:" + data.length());
  235. 235 System.out.println("单次请求耗时ms:" + (System.currentTimeMillis() - startPer));
  236. 236 }
  237. 237 System.out.println("查询总耗时ms:" + (System.currentTimeMillis() - start));
  238. 238 }
  239. 239
  240. 240 public static void main1(String[] args) throws Exception {
  241. 241 long start = System.currentTimeMillis();
  242. 242 String url = "http://samworld.samonkey.com/v2_0/media/detail/343";
  243. 243 Map<String, String> params = new HashMap<String, String>();
  244. 244 params.put("userId", "F60D72944B9E236E7C7D219851DB5C62");
  245. 245 params.put("deviceType", "IOS");
  246. 246 params.put("version", "v3.1.3");
  247. 247 String userAgent = "VRStore/3.1.3 (iPhone; iOS 9.3.2; Scale/3.00)";
  248. 248 String contentType = "application/json;charset=UTF-8";
  249. 249 String acceptEncoding = "gzip, deflate";
  250. 250 String encoding = "UTF-8";
  251. 251 // String data = HttpPoolUtils.get(url, params, "application/json",
  252. 252 // userAgent, "UTF-8");
  253. 253 String data = "";
  254. 254 HttpGet httpGet = new HttpGet();
  255. 255 CloseableHttpResponse response = null;
  256. 256 try {
  257. 257 StringBuilder sb = new StringBuilder();
  258. 258 sb.append(url);
  259. 259 boolean first = true;
  260. 260 if (params != null) {
  261. 261 for (Entry<String, String> entry : params.entrySet()) {
  262. 262 if (first && !url.contains("?")) {
  263. 263 sb.append("?");
  264. 264 } else {
  265. 265 sb.append("&");
  266. 266 }
  267. 267 sb.append(entry.getKey());
  268. 268 sb.append("=");
  269. 269 String value = entry.getValue();
  270. 270 sb.append(URLEncoder.encode(value, "UTF-8"));
  271. 271 first = false;
  272. 272 }
  273. 273 }
  274. 274
  275. 275 // LOG.info("[HttpPoolUtils Get] begin invoke:" + sb.toString());
  276. 276 httpGet.setURI(new URI(sb.toString()));
  277. 277 RequestConfig requestConfig = RequestConfig.copy(DEFAULT_REQUESTCONFIG).build();
  278. 278 httpGet.setConfig(requestConfig);
  279. 279 if (contentType != null && contentType != "") {
  280. 280 httpGet.setHeader(HttpHeaders.CONTENT_TYPE, contentType);
  281. 281 } else {
  282. 282 httpGet.setHeader(HttpHeaders.CONTENT_TYPE, "text/html");
  283. 283 }
  284. 284 if (userAgent != null && userAgent != "") {
  285. 285 httpGet.setHeader(HttpHeaders.USER_AGENT, userAgent);
  286. 286 }
  287. 287 if (acceptEncoding != null && acceptEncoding != "") {
  288. 288 httpGet.setHeader(HttpHeaders.ACCEPT_ENCODING, acceptEncoding);
  289. 289 }
  290. 290 httpGet.setHeader(HttpHeaders.ACCEPT_LANGUAGE, "zh-Hans-CN;q=1, pl-PL;q=0.9");
  291. 291 httpGet.setHeader(HttpHeaders.ACCEPT, "*/*");
  292. 292 response = httpclient.execute(httpGet);
  293. 293 HttpEntity entity = response.getEntity();
  294. 294 if (entity != null) {
  295. 295 data = EntityUtils.toString(entity, encoding);
  296. 296 }
  297. 297 // LOG.debug(String.format("[HttpPoolUtils Get]Debug url:%s ,
  298. 298 // response data %s:",sb.toString(),data));
  299. 299 } catch (Exception e) {
  300. 300 LOG.error(String.format("[HttpPoolUtils Get]invoke get error, url:%s, para:%s", url, params.toString()), e);
  301. 301 } finally {
  302. 302 if (response != null) {
  303. 303 try {
  304. 304 EntityUtils.consume(response.getEntity());
  305. 305 } catch (IOException e) {
  306. 306 e.printStackTrace();
  307. 307 }
  308. 308 }
  309. 309 httpGet.reset();
  310. 310 }
  311. 311
  312. 312 System.out.println(data);
  313. 313 System.out.println("查询总耗时ms:" + (System.currentTimeMillis() - start));
  314. 314 }
  315. 315
  316. 316 public static void main(String[] args) throws Exception {
  317. 317 Map<String, String> param = new HashMap();
  318. 318 param.put("access_token", "70603C5A83DDC1507B00AC52E55C13EA");
  319. 319 param.put("unionid", "1");
  320. 320 String result = HttpPoolUtils.get("https://graph.qq.com/oauth2.0/me", param, null, null, HttpPoolUtils.DEFAULT_ENCODING);
  321. 321 System.out.println(result);
  322. 322
  323. 323 long start = System.currentTimeMillis();
  324. 324 long startPer = System.currentTimeMillis();
  325. 325 String url = "http://nextjoy.cn/programMan/list_demo.php";
  326. 326 String data = get(url, Collections.<String, String> emptyMap(), null, null, HttpPoolUtils.DEFAULT_ENCODING);
  327. 327 System.out.println("结果长度:" + data.length());
  328. 328 System.out.println("Data:" + data);
  329. 329 System.out.println("单次请求耗时ms:" + (System.currentTimeMillis() - startPer));
  330. 330 System.out.println("查询总耗时ms:" + (System.currentTimeMillis() - start));
  331. 331 }
  332. 332 }

 

这里用到的jar包:

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

 

版权声明:本文为lxn0216原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/lxn0216/p/9262835.html