|
@@ -21,6 +21,7 @@ import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
|
|
|
import org.apache.http.client.methods.HttpGet;
|
|
import org.apache.http.client.methods.HttpGet;
|
|
|
import org.apache.http.client.methods.HttpPost;
|
|
import org.apache.http.client.methods.HttpPost;
|
|
|
import org.apache.http.client.methods.HttpPut;
|
|
import org.apache.http.client.methods.HttpPut;
|
|
|
|
|
+import org.apache.http.client.methods.HttpUriRequest;
|
|
|
import org.apache.http.config.Registry;
|
|
import org.apache.http.config.Registry;
|
|
|
import org.apache.http.config.RegistryBuilder;
|
|
import org.apache.http.config.RegistryBuilder;
|
|
|
import org.apache.http.config.SocketConfig;
|
|
import org.apache.http.config.SocketConfig;
|
|
@@ -61,6 +62,7 @@ import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
import java.util.Objects;
|
|
import java.util.Objects;
|
|
|
import java.util.Optional;
|
|
import java.util.Optional;
|
|
|
|
|
+import java.util.function.Consumer;
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -103,6 +105,9 @@ public class RestApiUtils {
|
|
|
.build();
|
|
.build();
|
|
|
|
|
|
|
|
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
|
|
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
|
|
|
|
|
+ // 连接池大小
|
|
|
|
|
+ cm.setMaxTotal(220);
|
|
|
|
|
+ cm.setDefaultMaxPerRoute((int)(cm.getMaxTotal() * 0.8));
|
|
|
httpClient = HttpClients.custom()
|
|
httpClient = HttpClients.custom()
|
|
|
.setConnectionManager(cm)
|
|
.setConnectionManager(cm)
|
|
|
.setDefaultCookieStore(cookieStore)
|
|
.setDefaultCookieStore(cookieStore)
|
|
@@ -269,6 +274,59 @@ public class RestApiUtils {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 执行 execute 返回 Map 数据对象
|
|
|
|
|
+ * @param request
|
|
|
|
|
+ * @param not200ThrowError
|
|
|
|
|
+ * @return
|
|
|
|
|
+ * @throws IOException
|
|
|
|
|
+ */
|
|
|
|
|
+ public static Map<String, Object> execute(HttpUriRequest request, boolean not200ThrowError) throws IOException {
|
|
|
|
|
+ CloseableHttpResponse resp = null;
|
|
|
|
|
+ try {
|
|
|
|
|
+ resp = httpClient.execute(request);
|
|
|
|
|
+ log.info("execute[{}] url {} return code: {}", request.getMethod(), request.getURI(), resp.getStatusLine().getStatusCode());
|
|
|
|
|
+ HttpEntity entity1 = resp.getEntity();
|
|
|
|
|
+ String result = EntityUtils.toString(entity1);
|
|
|
|
|
+ EntityUtils.consume(entity1);
|
|
|
|
|
+ if(not200ThrowError && resp.getStatusLine().getStatusCode() != 200) {
|
|
|
|
|
+ throw new IOException(result);
|
|
|
|
|
+ }
|
|
|
|
|
+ Object jsonResult = JSON.parse(result);
|
|
|
|
|
+ JSONObject jsonObject = new JSONObject(2);
|
|
|
|
|
+ if(jsonResult instanceof JSONArray) {
|
|
|
|
|
+ jsonObject.put("result", jsonResult);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ jsonObject = (JSONObject) jsonResult;
|
|
|
|
|
+ }
|
|
|
|
|
+ jsonObject.put("status_code", resp.getStatusLine().getStatusCode());
|
|
|
|
|
+ return jsonObject;
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ if(resp != null) {
|
|
|
|
|
+ resp.close();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 执行任意 request,并采用回调 consumer
|
|
|
|
|
+ * @param request
|
|
|
|
|
+ * @param consumer
|
|
|
|
|
+ * @throws IOException
|
|
|
|
|
+ */
|
|
|
|
|
+ public static void execute(HttpUriRequest request, Consumer<HttpResponse> consumer) throws IOException {
|
|
|
|
|
+ CloseableHttpResponse resp = null;
|
|
|
|
|
+ try {
|
|
|
|
|
+ resp = httpClient.execute(request);
|
|
|
|
|
+ log.info("execute[{}] url {} return code: {}", request.getMethod(), request.getURI(), resp.getStatusLine().getStatusCode());
|
|
|
|
|
+ consumer.accept(resp);
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ if (resp != null) {
|
|
|
|
|
+ resp.close();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
|
|
|
//---------
|
|
//---------
|
|
|
/**
|
|
/**
|