mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-10-30 01:58:23 +08:00
🎨 修复CloseableHttpClient相关的误用代码
This commit is contained in:
@ -8,10 +8,10 @@ import me.chanjar.weixin.common.error.WxError;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.entity.mime.HttpMultipartMode;
|
||||
@ -52,19 +52,15 @@ public class CommonUploadRequestExecutorApacheImpl
|
||||
.build();
|
||||
httpPost.setEntity(entity);
|
||||
}
|
||||
try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) {
|
||||
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
|
||||
if (responseContent == null || responseContent.isEmpty()) {
|
||||
throw new WxErrorException(String.format("上传失败,服务器响应空 url:%s param:%s", uri, param));
|
||||
}
|
||||
WxError error = WxError.fromJson(responseContent, wxType);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
}
|
||||
return responseContent;
|
||||
} finally {
|
||||
httpPost.releaseConnection();
|
||||
String responseContent = requestHttp.getRequestHttpClient().execute(httpPost, Utf8ResponseHandler.INSTANCE);
|
||||
if (StringUtils.isEmpty(responseContent)) {
|
||||
throw new WxErrorException(String.format("上传失败,服务器响应空 url:%s param:%s", uri, param));
|
||||
}
|
||||
WxError error = WxError.fromJson(responseContent, wxType);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
}
|
||||
return responseContent;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -8,7 +8,6 @@ import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.entity.mime.HttpMultipartMode;
|
||||
import org.apache.http.entity.mime.MultipartEntityBuilder;
|
||||
@ -43,15 +42,11 @@ public class OcrDiscernApacheHttpRequestExecutor extends OcrDiscernRequestExecut
|
||||
.build();
|
||||
httpPost.setEntity(entity);
|
||||
}
|
||||
try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) {
|
||||
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
|
||||
WxError error = WxError.fromJson(responseContent, wxType);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
}
|
||||
return responseContent;
|
||||
} finally {
|
||||
httpPost.releaseConnection();
|
||||
String responseContent = requestHttp.getRequestHttpClient().execute(httpPost, Utf8ResponseHandler.INSTANCE);
|
||||
WxError error = WxError.fromJson(responseContent, wxType);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
}
|
||||
return responseContent;
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,9 @@
|
||||
package me.chanjar.weixin.common.util.http.apache;
|
||||
|
||||
import org.apache.http.impl.client.BasicResponseHandler;
|
||||
|
||||
public class ApacheBasicResponseHandler extends BasicResponseHandler {
|
||||
|
||||
public static final ApacheBasicResponseHandler INSTANCE = new ApacheBasicResponseHandler();
|
||||
|
||||
}
|
||||
@ -68,11 +68,7 @@ public class ApacheMediaDownloadRequestExecutor extends BaseMediaDownloadRequest
|
||||
baseName = String.valueOf(System.currentTimeMillis());
|
||||
}
|
||||
|
||||
return FileUtils.createTmpFile(inputStream, baseName, FilenameUtils.getExtension(fileName),
|
||||
super.tmpDirFile);
|
||||
|
||||
} finally {
|
||||
httpGet.releaseConnection();
|
||||
return FileUtils.createTmpFile(inputStream, baseName, FilenameUtils.getExtension(fileName), super.tmpDirFile);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -10,7 +10,6 @@ import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.entity.mime.HttpMultipartMode;
|
||||
@ -45,15 +44,11 @@ public class ApacheMediaInputStreamUploadRequestExecutor extends MediaInputStrea
|
||||
.build();
|
||||
httpPost.setEntity(entity);
|
||||
}
|
||||
try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) {
|
||||
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
|
||||
WxError error = WxError.fromJson(responseContent, wxType);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
}
|
||||
return WxMediaUploadResult.fromJson(responseContent);
|
||||
} finally {
|
||||
httpPost.releaseConnection();
|
||||
String responseContent = requestHttp.getRequestHttpClient().execute(httpPost, Utf8ResponseHandler.INSTANCE);
|
||||
WxError error = WxError.fromJson(responseContent, wxType);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
}
|
||||
return WxMediaUploadResult.fromJson(responseContent);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
package me.chanjar.weixin.common.util.http.apache;
|
||||
|
||||
import me.chanjar.weixin.common.enums.WxType;
|
||||
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
|
||||
import me.chanjar.weixin.common.enums.WxType;
|
||||
import me.chanjar.weixin.common.error.WxError;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor;
|
||||
@ -9,7 +9,6 @@ import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.entity.mime.HttpMultipartMode;
|
||||
import org.apache.http.entity.mime.MultipartEntityBuilder;
|
||||
@ -41,15 +40,11 @@ public class ApacheMediaUploadRequestExecutor extends MediaUploadRequestExecutor
|
||||
.build();
|
||||
httpPost.setEntity(entity);
|
||||
}
|
||||
try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) {
|
||||
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
|
||||
WxError error = WxError.fromJson(responseContent, wxType);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
}
|
||||
return WxMediaUploadResult.fromJson(responseContent);
|
||||
} finally {
|
||||
httpPost.releaseConnection();
|
||||
String responseContent = requestHttp.getRequestHttpClient().execute(httpPost, Utf8ResponseHandler.INSTANCE);
|
||||
WxError error = WxError.fromJson(responseContent, wxType);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
}
|
||||
return WxMediaUploadResult.fromJson(responseContent);
|
||||
}
|
||||
}
|
||||
|
||||
@ -10,7 +10,6 @@ import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.entity.mime.HttpMultipartMode;
|
||||
import org.apache.http.entity.mime.MultipartEntityBuilder;
|
||||
@ -58,16 +57,12 @@ public class ApacheMinishopMediaUploadRequestCustomizeExecutor extends MinishopU
|
||||
.build();
|
||||
httpPost.setEntity(entity);
|
||||
}
|
||||
try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) {
|
||||
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
|
||||
WxError error = WxError.fromJson(responseContent, wxType);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
}
|
||||
log.info("responseContent: {}", responseContent);
|
||||
return WxMinishopImageUploadCustomizeResult.fromJson(responseContent);
|
||||
} finally {
|
||||
httpPost.releaseConnection();
|
||||
String responseContent = requestHttp.getRequestHttpClient().execute(httpPost, Utf8ResponseHandler.INSTANCE);
|
||||
WxError error = WxError.fromJson(responseContent, wxType);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
}
|
||||
log.info("responseContent: {}", responseContent);
|
||||
return WxMinishopImageUploadCustomizeResult.fromJson(responseContent);
|
||||
}
|
||||
}
|
||||
|
||||
@ -10,7 +10,6 @@ import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.entity.mime.HttpMultipartMode;
|
||||
import org.apache.http.entity.mime.MultipartEntityBuilder;
|
||||
@ -43,16 +42,12 @@ public class ApacheMinishopMediaUploadRequestExecutor extends MinishopUploadRequ
|
||||
.build();
|
||||
httpPost.setEntity(entity);
|
||||
}
|
||||
try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) {
|
||||
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
|
||||
WxError error = WxError.fromJson(responseContent, wxType);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
}
|
||||
log.info("responseContent: {}", responseContent);
|
||||
return WxMinishopImageUploadResult.fromJson(responseContent);
|
||||
} finally {
|
||||
httpPost.releaseConnection();
|
||||
String responseContent = requestHttp.getRequestHttpClient().execute(httpPost, Utf8ResponseHandler.INSTANCE);
|
||||
WxError error = WxError.fromJson(responseContent, wxType);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
}
|
||||
log.info("responseContent: {}", responseContent);
|
||||
return WxMinishopImageUploadResult.fromJson(responseContent);
|
||||
}
|
||||
}
|
||||
|
||||
@ -6,7 +6,6 @@ import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
|
||||
@ -37,12 +36,8 @@ public class ApacheSimpleGetRequestExecutor extends SimpleGetRequestExecutor<Clo
|
||||
httpGet.setConfig(config);
|
||||
}
|
||||
|
||||
try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpGet)) {
|
||||
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
|
||||
return handleResponse(wxType, responseContent);
|
||||
} finally {
|
||||
httpGet.releaseConnection();
|
||||
}
|
||||
String responseContent = requestHttp.getRequestHttpClient().execute(httpGet, Utf8ResponseHandler.INSTANCE);
|
||||
return handleResponse(wxType, responseContent);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -7,7 +7,6 @@ import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
|
||||
import org.apache.http.Consts;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
@ -39,12 +38,8 @@ public class ApacheSimplePostRequestExecutor extends SimplePostRequestExecutor<C
|
||||
httpPost.setEntity(entity);
|
||||
}
|
||||
|
||||
try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) {
|
||||
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
|
||||
return this.handleResponse(wxType, responseContent);
|
||||
} finally {
|
||||
httpPost.releaseConnection();
|
||||
}
|
||||
String responseContent = requestHttp.getRequestHttpClient().execute(httpPost, Utf8ResponseHandler.INSTANCE);
|
||||
return this.handleResponse(wxType, responseContent);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -0,0 +1,17 @@
|
||||
package me.chanjar.weixin.common.util.http.apache;
|
||||
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.impl.client.AbstractResponseHandler;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class ByteArrayResponseHandler extends AbstractResponseHandler<byte[]> {
|
||||
|
||||
public static final ByteArrayResponseHandler INSTANCE = new ByteArrayResponseHandler();
|
||||
|
||||
@Override
|
||||
public byte[] handleEntity(HttpEntity entity) throws IOException {
|
||||
return EntityUtils.toByteArray(entity);
|
||||
}
|
||||
}
|
||||
@ -1,33 +1,23 @@
|
||||
package me.chanjar.weixin.common.util.http.apache;
|
||||
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.client.ResponseHandler;
|
||||
import org.apache.http.impl.client.AbstractResponseHandler;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.StatusLine;
|
||||
import org.apache.http.client.HttpResponseException;
|
||||
import org.apache.http.client.ResponseHandler;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
|
||||
/**
|
||||
* 输入流响应处理器.
|
||||
*
|
||||
* @author Daniel Qian
|
||||
* @author altusea
|
||||
*/
|
||||
public class InputStreamResponseHandler implements ResponseHandler<InputStream> {
|
||||
public class InputStreamResponseHandler extends AbstractResponseHandler<InputStream> {
|
||||
|
||||
public static final ResponseHandler<InputStream> INSTANCE = new InputStreamResponseHandler();
|
||||
private static final int STATUS_CODE_300 = 300;
|
||||
|
||||
@Override
|
||||
public InputStream handleResponse(final HttpResponse response) throws IOException {
|
||||
final StatusLine statusLine = response.getStatusLine();
|
||||
final HttpEntity entity = response.getEntity();
|
||||
if (statusLine.getStatusCode() >= STATUS_CODE_300) {
|
||||
EntityUtils.consume(entity);
|
||||
throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase());
|
||||
}
|
||||
return entity == null ? null : entity.getContent();
|
||||
public InputStream handleEntity(HttpEntity entity) throws IOException {
|
||||
return entity.getContent();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -1,33 +1,24 @@
|
||||
package me.chanjar.weixin.common.util.http.apache;
|
||||
|
||||
import org.apache.http.Consts;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.StatusLine;
|
||||
import org.apache.http.client.HttpResponseException;
|
||||
import org.apache.http.client.ResponseHandler;
|
||||
import org.apache.http.impl.client.AbstractResponseHandler;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
* copy from {@link org.apache.http.impl.client.BasicResponseHandler}
|
||||
* Utf8ResponseHandler
|
||||
*
|
||||
* @author Daniel Qian
|
||||
* @author altusea
|
||||
*/
|
||||
public class Utf8ResponseHandler implements ResponseHandler<String> {
|
||||
public class Utf8ResponseHandler extends AbstractResponseHandler<String> {
|
||||
|
||||
public static final ResponseHandler<String> INSTANCE = new Utf8ResponseHandler();
|
||||
|
||||
@Override
|
||||
public String handleResponse(final HttpResponse response) throws IOException {
|
||||
final StatusLine statusLine = response.getStatusLine();
|
||||
final HttpEntity entity = response.getEntity();
|
||||
if (statusLine.getStatusCode() >= 300) {
|
||||
EntityUtils.consume(entity);
|
||||
throw new HttpResponseException(statusLine.getStatusCode(), statusLine.toString());
|
||||
}
|
||||
return entity == null ? null : EntityUtils.toString(entity, Consts.UTF_8);
|
||||
public String handleEntity(HttpEntity entity) throws IOException {
|
||||
return EntityUtils.toString(entity, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -79,13 +79,13 @@ public class DefaultApacheHttpClientBuilderTest {
|
||||
HttpUriRequest request = new HttpGet("http://localhost:8080");
|
||||
HttpContext context = HttpClientContext.create();
|
||||
try (CloseableHttpResponse resp = client.execute(request, context)) {
|
||||
Assert.assertEquals("requestInterceptor1", context.getAttribute("interceptor_called"), "成功调用 requestInterceptor1 并向 content 中写入了数据");
|
||||
Assert.assertEquals(context.getAttribute("interceptor_called"), "requestInterceptor1", "成功调用 requestInterceptor1 并向 content 中写入了数据");
|
||||
|
||||
// 测试拦截器执行顺序
|
||||
Assert.assertEquals("requestInterceptor1", interceptorOrder.get(0));
|
||||
Assert.assertEquals("requestInterceptor2", interceptorOrder.get(1));
|
||||
Assert.assertEquals("responseInterceptor1", interceptorOrder.get(2));
|
||||
Assert.assertEquals("responseInterceptor2", interceptorOrder.get(3));
|
||||
Assert.assertEquals(interceptorOrder.get(0), "requestInterceptor1");
|
||||
Assert.assertEquals(interceptorOrder.get(1), "requestInterceptor2");
|
||||
Assert.assertEquals(interceptorOrder.get(2), "responseInterceptor1");
|
||||
Assert.assertEquals(interceptorOrder.get(3), "responseInterceptor2");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user