From 462da67a046e74db45b3b0168c1ef46106e6f929 Mon Sep 17 00:00:00 2001 From: liuchengqian Date: Mon, 6 Mar 2023 09:52:08 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/xkrs/utils/WDHttpClientUtils.java | 32 ++++++++++++++----- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/xkrs/utils/WDHttpClientUtils.java b/src/main/java/com/xkrs/utils/WDHttpClientUtils.java index 35dd6c9..ea6cb9c 100644 --- a/src/main/java/com/xkrs/utils/WDHttpClientUtils.java +++ b/src/main/java/com/xkrs/utils/WDHttpClientUtils.java @@ -16,6 +16,8 @@ import org.slf4j.LoggerFactory; */ public class WDHttpClientUtils { + private static final boolean printLog = false; + private static final Logger log = LoggerFactory.getLogger(HttpClientUtils.class); /** @@ -59,7 +61,9 @@ public class WDHttpClientUtils { * 封装get请求方式的处理 */ private static String doGet(String url, CloseableHttpClient httpClient) throws Exception { - log.info("HGet请求url={}", url); + if (printLog) { + log.info("HGet请求url={}", url); + } HttpGet httpGet = new HttpGet(url); return execute(httpClient, httpGet); } @@ -68,8 +72,10 @@ public class WDHttpClientUtils { * 封装post请求方式的处理 */ private static String doPost(CloseableHttpClient httpClient, String url, String params) throws Exception { - log.info("Post请求url:{}", url); - log.info("Post请求params:{}", params); + if (printLog) { + log.info("Post请求url:{}", url); + log.info("Post请求params:{}", params); + } HttpPost httpPost = new HttpPost(url); httpPost.addHeader("Content-Type", "application/json; charset=utf-8"); httpPost.setEntity(new StringEntity(params, "utf-8")); @@ -80,7 +86,9 @@ public class WDHttpClientUtils { * 封装post请求方式的处理 */ private static String doPost(CloseableHttpClient httpClient, String url) throws Exception { - log.info("Post请求url:{}", url); + if (printLog) { + log.info("Post请求url:{}", url); + } HttpPost httpPost = new HttpPost(url); httpPost.addHeader("Content-Type", "application/json; charset=utf-8"); return execute(httpClient, httpPost); @@ -95,15 +103,21 @@ public class WDHttpClientUtils { try { response = httpClient.execute(requestBase); int statusCode = response.getStatusLine().getStatusCode(); - log.info("HttpClient响应码={}", statusCode); + if (printLog) { + log.info("HttpClient响应码={}", statusCode); + } final int success = 200; if (statusCode == success) { result = EntityUtils.toString(response.getEntity(), "utf-8"); } else { - log.error("HttpClient请求失败,错误码={}", statusCode); + if (printLog) { + log.error("HttpClient请求失败,错误码={}", statusCode); + } } } catch (Exception e) { - log.error("HttpClient请求异常:", e); + if (printLog) { + log.error("HttpClient请求异常:", e); + } } finally { if (null != httpClient) { httpClient.close(); @@ -112,7 +126,9 @@ public class WDHttpClientUtils { response.close(); } } - log.info("HttpClient请求结果:{}", result); + if (printLog) { + log.info("HttpClient请求结果:{}", result); + } return result; }