diff --git a/src/main/java/com/xkrs/utils/HttpClientUtils.java b/src/main/java/com/xkrs/utils/HttpClientUtils.java index 491a3ef..045f3f5 100644 --- a/src/main/java/com/xkrs/utils/HttpClientUtils.java +++ b/src/main/java/com/xkrs/utils/HttpClientUtils.java @@ -33,6 +33,8 @@ import java.security.cert.X509Certificate; */ public class HttpClientUtils { + private static final boolean printLog = false; + private static final Logger log = LoggerFactory.getLogger(HttpClientUtils.class); /** @@ -79,8 +81,10 @@ public class HttpClientUtils { } private static String doPostTextPlain(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"); StringEntity requestEntity = new StringEntity(params, "UTF-8"); @@ -110,7 +114,9 @@ public class HttpClientUtils { * 封装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); } @@ -119,8 +125,10 @@ public class HttpClientUtils { * 封装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)); @@ -136,15 +144,21 @@ public class HttpClientUtils { 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(); @@ -153,7 +167,9 @@ public class HttpClientUtils { response.close(); } } - log.info("HttpClient请求结果:{}", result); + if (printLog) { + log.info("HttpClient请求结果:{}", result); + } return result; }