|
@@ -1,5 +1,6 @@
|
|
|
package api.common.util;
|
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.http.client.config.RequestConfig;
|
|
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
import org.apache.http.client.methods.HttpGet;
|
|
@@ -31,6 +32,7 @@ import java.util.Map;
|
|
|
* <version>${http.client.version}</version>
|
|
|
* </dependency>
|
|
|
*/
|
|
|
+@Slf4j
|
|
|
public class HttpUtil {
|
|
|
|
|
|
public static CloseableHttpClient getHttpClient() throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
|
|
@@ -63,27 +65,32 @@ public class HttpUtil {
|
|
|
|
|
|
|
|
|
/**
|
|
|
- * 普通 get 请求
|
|
|
+ * 默认请求头的 get 请求
|
|
|
*
|
|
|
* @param closeableHttpClient http 客户端
|
|
|
* @param requestConfig 请求配置
|
|
|
* @param url 请求路径带参数
|
|
|
* @return 请求结果
|
|
|
- * @throws IOException IO异常
|
|
|
*/
|
|
|
- public static String get(CloseableHttpClient closeableHttpClient, RequestConfig requestConfig, String url) throws IOException {
|
|
|
- //1 创建 post 请求
|
|
|
- HttpGet get = new HttpGet(url);
|
|
|
- get.setConfig(requestConfig);
|
|
|
- //2 发送请求
|
|
|
- CloseableHttpResponse response = closeableHttpClient.execute(get);
|
|
|
- //3 处理返回结果,如果状态码为200,就是正常返回
|
|
|
- int statusCode = response.getStatusLine().getStatusCode();
|
|
|
- if (statusCode == 200) {
|
|
|
- return EntityUtils.toString(response.getEntity());
|
|
|
- } else {
|
|
|
- throw new RuntimeException("------- 请求错误:" + statusCode);
|
|
|
+ public static String get(CloseableHttpClient closeableHttpClient, RequestConfig requestConfig, String url) {
|
|
|
+ String result = null;
|
|
|
+ try {
|
|
|
+ //1 创建 post 请求
|
|
|
+ HttpGet get = new HttpGet(url);
|
|
|
+ get.setConfig(requestConfig);
|
|
|
+ //2 发送请求
|
|
|
+ CloseableHttpResponse response = closeableHttpClient.execute(get);
|
|
|
+ //3 处理返回结果,如果状态码为200,就是正常返回
|
|
|
+ int statusCode = response.getStatusLine().getStatusCode();
|
|
|
+ if (statusCode == 200) {
|
|
|
+ result = EntityUtils.toString(response.getEntity());
|
|
|
+ } else {
|
|
|
+ throw new RuntimeException("------- 请求错误:" + statusCode);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.info("请求 " + url + " 失败。", e);
|
|
|
}
|
|
|
+ return result;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -129,7 +136,6 @@ public class HttpUtil {
|
|
|
public static InputStream getInputStream(CloseableHttpClient closeableHttpClient, RequestConfig requestConfig, String url) throws IOException {
|
|
|
return getInputStream(closeableHttpClient, requestConfig, url, null);
|
|
|
}
|
|
|
-
|
|
|
/**
|
|
|
* 自定义请求头的 get 请求下载文件
|
|
|
*
|
|
@@ -139,24 +145,29 @@ public class HttpUtil {
|
|
|
* @throws IOException 异常
|
|
|
*/
|
|
|
public static InputStream getInputStream(CloseableHttpClient closeableHttpClient, RequestConfig requestConfig, String url, Map<String, String> headers) throws IOException {
|
|
|
- //1 创建 post 请求
|
|
|
- HttpGet get = new HttpGet(url);
|
|
|
- //2 设置请求默认配置
|
|
|
- get.setConfig(requestConfig);
|
|
|
- //3 设置请求头 post.setHeader("Content-type", "application/json; charset=utf-8");
|
|
|
- if (!CollectionUtil.isEmpty(headers)) {
|
|
|
- headers.forEach(get::setHeader);
|
|
|
- }
|
|
|
- //4 发送请求
|
|
|
- CloseableHttpResponse response = closeableHttpClient.execute(get);
|
|
|
- //5 处理返回结果,
|
|
|
- //5-1 如果状态码为200,就是正常返回
|
|
|
- int statusCode = response.getStatusLine().getStatusCode();
|
|
|
- if (statusCode == 200) {
|
|
|
- return response.getEntity().getContent();
|
|
|
- } else {
|
|
|
- throw new RuntimeException("------- 请求错误:" + statusCode);
|
|
|
+ InputStream result = null;
|
|
|
+ try {
|
|
|
+ //1 创建 post 请求
|
|
|
+ HttpGet get = new HttpGet(url);
|
|
|
+ //2 设置请求默认配置
|
|
|
+ get.setConfig(requestConfig);
|
|
|
+ //3 设置请求头 post.setHeader("Content-type", "application/json; charset=utf-8");
|
|
|
+ if (!CollectionUtil.isEmpty(headers)) {
|
|
|
+ headers.forEach(get::setHeader);
|
|
|
+ }
|
|
|
+ //4 发送请求
|
|
|
+ CloseableHttpResponse response = closeableHttpClient.execute(get);
|
|
|
+ //5 处理返回结果,
|
|
|
+ //5-1 如果状态码为200,就是正常返回
|
|
|
+ int statusCode = response.getStatusLine().getStatusCode();
|
|
|
+ log.info("请求 " + url + " 状态码为:" + statusCode);
|
|
|
+ if (statusCode == 200) {
|
|
|
+ result = response.getEntity().getContent();
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.info("请求 " + url + " 失败。", e);
|
|
|
}
|
|
|
+ return result;
|
|
|
}
|
|
|
|
|
|
/**
|