diff --git a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/weixin/WeixinRequestExecutor.java b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/weixin/WeixinRequestExecutor.java index 8f4eb32d..24153148 100644 --- a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/weixin/WeixinRequestExecutor.java +++ b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/weixin/WeixinRequestExecutor.java @@ -132,13 +132,11 @@ public class WeixinRequestExecutor { if (logger.isEnabled(InternalLogLevel.DEBUG)) { logger.debug("weixin request >> " + request.getMethod() + " " + request.getURI().toString()); - } - if(request.getEntity() instanceof StringEntity){ - logger.debug("weixin request body >> " + ((StringEntity) request.getEntity()).getContentString()); + printHttpRequest(request,InternalLogLevel.DEBUG); } HttpResponse httpResponse = httpClient.execute(request); WeixinResponse response = new WeixinResponse(httpResponse); - handleResponse(response); + handleResponse(request,response); return response; } catch (HttpClientException e) { throw new WeixinException(e); @@ -170,7 +168,7 @@ public class WeixinRequestExecutor { * 微信请求响应 * @throws WeixinException */ - protected void handleResponse(WeixinResponse response) + protected void handleResponse(HttpRequest request,WeixinResponse response) throws WeixinException { boolean hasStreamMimeType = hasStreamMimeType(response); if (logger.isEnabled(InternalLogLevel.DEBUG)) { @@ -187,6 +185,7 @@ public class WeixinRequestExecutor { ApiResult result = response.getAsResult(); if (!SUCCESS_CODE.contains(String.format(",%s,", result.getReturnCode() .toLowerCase()))) { + printHttpRequest(request,InternalLogLevel.WARN); throw new WeixinException(result.getReturnCode(), result.getReturnMsg(),WeixinErrorUtil2.getText(result.getReturnCode())); } @@ -196,6 +195,7 @@ public class WeixinRequestExecutor { XmlResult.class, response); if (!SUCCESS_CODE.contains(String.format(",%s,", xmlResult .getResultCode().toLowerCase()))) { + printHttpRequest(request,InternalLogLevel.WARN); throw new WeixinException(xmlResult.getErrCode(), xmlResult.getErrCodeDes(),WeixinErrorUtil2.getText(xmlResult.getErrCode())); } @@ -205,6 +205,18 @@ public class WeixinRequestExecutor { } } + private void printHttpRequest(HttpRequest request,InternalLogLevel level){ + if(request.getEntity() instanceof StringEntity){ + if(level.equals(InternalLogLevel.DEBUG)) { + logger.debug("weixin request body >> " + ((StringEntity) request.getEntity()).getContentString()); + }else if(level.equals(InternalLogLevel.INFO)){ + logger.info("weixin request body >> " + ((StringEntity) request.getEntity()).getContentString()); + }else if(level.equals(InternalLogLevel.WARN)){ + logger.warn("weixin request body >> " + ((StringEntity) request.getEntity()).getContentString()); + } + } + } + public HttpClient getExecuteClient() { return httpClient; }