entry : header.entrySet()) {
+ httpGet.addHeader(entry.getKey(), entry.getValue());
+ }
+ }
+ httpResponse = httpClient.execute(httpGet);
+ int statusCode = httpResponse.getStatusLine().getStatusCode();
+ if (statusCode == HttpStatus.SC_OK) {
+ HttpEntity resEntity = httpResponse.getEntity();
+ result = EntityUtils.toString(resEntity);
+ EntityUtils.consume(resEntity);
+ } else {
+ result = readHttpResponse(httpResponse);
+ }
+ } finally {
+ if (httpClient != null) {
+ httpClient.close();
+ }
+ if (httpResponse != null) {
+ HttpEntity httpEntity = httpResponse.getEntity();
+ if (httpEntity != null) {
+ EntityUtils.consume(httpEntity);
+ }
+ }
+ }
+ return result;
+ }
+
+ public static CloseableHttpClient getHttpClient() throws Exception {
+// thread = new HttpClientConnectionMonitorThread(cm);
+ CloseableHttpClient httpClient = HttpClients.custom()
+ .setSSLSocketFactory(sslsf)
+ .setConnectionManager(cm)
+ .setConnectionManagerShared(true)
+ .disableAutomaticRetries() //关闭自动处理重定向
+ .setRedirectStrategy(new LaxRedirectStrategy())//利用LaxRedirectStrategy处理POST重定向问题
+ .setDefaultRequestConfig(requestConfig)
+ .build();
+
+ return httpClient;
+ }
+
+ public static HttpPost getHttpPost(String url) {
+ HttpPost httpPost = new HttpPost(url);
+ httpPost.setConfig(requestConfig);
+ return httpPost;
+ }
+
+ private static String readHttpResponse(HttpResponse httpResponse)
+ throws ParseException, IOException {
+ StringBuilder builder = new StringBuilder();
+ // 获取响应消息实体
+ HttpEntity entity = httpResponse.getEntity();
+ // 响应状态
+ builder.append("status:" + httpResponse.getStatusLine());
+ builder.append("headers:");
+ HeaderIterator iterator = httpResponse.headerIterator();
+ while (iterator.hasNext()) {
+ builder.append("\t" + iterator.next());
+ }
+ // 判断响应实体是否为空
+ if (entity != null) {
+ String responseString = EntityUtils.toString(entity);
+ builder.append("response length:" + responseString.length());
+ builder.append("response content:" + responseString.replace("\r\n", ""));
+ }
+ return builder.toString();
+ }
+
+ public static void main(String[] args) throws Exception {
+
+
+// //用线程池发送请求
+// ExecutorService executor = Executors.newFixedThreadPool(3);
+//
+// Thread t1 = new Thread(new Runnable() {
+// @Override
+// public void run() {
+// for (int i = 0; i < 100; i++) {
+// HttpPost post = getHttpPost("http://127.0.0.1:8080/qhmz/receiveBankData.do");
+// System.out.println("请求第【" + (i + 1) + "】次:【receiveBankData】");
+// try {
+// CloseableHttpClient httpclient = UtilHttp.getHttpClient();
+// CloseableHttpResponse response2 = httpclient.execute(post);
+// System.out.println("请求第【" + (i + 1) + "】次:【" + post.getURI() + "】,结果" + response2.getStatusLine());
+// httpclient.close();
+// EntityUtils.consume(response2.getEntity());
+// } catch (Exception e) {
+// System.out.println("请求第【" + (i + 1) + "】次:【" + post.getURI() + "】" + UtilPub.getOrigMsg(e));
+// }
+//
+// }
+// }
+// });
+// Thread t2 = new Thread(new Runnable() {
+// @Override
+// public void run() {
+// for (int i = 0; i < 100; i++) {
+// HttpPost post = getHttpPost("http://127.0.0.1:8080/qhmz/cust/ebank/biz/service.do");
+// System.out.println("请求第【" + (i + 1) + "】次:【service】");
+// try {
+// CloseableHttpClient httpclient = UtilHttp.getHttpClient();
+// CloseableHttpResponse response2 = httpclient.execute(post);
+// System.out.println("请求第【" + (i + 1) + "】次:【" + post.getURI() + "】,结果" + response2.getStatusLine());
+// httpclient.close();
+// EntityUtils.consume(response2.getEntity());
+// } catch (Exception e) {
+// System.out.println("请求第【" + (i + 1) + "】次:【" + post.getURI() + "】" + UtilPub.getOrigMsg(e));
+// }
+//
+// }
+// }
+// });
+// Thread t3 = new Thread(new Runnable() {
+// @Override
+// public void run() {
+// for (int i = 0; i < 100; i++) {
+// HttpPost post = getHttpPost("http://127.0.0.1:8080/qhmz/cust/ebank/biz/isSupBank.do");
+// System.out.println("请求第【" + (i + 1) + "】次:【isSupBank】");
+// try {
+// CloseableHttpClient httpclient = UtilHttp.getHttpClient();
+// CloseableHttpResponse response2 = httpclient.execute(post);
+// System.out.println("请求第【" + (i + 1) + "】次:【" + post.getURI() + "】,结果" + response2.getStatusLine());
+// httpclient.close();
+// EntityUtils.consume(response2.getEntity());
+// } catch (Exception e) {
+// System.out.println("请求第【" + (i + 1) + "】次:【" + post.getURI() + "】" + UtilPub.getOrigMsg(e));
+// }
+//
+// }
+// }
+// });
+// executor.execute(t1);
+// executor.execute(t2);
+// executor.execute(t3);
+// executor.shutdown();
+ }
+
+ /**
+ * Description: 使用管理器,管理HTTP连接池 无效链接定期清理功能
+ *
+ * @author andy 2017年8月28日
+ */
+ public static class HttpClientConnectionMonitorThread extends Thread {
+
+ private final HttpClientConnectionManager connManager;
+ private volatile boolean shutdown;
+
+ public HttpClientConnectionMonitorThread(HttpClientConnectionManager connManager) {
+ super();
+ this.setName("http-connection-monitor");
+ this.setDaemon(true);
+ this.connManager = connManager;
+ this.start();
+ }
+
+ @Override
+ public void run() {
+ try {
+ while (!shutdown) {
+ synchronized (this) {
+ wait(5000); // 等待5秒
+ // 关闭过期的链接
+ connManager.closeExpiredConnections();
+ // 选择关闭 空闲30秒的链接
+ connManager.closeIdleConnections(30, TimeUnit.SECONDS);
+ }
+ }
+ } catch (InterruptedException ex) {
+ }
+ }
+
+ /**
+ * 方法描述: 停止 管理器 清理无效链接 (该方法当前暂时关闭)
+ *
+ * @author andy 2017年8月28日 下午1:45:18
+ */
+ @Deprecated
+ public void shutDownMonitor() {
+ synchronized (this) {
+ shutdown = true;
+ notifyAll();
+ }
+ }
+
+ }
+
+}