:art: 修复CloseableHttpClient相关的误用代码 · BING2U/WxJava@8bacc94 · GitHub
Skip to content

Commit 8bacc94

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

41 files changed

Lines changed: 240 additions & 443 deletions

File tree

Some content is hidden

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

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

Lines changed: 12 additions & 31 deletions

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
}
Lines changed: 9 additions & 0 deletions
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

0 commit comments

Comments
 (0)