Skip to content

Commit 8bacc94

Browse files
authored
🎨 修复CloseableHttpClient相关的误用代码
1 parent 3e1a38a commit 8bacc94

File tree

41 files changed

+240
-443
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+240
-443
lines changed

weixin-java-channel/src/main/java/me/chanjar/weixin/channel/api/impl/WxChannelServiceHttpClientImpl.java

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
package me.chanjar.weixin.channel.api.impl;
22

3-
import static me.chanjar.weixin.channel.constant.WxChannelApiUrlConstants.GET_ACCESS_TOKEN_URL;
4-
import static me.chanjar.weixin.channel.constant.WxChannelApiUrlConstants.GET_STABLE_ACCESS_TOKEN_URL;
5-
6-
import java.io.IOException;
73
import lombok.extern.slf4j.Slf4j;
84
import me.chanjar.weixin.channel.bean.token.StableTokenParam;
95
import me.chanjar.weixin.channel.config.WxChannelConfig;
106
import me.chanjar.weixin.channel.util.JsonUtils;
117
import me.chanjar.weixin.common.util.http.HttpType;
8+
import me.chanjar.weixin.common.util.http.apache.ApacheBasicResponseHandler;
129
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
1310
import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder;
1411
import org.apache.commons.lang3.StringUtils;
1512
import org.apache.http.HttpHost;
1613
import org.apache.http.client.HttpClient;
1714
import org.apache.http.client.config.RequestConfig;
18-
import org.apache.http.client.methods.CloseableHttpResponse;
1915
import org.apache.http.client.methods.HttpGet;
2016
import org.apache.http.client.methods.HttpPost;
2117
import org.apache.http.entity.ContentType;
2218
import org.apache.http.entity.StringEntity;
23-
import org.apache.http.impl.client.BasicResponseHandler;
2419
import org.apache.http.impl.client.CloseableHttpClient;
2520

