Skip to content

Commit d759e69

Browse files
committed
fix: #256, #172 CloseableHttpResponse 使用外没有关闭的问题
1 parent 06e4dda commit d759e69

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpServiceImpl.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,15 @@ public String getAccessToken(boolean forceRefresh) throws WxErrorException {
158158
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
159159
httpGet.setConfig(config);
160160
}
161-
CloseableHttpResponse response = getHttpclient().execute(httpGet);
162-
String resultContent = new BasicResponseHandler().handleResponse(response);
163-
WxError error = WxError.fromJson(resultContent);
164-
if (error.getErrorCode() != 0) {
165-
throw new WxErrorException(error);
161+
try (CloseableHttpResponse response = getHttpclient().execute(httpGet)) {
162+
String resultContent = new BasicResponseHandler().handleResponse(response);
163+
WxError error = WxError.fromJson(resultContent);
164+
if (error.getErrorCode() != 0) {
165+
throw new WxErrorException(error);
166+
}
167+
WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
168+
wxMpConfigStorage.updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
166169
}
167-
WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
168-
wxMpConfigStorage.updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
169170
} catch (ClientProtocolException e) {
170171
throw new RuntimeException(e);
171172
} catch (IOException e) {
@@ -851,8 +852,7 @@ public WxMpPrepayIdResult getPrepayId(final Map<String, String> parameters) {
851852

852853
StringEntity entity = new StringEntity(request.toString(), Consts.UTF_8);
853854
httpPost.setEntity(entity);
854-
try {
855-
CloseableHttpResponse response = getHttpclient().execute(httpPost);
855+
try(CloseableHttpResponse response = getHttpclient().execute(httpPost)) {
856856
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
857857
XStream xstream = XStreamInitializer.getInstance();
858858
xstream.alias("xml", WxMpPrepayIdResult.class);
@@ -944,8 +944,7 @@ else if (outTradeNo != null && !"".equals(outTradeNo.trim()))
944944

945945
StringEntity entity = new StringEntity(request.toString(), Consts.UTF_8);
946946
httpPost.setEntity(entity);
947-
try {
948-
CloseableHttpResponse response = httpClient.execute(httpPost);
947+
try(CloseableHttpResponse response = httpClient.execute(httpPost)) {
949948
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
950949
XStream xstream = XStreamInitializer.getInstance();
951950
xstream.alias("xml", WxMpPayResult.class);
@@ -1000,8 +999,7 @@ public WxRedpackResult sendRedpack(Map<String, String> parameters) throws WxErro
1000999

10011000
StringEntity entity = new StringEntity(request.toString(), Consts.UTF_8);
10021001
httpPost.setEntity(entity);
1003-
try {
1004-
CloseableHttpResponse response = getHttpclient().execute(httpPost);
1002+
try(CloseableHttpResponse response = getHttpclient().execute(httpPost)) {
10051003
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
10061004
XStream xstream = XStreamInitializer.getInstance();
10071005
xstream.processAnnotations(WxRedpackResult.class);

0 commit comments

Comments
 (0)