This commit is contained in:
liuchengqian 2023-03-06 09:52:08 +08:00
parent 486d0d80c6
commit 462da67a04

View File

@ -16,6 +16,8 @@ import org.slf4j.LoggerFactory;
*/ */
public class WDHttpClientUtils { public class WDHttpClientUtils {
private static final boolean printLog = false;
private static final Logger log = LoggerFactory.getLogger(HttpClientUtils.class); private static final Logger log = LoggerFactory.getLogger(HttpClientUtils.class);
/** /**
@ -59,7 +61,9 @@ public class WDHttpClientUtils {
* 封装get请求方式的处理 * 封装get请求方式的处理
*/ */
private static String doGet(String url, CloseableHttpClient httpClient) throws Exception { private static String doGet(String url, CloseableHttpClient httpClient) throws Exception {
if (printLog) {
log.info("HGet请求url={}", url); log.info("HGet请求url={}", url);
}
HttpGet httpGet = new HttpGet(url); HttpGet httpGet = new HttpGet(url);
return execute(httpClient, httpGet); return execute(httpClient, httpGet);
} }
@ -68,8 +72,10 @@ public class WDHttpClientUtils {
* 封装post请求方式的处理 * 封装post请求方式的处理
*/ */
private static String doPost(CloseableHttpClient httpClient, String url, String params) throws Exception { private static String doPost(CloseableHttpClient httpClient, String url, String params) throws Exception {
if (printLog) {
log.info("Post请求url{}", url); log.info("Post请求url{}", url);
log.info("Post请求params{}", params); log.info("Post请求params{}", params);
}
HttpPost httpPost = new HttpPost(url); HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Content-Type", "application/json; charset=utf-8"); httpPost.addHeader("Content-Type", "application/json; charset=utf-8");
httpPost.setEntity(new StringEntity(params, "utf-8")); httpPost.setEntity(new StringEntity(params, "utf-8"));
@ -80,7 +86,9 @@ public class WDHttpClientUtils {
* 封装post请求方式的处理 * 封装post请求方式的处理
*/ */
private static String doPost(CloseableHttpClient httpClient, String url) throws Exception { private static String doPost(CloseableHttpClient httpClient, String url) throws Exception {
if (printLog) {
log.info("Post请求url{}", url); log.info("Post请求url{}", url);
}
HttpPost httpPost = new HttpPost(url); HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Content-Type", "application/json; charset=utf-8"); httpPost.addHeader("Content-Type", "application/json; charset=utf-8");
return execute(httpClient, httpPost); return execute(httpClient, httpPost);
@ -95,15 +103,21 @@ public class WDHttpClientUtils {
try { try {
response = httpClient.execute(requestBase); response = httpClient.execute(requestBase);
int statusCode = response.getStatusLine().getStatusCode(); int statusCode = response.getStatusLine().getStatusCode();
if (printLog) {
log.info("HttpClient响应码={}", statusCode); log.info("HttpClient响应码={}", statusCode);
}
final int success = 200; final int success = 200;
if (statusCode == success) { if (statusCode == success) {
result = EntityUtils.toString(response.getEntity(), "utf-8"); result = EntityUtils.toString(response.getEntity(), "utf-8");
} else { } else {
if (printLog) {
log.error("HttpClient请求失败错误码={}", statusCode); log.error("HttpClient请求失败错误码={}", statusCode);
} }
}
} catch (Exception e) { } catch (Exception e) {
if (printLog) {
log.error("HttpClient请求异常", e); log.error("HttpClient请求异常", e);
}
} finally { } finally {
if (null != httpClient) { if (null != httpClient) {
httpClient.close(); httpClient.close();
@ -112,7 +126,9 @@ public class WDHttpClientUtils {
response.close(); response.close();
} }
} }
if (printLog) {
log.info("HttpClient请求结果{}", result); log.info("HttpClient请求结果{}", result);
}
return result; return result;
} }