21+
import java.io.IOException;
22+
23+
import static me.chanjar.weixin.channel.constant.WxChannelApiUrlConstants.GET_ACCESS_TOKEN_URL;
24+
import static me.chanjar.weixin.channel.constant.WxChannelApiUrlConstants.GET_STABLE_ACCESS_TOKEN_URL;
25+
2626
/**
2727
* @author <a href="https://github.com/lixize">Zeyes</a>
2828
*/
@@ -76,27 +76,12 @@ protected String doGetAccessTokenRequest() throws IOException {
7676

7777
url = String.format(url, config.getAppid(), config.getSecret());
7878

79-
HttpGet httpGet = null;
80-
CloseableHttpResponse response = null;
81-
try {
82-
httpGet = new HttpGet(url);
83-
if (this.getRequestHttpProxy() != null) {
84-
RequestConfig requestConfig = RequestConfig.custom().setProxy(this.getRequestHttpProxy()).build();
85-
httpGet.setConfig(requestConfig);
86-
}
87-
response = getRequestHttpClient().execute(httpGet);
88-
return new BasicResponseHandler().handleResponse(response);
89-
} finally {
90-
if (httpGet != null) {
91-
httpGet.releaseConnection();
92-
}
93-
if (response != null) {
94-
try {
95-
response.close();
96-
} catch (IOException ignored) {
97-
}
98-
}
79+
HttpGet httpGet = new HttpGet(url);
80+
if (this.getRequestHttpProxy() != null) {
81+
RequestConfig requestConfig = RequestConfig.custom().setProxy(this.getRequestHttpProxy()).build();
82+
httpGet.setConfig(requestConfig);
9983
}
84+
return getRequestHttpClient().execute(httpGet, ApacheBasicResponseHandler.INSTANCE);
10085
}
10186

10287
/**
@@ -125,10 +110,6 @@ protected String doGetStableAccessTokenRequest(boolean forceRefresh) throws IOEx
125110
assert requestJson != null;
126111

127112
httpPost.setEntity(new StringEntity(requestJson, ContentType.APPLICATION_JSON));
128-
try (CloseableHttpResponse response = getRequestHttpClient().execute(httpPost)) {
129-
return new BasicResponseHandler().handleResponse(response);
130-
} finally {
131-
httpPost.releaseConnection();
132-
}
113+
return getRequestHttpClient().execute(httpPost, ApacheBasicResponseHandler.INSTANCE);
133114
}
134115
}

weixin-java-channel/src/main/java/me/chanjar/weixin/channel/executor/ChannelFileUploadRequestExecutor.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
package me.chanjar.weixin.channel.executor;
22

3-
4-
import java.io.File;
5-
import java.io.IOException;
6-
73
import me.chanjar.weixin.common.enums.WxType;
84
import me.chanjar.weixin.common.error.WxErrorException;
95
import me.chanjar.weixin.common.util.http.RequestExecutor;
@@ -13,12 +9,14 @@
139
import org.apache.http.HttpEntity;
1410
import org.apache.http.HttpHost;
1511
import org.apache.http.client.config.RequestConfig;
16-
import org.apache.http.client.methods.CloseableHttpResponse;
1712
import org.apache.http.client.methods.HttpPost;
1813
import org.apache.http.entity.mime.HttpMultipartMode;
1914
import org.apache.http.entity.mime.MultipartEntityBuilder;
2015
import org.apache.http.impl.client.CloseableHttpClient;
2116

17+
import java.io.File;
18+
import java.io.IOException;
19+
2220
/**
2321
* 视频号小店 图片上传接口 请求的参数是File, 返回的结果是String
2422
*
@@ -47,11 +45,7 @@ public String execute(String uri, File file, WxType wxType) throws WxErrorExcept
4745
.build();
4846
httpPost.setEntity(entity);
4947
}
50-
try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) {
51-
return Utf8ResponseHandler.INSTANCE.handleResponse(response);
52-
} finally {
53-
httpPost.releaseConnection();
54-
}
48+
return requestHttp.getRequestHttpClient().execute(httpPost, Utf8ResponseHandler.INSTANCE);
5549
}
5650

5751
@Override
@@ -60,6 +54,7 @@ public void execute(String uri, File data, ResponseHandler<String> handler, WxTy
6054
handler.handle(this.execute(uri, data, wxType));
6155
}
6256

57+
@SuppressWarnings("unchecked")
6358
public static RequestExecutor<String, File> create(RequestHttp<?, ?> requestHttp) throws WxErrorException {
6459
switch (requestHttp.getRequestType()) {
6560
case APACHE_HTTP:

weixin-java-channel/src/main/java/me/chanjar/weixin/channel/executor/ChannelMediaDownloadRequestExecutor.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
11
package me.chanjar.weixin.channel.executor;
22

3-
import static org.apache.commons.io.FileUtils.openOutputStream;
4-
5-
import java.io.File;
6-
import java.io.IOException;
7-
import java.io.InputStream;
8-
import java.io.OutputStream;
9-
import java.util.UUID;
10-
import java.util.regex.Matcher;
11-
import java.util.regex.Pattern;
123
import lombok.extern.slf4j.Slf4j;
134
import me.chanjar.weixin.channel.bean.image.ChannelImageResponse;
145
import me.chanjar.weixin.channel.util.JsonUtils;
@@ -30,6 +21,16 @@
3021
import org.apache.http.entity.ContentType;
3122
import org.apache.http.impl.client.CloseableHttpClient;
3223

24+
import java.io.File;
25+
import java.io.IOException;
26+
import java.io.InputStream;
27+
import java.io.OutputStream;
28+
import java.util.UUID;
29+
import java.util.regex.Matcher;
30+
import java.util.regex.Pattern;
31+
32+
import static org.apache.commons.io.FileUtils.openOutputStream;
33+
3334
/**
3435
* 下载媒体文件请求执行器
3536
*
@@ -90,10 +91,7 @@ public ChannelImageResponse execute(String uri, String data, WxType wxType) thro
9091
extension = "unknown";
9192
}
9293
File file = createTmpFile(inputStream, baseName, extension, tmpDirFile);
93-
ChannelImageResponse result = new ChannelImageResponse(file, contentType);
94-
return result;
95-
} finally {
96-
httpGet.releaseConnection();
94+
return new ChannelImageResponse(file, contentType);
9795
}
9896
}
9997

weixin-java-common/src/main/java/me/chanjar/weixin/common/executor/CommonUploadRequestExecutorApacheImpl.java

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
import me.chanjar.weixin.common.error.WxErrorException;
99
import me.chanjar.weixin.common.util.http.RequestHttp;
1010
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
11+
import org.apache.commons.lang3.StringUtils;
1112
import org.apache.http.HttpEntity;
1213
import org.apache.http.HttpHost;
1314
import org.apache.http.client.config.RequestConfig;
14-
import org.apache.http.client.methods.CloseableHttpResponse;
1515
import org.apache.http.client.methods.HttpPost;
1616
import org.apache.http.entity.ContentType;
1717
import org.apache.http.entity.mime.HttpMultipartMode;
@@ -52,19 +52,15 @@ public String execute(String uri, CommonUploadParam param, WxType wxType) throws
5252
.build();
5353
httpPost.setEntity(entity);
5454
}
55-
try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) {
56-
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
57-
if (responseContent == null || responseContent.isEmpty()) {
58-
throw new WxErrorException(String.format("上传失败,服务器响应空 url:%s param:%s", uri, param));
59-
}
60-
WxError error = WxError.fromJson(responseContent, wxType);
61-
if (error.getErrorCode() != 0) {
62-
throw new WxErrorException(error);
63-
}
64-
return responseContent;
65-
} finally {
66-
httpPost.releaseConnection();
55+
String responseContent = requestHttp.getRequestHttpClient().execute(httpPost, Utf8ResponseHandler.INSTANCE);
56+
if (StringUtils.isEmpty(responseContent)) {
57+
throw new WxErrorException(String.format("上传失败,服务器响应空 url:%s param:%s", uri, param));
6758
}
59+
WxError error = WxError.fromJson(responseContent, wxType);
60+
if (error.getErrorCode() != 0) {
61+
throw new WxErrorException(error);
62+
}
63+
return responseContent;
6864
}
6965

7066
/**

weixin-java-common/src/main/java/me/chanjar/weixin/common/requestexecuter/ocr/OcrDiscernApacheHttpRequestExecutor.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import org.apache.http.HttpEntity;
99
import org.apache.http.HttpHost;
1010
import org.apache.http.client.config.RequestConfig;
11-
import org.apache.http.client.methods.CloseableHttpResponse;
1211
import org.apache.http.client.methods.HttpPost;
1312
import org.apache.http.entity.mime.HttpMultipartMode;
1413
import org.apache.http.entity.mime.MultipartEntityBuilder;
@@ -43,15 +42,11 @@ public String execute(String uri, File file, WxType wxType) throws WxErrorExcept
4342
.build();
4443
httpPost.setEntity(entity);
4544
}
46-
try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) {
47-
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
48-
WxError error = WxError.fromJson(responseContent, wxType);
49-
if (error.getErrorCode() != 0) {
50-
throw new WxErrorException(error);
51-
}
52-
return responseContent;
53-
} finally {
54-
httpPost.releaseConnection();
45+
String responseContent = requestHttp.getRequestHttpClient().execute(httpPost, Utf8ResponseHandler.INSTANCE);
46+
WxError error = WxError.fromJson(responseContent, wxType);
47+
if (error.getErrorCode() != 0) {
48+
throw new WxErrorException(error);
5549
}
50+
return responseContent;
5651
}
5752
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package me.chanjar.weixin.common.util.http.apache;
2+
3+
import org.apache.http.impl.client.BasicResponseHandler;
4+
5+
public class ApacheBasicResponseHandler extends BasicResponseHandler {
6+
7+
public static final ApacheBasicResponseHandler INSTANCE = new ApacheBasicResponseHandler();
8+
9+
}

weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/apache/ApacheMediaDownloadRequestExecutor.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,7 @@ public File execute(String uri, String queryParam, WxType wxType) throws WxError
6868
baseName = String.valueOf(System.currentTimeMillis());
6969
}
7070

71-
return FileUtils.createTmpFile(inputStream, baseName, FilenameUtils.getExtension(fileName),
72-
super.tmpDirFile);
73-
74-
} finally {
75-
httpGet.releaseConnection();
71+
return FileUtils.createTmpFile(inputStream, baseName, FilenameUtils.getExtension(fileName), super.tmpDirFile);
7672
}
7773
}
7874

weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/apache/ApacheMediaInputStreamUploadRequestExecutor.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import org.apache.http.HttpEntity;
1111
import org.apache.http.HttpHost;
1212
import org.apache.http.client.config.RequestConfig;
13-
import org.apache.http.client.methods.CloseableHttpResponse;
1413
import org.apache.http.client.methods.HttpPost;
1514
import org.apache.http.entity.ContentType;
1615
import org.apache.http.entity.mime.HttpMultipartMode;
@@ -45,15 +44,11 @@ public WxMediaUploadResult execute(String uri, InputStreamData data, WxType wxTy
4544
.build();
4645
httpPost.setEntity(entity);
4746
}
48-
try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) {
49-
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
50-
WxError error = WxError.fromJson(responseContent, wxType);
51-
if (error.getErrorCode() != 0) {
52-
throw new WxErrorException(error);
53-
}
54-
return WxMediaUploadResult.fromJson(responseContent);
55-
} finally {
56-
httpPost.releaseConnection();
47+
String responseContent = requestHttp.getRequestHttpClient().execute(httpPost, Utf8ResponseHandler.INSTANCE);
48+
WxError error = WxError.fromJson(responseContent, wxType);
49+
if (error.getErrorCode() != 0) {
50+
throw new WxErrorException(error);
5751
}
52+
return WxMediaUploadResult.fromJson(responseContent);
5853
}
5954
}

weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/apache/ApacheMediaUploadRequestExecutor.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package me.chanjar.weixin.common.util.http.apache;
22

3-
import me.chanjar.weixin.common.enums.WxType;
43
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
4+
import me.chanjar.weixin.common.enums.WxType;
55
import me.chanjar.weixin.common.error.WxError;
66
import me.chanjar.weixin.common.error.WxErrorException;
77
import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor;
88
import me.chanjar.weixin.common.util.http.RequestHttp;
99
import org.apache.http.HttpEntity;
1010
import org.apache.http.HttpHost;
1111
import org.apache.http.client.config.RequestConfig;
12-
import org.apache.http.client.methods.CloseableHttpResponse;
1312
import org.apache.http.client.methods.HttpPost;
1413
import org.apache.http.entity.mime.HttpMultipartMode;
1514
import org.apache.http.entity.mime.MultipartEntityBuilder;
@@ -41,15 +40,11 @@ public WxMediaUploadResult execute(String uri, File file, WxType wxType) throws
4140
.build();
4241
httpPost.setEntity(entity);
4342
}
44-
try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) {
45-
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
46-
WxError error = WxError.fromJson(responseContent, wxType);
47-
if (error.getErrorCode() != 0) {
48-
throw new WxErrorException(error);
49-
}
50-
return WxMediaUploadResult.fromJson(responseContent);
51-
} finally {
52-
httpPost.releaseConnection();
43+
String responseContent = requestHttp.getRequestHttpClient().execute(httpPost, Utf8ResponseHandler.INSTANCE);
44+
WxError error = WxError.fromJson(responseContent, wxType);
45+
if (error.getErrorCode() != 0) {
46+
throw new WxErrorException(error);
5347
}
48+
return WxMediaUploadResult.fromJson(responseContent);
5449
}
5550
}

weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/apache/ApacheMinishopMediaUploadRequestCustomizeExecutor.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import org.apache.http.HttpEntity;
1111
import org.apache.http.HttpHost;
1212
import org.apache.http.client.config.RequestConfig;
13-
import org.apache.http.client.methods.CloseableHttpResponse;
1413
import org.apache.http.client.methods.HttpPost;
1514
import org.apache.http.entity.mime.HttpMultipartMode;
1615
import org.apache.http.entity.mime.MultipartEntityBuilder;
@@ -58,16 +57,12 @@ public WxMinishopImageUploadCustomizeResult execute(String uri, File file, WxTyp
5857
.build();
5958
httpPost.setEntity(entity);
6059
}
61-
try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) {
62-
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
63-
WxError error = WxError.fromJson(responseContent, wxType);
64-
if (error.getErrorCode() != 0) {
65-
throw new WxErrorException(error);
66-
}
67-
log.info("responseContent: {}", responseContent);
68-
return WxMinishopImageUploadCustomizeResult.fromJson(responseContent);
69-
} finally {
70-
httpPost.releaseConnection();
60+
String responseContent = requestHttp.getRequestHttpClient().execute(httpPost, Utf8ResponseHandler.INSTANCE);
61+
WxError error = WxError.fromJson(responseContent, wxType);
62+
if (error.getErrorCode() != 0) {
63+
throw new WxErrorException(error);
7164
}
65+
log.info("responseContent: {}", responseContent);
66+
return WxMinishopImageUploadCustomizeResult.fromJson(responseContent);
7267
}
7368
}

weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/apache/ApacheMinishopMediaUploadRequestExecutor.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import org.apache.http.HttpEntity;
1111
import org.apache.http.HttpHost;
1212
import org.apache.http.client.config.RequestConfig;
13-
import org.apache.http.client.methods.CloseableHttpResponse;
1413
import org.apache.http.client.methods.HttpPost;
1514
import org.apache.http.entity.mime.HttpMultipartMode;
1615
import org.apache.http.entity.mime.MultipartEntityBuilder;
@@ -43,16 +42,12 @@ public WxMinishopImageUploadResult execute(String uri, File file, WxType wxType)
4342
.build();
4443
httpPost.setEntity(entity);
4544
}
46-
try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) {
47-
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
48-
WxError error = WxError.fromJson(responseContent, wxType);
49-
if (error.getErrorCode() != 0) {
50-
throw new WxErrorException(error);
51-
}
52-
log.info("responseContent: {}", responseContent);
53-
return WxMinishopImageUploadResult.fromJson(responseContent);
54-
} finally {
55-
httpPost.releaseConnection();
45+
String responseContent = requestHttp.getRequestHttpClient().execute(httpPost, Utf8ResponseHandler.INSTANCE);
46+
WxError error = WxError.fromJson(responseContent, wxType);
47+
if (error.getErrorCode() != 0) {
48+
throw new WxErrorException(error);
5649
}
50+
log.info("responseContent: {}", responseContent);
51+
return WxMinishopImageUploadResult.fromJson(responseContent);
5752
}
5853
}

0 commit comments

Comments
 (0)