HttpPoolUtils 连接池管理的GET POST请求

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

 

这里用到的jar包:

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

 

posted @ 2018-07-04 13:28  爱,诗意永存  阅读(1680)  评论(0编辑  收藏  举报