|
@@ -1,15 +1,26 @@
|
|
|
package cn.com.qmth.examcloud.commons.util;
|
|
|
|
|
|
import java.io.InputStream;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
import org.apache.commons.io.IOUtils;
|
|
|
+import org.apache.http.HttpStatus;
|
|
|
import org.apache.http.client.config.RequestConfig;
|
|
|
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.client.methods.HttpPut;
|
|
|
+import org.apache.http.client.methods.HttpUriRequest;
|
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
import org.apache.http.impl.client.HttpClientBuilder;
|
|
|
+import org.apache.http.impl.client.HttpClients;
|
|
|
+import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
|
|
import org.apache.http.util.EntityUtils;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+
|
|
|
+import cn.com.qmth.examcloud.commons.helpers.JsonHttpResponseHolder;
|
|
|
import cn.com.qmth.examcloud.commons.logging.ExamCloudLog;
|
|
|
import cn.com.qmth.examcloud.commons.logging.ExamCloudLogFactory;
|
|
|
|
|
@@ -22,7 +33,85 @@ import cn.com.qmth.examcloud.commons.logging.ExamCloudLogFactory;
|
|
|
*/
|
|
|
public class HttpClientUtil {
|
|
|
|
|
|
- private static final ExamCloudLog LOGGER = ExamCloudLogFactory.getLog(HttpClientUtil.class);
|
|
|
+ private static final ExamCloudLog LOGGER = ExamCloudLogFactory.getLog("HTTP_CLIENT");
|
|
|
+
|
|
|
+ protected static ExamCloudLog log = ExamCloudLogFactory.getLog("HTTP_CLIENT");
|
|
|
+
|
|
|
+ private static CloseableHttpClient httpclient;
|
|
|
+
|
|
|
+ private static RequestConfig requestConfig;
|
|
|
+
|
|
|
+ static {
|
|
|
+ PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(60,
|
|
|
+ TimeUnit.SECONDS);
|
|
|
+ cm.setValidateAfterInactivity(1000);
|
|
|
+ cm.setMaxTotal(8000);
|
|
|
+ cm.setDefaultMaxPerRoute(200);
|
|
|
+ httpclient = HttpClients.custom().setConnectionManager(cm).disableAutomaticRetries()
|
|
|
+ .build();
|
|
|
+
|
|
|
+ requestConfig = RequestConfig.custom().setConnectionRequestTimeout(500)
|
|
|
+ .setSocketTimeout(10000).setConnectTimeout(10000).build();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static CloseableHttpClient getHttpClient() {
|
|
|
+ return httpclient;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static HttpPost buildHttpPost(String url) {
|
|
|
+ HttpPost httpPost = new HttpPost(url);
|
|
|
+ httpPost.setConfig(requestConfig);
|
|
|
+
|
|
|
+ return httpPost;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static HttpPut buildHttpPut(String url) {
|
|
|
+ HttpPut httpPut = new HttpPut(url);
|
|
|
+ httpPut.setConfig(requestConfig);
|
|
|
+
|
|
|
+ return httpPut;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static HttpGet buildHttpGet(String url) {
|
|
|
+ HttpGet httpGet = new HttpGet(url);
|
|
|
+ httpGet.setConfig(requestConfig);
|
|
|
+
|
|
|
+ return httpGet;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static JsonHttpResponseHolder execute(final HttpUriRequest request) {
|
|
|
+
|
|
|
+ CloseableHttpResponse response = null;
|
|
|
+ JsonHttpResponseHolder responseHolder = null;
|
|
|
+ long s = System.currentTimeMillis();
|
|
|
+ try {
|
|
|
+
|
|
|
+ response = httpclient.execute(request);
|
|
|
+ int statusCode = response.getStatusLine().getStatusCode();
|
|
|
+ String entityStr = EntityUtils.toString(response.getEntity(), "UTF-8");
|
|
|
+ JSONObject obj = JSON.parseObject(entityStr);
|
|
|
+ responseHolder = new JsonHttpResponseHolder(statusCode, obj);
|
|
|
+
|
|
|
+ if (HttpStatus.SC_OK != responseHolder.getStatusCode()) {
|
|
|
+ log.error("[HTTP-FAIL]. statusCode=" + statusCode + "; responseEntity=" + entityStr
|
|
|
+ + "; uri=" + request.getURI());
|
|
|
+ } else {
|
|
|
+ if (log.isDebugEnabled()) {
|
|
|
+ log.debug("[HTTP-OK]. statusCode=" + statusCode + "; responseEntity="
|
|
|
+ + entityStr + "; uri=" + request.getURI());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("[HTTP-ERROR]. uri=" + request.getURI(), e);
|
|
|
+ } finally {
|
|
|
+ IOUtils.closeQuietly(response);
|
|
|
+ log.debug("[HTTP-COST]. cost = " + (System.currentTimeMillis() - s) + " ms; uri="
|
|
|
+ + request.getURI());
|
|
|
+ }
|
|
|
+ return responseHolder;
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 关闭流
|