From 25b806e384215c6f015370c9cacbd1b5ba62aa2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=83=A1=E6=A5=A0?= Date: Mon, 19 Aug 2019 12:44:06 +0800 Subject: [PATCH 1/2] =?UTF-8?q?refactor:=20HttpClient=20=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E5=AE=89=E5=85=A8,=20=E5=8F=AF=E4=BB=A5=E5=85=A8=E5=B1=80?= =?UTF-8?q?=E5=A4=8D=E7=94=A8,=20=E4=B9=9F=E5=8F=AF=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E7=9A=84=20httpClient=20=E5=88=B0?= =?UTF-8?q?=20WxClient.=20=E8=A7=81:=20https://hc.apache.org/httpcomponent?= =?UTF-8?q?s-client-ga/tutorial/html/fundamentals.html#d5e213?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + .../com/riversoft/weixin/common/WxClient.java | 61 +++++++++++-------- 2 files changed, 37 insertions(+), 25 deletions(-) diff --git a/.gitignore b/.gitignore index bc8fb14..3e9f6a9 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ *.ipr *.iws **/*.iml +**/*.DS_Store .project .classpath diff --git a/weixin-common/src/main/java/com/riversoft/weixin/common/WxClient.java b/weixin-common/src/main/java/com/riversoft/weixin/common/WxClient.java index 87add02..7c18717 100644 --- a/weixin-common/src/main/java/com/riversoft/weixin/common/WxClient.java +++ b/weixin-common/src/main/java/com/riversoft/weixin/common/WxClient.java @@ -21,7 +21,11 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.*; +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; import java.util.Map; import java.util.UUID; import java.util.regex.Matcher; @@ -34,13 +38,15 @@ public class WxClient { private static Logger logger = LoggerFactory.getLogger(WxClient.class); - protected CloseableHttpClient httpClient; + protected static CloseableHttpClient httpClient; private String clientId; private String clientSecret; private AccessTokenHolder accessTokenHolder; public WxClient() { - httpClient = HttpClients.createDefault(); + if (httpClient == null) { + httpClient = HttpClients.createDefault(); + } } public WxClient(String tokenUrl, String clientId, String clientSecret) { @@ -54,6 +60,10 @@ public WxClient(String clientId, String clientSecret, AccessTokenHolder accessTo httpClient = HttpClients.createDefault(); } + public static void setDefaultHttpClient(CloseableHttpClient httpClient) { + WxClient.httpClient = httpClient; + } + public String getClientId() { return clientId; } @@ -75,13 +85,13 @@ public String get(String url) { } public String get(String url, boolean needToken) { - if(needToken) { + if (needToken) { try { return httpGet(appendAccessToken(url)); } catch (Exception e) { - if(e instanceof WxRuntimeException) { + if (e instanceof WxRuntimeException) { WxRuntimeException wxRuntimeException = (WxRuntimeException) e; - if(invalidToken(wxRuntimeException.getCode())) { + if (invalidToken(wxRuntimeException.getCode())) { logger.warn("token invalid: {}, will refresh.", wxRuntimeException.getCode()); refreshToken(); return httpGet(appendAccessToken(url)); @@ -95,13 +105,13 @@ public String get(String url, boolean needToken) { } public byte[] getBinary(String url, boolean needToken) { - if(needToken) { + if (needToken) { try { return httpGetBinary(appendAccessToken(url)); } catch (Exception e) { - if(e instanceof WxRuntimeException) { + if (e instanceof WxRuntimeException) { WxRuntimeException wxRuntimeException = (WxRuntimeException) e; - if(invalidToken(wxRuntimeException.getCode())) { + if (invalidToken(wxRuntimeException.getCode())) { logger.warn("token invalid: {}, will refresh.", wxRuntimeException.getCode()); refreshToken(); return httpGetBinary(appendAccessToken(url)); @@ -172,9 +182,9 @@ public File download(String url) { try { return httpDownload(appendAccessToken(url)); } catch (Exception e) { - if(e instanceof WxRuntimeException) { + if (e instanceof WxRuntimeException) { WxRuntimeException wxRuntimeException = (WxRuntimeException) e; - if(invalidToken(wxRuntimeException.getCode())) { + if (invalidToken(wxRuntimeException.getCode())) { logger.warn("token invalid: {}, will refresh.", wxRuntimeException.getCode()); refreshToken(); return httpDownload(appendAccessToken(url)); @@ -188,9 +198,9 @@ public InputStream copyStream(String url, String post) { try { return httpCopyFromStream(appendAccessToken(url), post); } catch (Exception e) { - if(e instanceof WxRuntimeException) { + if (e instanceof WxRuntimeException) { WxRuntimeException wxRuntimeException = (WxRuntimeException) e; - if(invalidToken(wxRuntimeException.getCode())) { + if (invalidToken(wxRuntimeException.getCode())) { logger.warn("token invalid: {}, will refresh.", wxRuntimeException.getCode()); refreshToken(); return httpCopyFromStream(appendAccessToken(url), post); @@ -202,9 +212,10 @@ public InputStream copyStream(String url, String post) { /** * 永久素材下载使用,奇葩的下载方式 - * @param url - * @param post - * @return + * + * @param url request url + * @param post post content + * @return ByteArrayInputStream or Exception when error occured */ private InputStream httpCopyFromStream(String url, String post) { HttpPost httpPost = new HttpPost(url); @@ -223,7 +234,7 @@ private InputStream httpCopyFromStream(String url, String post) { } else { InputStream inputStream = entity.getContent(); byte[] binaryContent = IOUtils.toByteArray(inputStream); - String content = new String(binaryContent, "UTF-8"); + String content = new String(binaryContent, StandardCharsets.UTF_8); if (content.contains("errcode")) { WxError wxError = WxError.fromJson(content); throw new WxRuntimeException(wxError); @@ -260,7 +271,7 @@ private File httpDownload(String url) { return tempFile; } else { - String errors = entity == null ? null : EntityUtils.toString(entity, Consts.UTF_8); + String errors = entity == null ? "" : EntityUtils.toString(entity, Consts.UTF_8); logger.warn("download file : {} failed: {}", url, errors); if (errors.contains("errcode")) { WxError wxError = WxError.fromJson(errors); @@ -292,9 +303,9 @@ public String post(String url, String content) { try { return httpPost(appendAccessToken(url), content); } catch (Exception e) { - if(e instanceof WxRuntimeException) { + if (e instanceof WxRuntimeException) { WxRuntimeException wxRuntimeException = (WxRuntimeException) e; - if(invalidToken(wxRuntimeException.getCode())) { + if (invalidToken(wxRuntimeException.getCode())) { logger.warn("token invalid: {}, will refresh.", wxRuntimeException.getCode()); refreshToken(); return httpPost(appendAccessToken(url), content); @@ -317,9 +328,9 @@ public String post(String url, InputStream inputStream, String fileName, Map form) { if (file != null) { MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create(); multipartEntityBuilder.addBinaryBody("media", file).setMode(HttpMultipartMode.RFC6532); - if(form != null && !form.isEmpty()) { - for(String key: form.keySet()) { + if (form != null && !form.isEmpty()) { + for (String key : form.keySet()) { multipartEntityBuilder.addTextBody(key, form.get(key)); } } @@ -413,7 +424,7 @@ public AccessToken getAccessToken() { private boolean invalidToken(int code) { boolean result = code == 42001 || code == 40001 || code == 40014; - if(result) { + if (result) { accessTokenHolder.expireToken();//强制设置为无效 } return result; From 75cb4cd055f9e1471896b35c7efe3c46f21c7747 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=83=A1=E6=A5=A0?= Date: Mon, 19 Aug 2019 12:53:27 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20=E6=BC=8F=E6=8E=89=E4=BA=86=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E6=9E=84=E9=80=A0=E6=96=B9=E6=B3=95...?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/riversoft/weixin/common/WxClient.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/weixin-common/src/main/java/com/riversoft/weixin/common/WxClient.java b/weixin-common/src/main/java/com/riversoft/weixin/common/WxClient.java index 7c18717..e479545 100644 --- a/weixin-common/src/main/java/com/riversoft/weixin/common/WxClient.java +++ b/weixin-common/src/main/java/com/riversoft/weixin/common/WxClient.java @@ -54,10 +54,10 @@ public WxClient(String tokenUrl, String clientId, String clientSecret) { } public WxClient(String clientId, String clientSecret, AccessTokenHolder accessTokenHolder) { + this(); this.clientId = clientId; this.clientSecret = clientSecret; this.accessTokenHolder = accessTokenHolder; - httpClient = HttpClients.createDefault(); } public static void setDefaultHttpClient(CloseableHttpClient httpClient) {