🎨 修复CloseableHttpClient相关的误用代码

This commit is contained in:
altusea 2025-05-22 15:03:27 +08:00 committed by GitHub
parent 3e1a38a696
commit 8bacc9425e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
41 changed files with 240 additions and 443 deletions

View File

@ -1,28 +1,28 @@
package me.chanjar.weixin.channel.api.impl; package me.chanjar.weixin.channel.api.impl;
import static me.chanjar.weixin.channel.constant.WxChannelApiUrlConstants.GET_ACCESS_TOKEN_URL;
import static me.chanjar.weixin.channel.constant.WxChannelApiUrlConstants.GET_STABLE_ACCESS_TOKEN_URL;
import java.io.IOException;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.channel.bean.token.StableTokenParam; import me.chanjar.weixin.channel.bean.token.StableTokenParam;
import me.chanjar.weixin.channel.config.WxChannelConfig; import me.chanjar.weixin.channel.config.WxChannelConfig;
import me.chanjar.weixin.channel.util.JsonUtils; import me.chanjar.weixin.channel.util.JsonUtils;
import me.chanjar.weixin.common.util.http.HttpType; import me.chanjar.weixin.common.util.http.HttpType;
import me.chanjar.weixin.common.util.http.apache.ApacheBasicResponseHandler;
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder; import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder; import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.client.HttpClient; import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig; 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.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType; import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity; import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import java.io.IOException;
import static me.chanjar.weixin.channel.constant.WxChannelApiUrlConstants.GET_ACCESS_TOKEN_URL;
import static me.chanjar.weixin.channel.constant.WxChannelApiUrlConstants.GET_STABLE_ACCESS_TOKEN_URL;
/** /**
* @author <a href="https://github.com/lixize">Zeyes</a> * @author <a href="https://github.com/lixize">Zeyes</a>
*/ */
@ -76,27 +76,12 @@ public class WxChannelServiceHttpClientImpl extends BaseWxChannelServiceImpl<Htt
url = String.format(url, config.getAppid(), config.getSecret()); url = String.format(url, config.getAppid(), config.getSecret());
HttpGet httpGet = null; HttpGet httpGet = new HttpGet(url);
CloseableHttpResponse response = null; if (this.getRequestHttpProxy() != null) {
try { RequestConfig requestConfig = RequestConfig.custom().setProxy(this.getRequestHttpProxy()).build();
httpGet = new HttpGet(url); httpGet.setConfig(requestConfig);
if (this.getRequestHttpProxy() != null) {
RequestConfig requestConfig = RequestConfig.custom().setProxy(this.getRequestHttpProxy()).build();
httpGet.setConfig(requestConfig);
}
response = getRequestHttpClient().execute(httpGet);
return new BasicResponseHandler().handleResponse(response);
} finally {
if (httpGet != null) {
httpGet.releaseConnection();
}
if (response != null) {
try {
response.close();
} catch (IOException ignored) {
}
}
} }
return getRequestHttpClient().execute(httpGet, ApacheBasicResponseHandler.INSTANCE);
} }
/** /**
@ -125,10 +110,6 @@ public class WxChannelServiceHttpClientImpl extends BaseWxChannelServiceImpl<Htt
assert requestJson != null; assert requestJson != null;
httpPost.setEntity(new StringEntity(requestJson, ContentType.APPLICATION_JSON)); httpPost.setEntity(new StringEntity(requestJson, ContentType.APPLICATION_JSON));
try (CloseableHttpResponse response = getRequestHttpClient().execute(httpPost)) { return getRequestHttpClient().execute(httpPost, ApacheBasicResponseHandler.INSTANCE);
return new BasicResponseHandler().handleResponse(response);
} finally {
httpPost.releaseConnection();
}
} }
} }

View File

@ -1,9 +1,5 @@
package me.chanjar.weixin.channel.executor; package me.chanjar.weixin.channel.executor;
import java.io.File;
import java.io.IOException;
import me.chanjar.weixin.common.enums.WxType; import me.chanjar.weixin.common.enums.WxType;
import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestExecutor; import me.chanjar.weixin.common.util.http.RequestExecutor;
@ -13,12 +9,14 @@ import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig; 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.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import java.io.File;
import java.io.IOException;
/** /**
* 视频号小店 图片上传接口 请求的参数是File, 返回的结果是String * 视频号小店 图片上传接口 请求的参数是File, 返回的结果是String
* *
@ -47,11 +45,7 @@ public class ChannelFileUploadRequestExecutor implements RequestExecutor<String,
.build(); .build();
httpPost.setEntity(entity); httpPost.setEntity(entity);
} }
try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) { return requestHttp.getRequestHttpClient().execute(httpPost, Utf8ResponseHandler.INSTANCE);
return Utf8ResponseHandler.INSTANCE.handleResponse(response);
} finally {
httpPost.releaseConnection();
}
} }
@Override @Override
@ -60,6 +54,7 @@ public class ChannelFileUploadRequestExecutor implements RequestExecutor<String,
handler.handle(this.execute(uri, data, wxType)); handler.handle(this.execute(uri, data, wxType));
} }
@SuppressWarnings("unchecked")
public static RequestExecutor<String, File> create(RequestHttp<?, ?> requestHttp) throws WxErrorException { public static RequestExecutor<String, File> create(RequestHttp<?, ?> requestHttp) throws WxErrorException {
switch (requestHttp.getRequestType()) { switch (requestHttp.getRequestType()) {
case APACHE_HTTP: case APACHE_HTTP:

View File

@ -1,14 +1,5 @@
package me.chanjar.weixin.channel.executor; package me.chanjar.weixin.channel.executor;
import static org.apache.commons.io.FileUtils.openOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.channel.bean.image.ChannelImageResponse; import me.chanjar.weixin.channel.bean.image.ChannelImageResponse;
import me.chanjar.weixin.channel.util.JsonUtils; import me.chanjar.weixin.channel.util.JsonUtils;
@ -30,6 +21,16 @@ import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.ContentType; import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static org.apache.commons.io.FileUtils.openOutputStream;
/** /**
* 下载媒体文件请求执行器 * 下载媒体文件请求执行器
* *
@ -90,10 +91,7 @@ public class ChannelMediaDownloadRequestExecutor implements RequestExecutor<Chan
extension = "unknown"; extension = "unknown";
} }
File file = createTmpFile(inputStream, baseName, extension, tmpDirFile); File file = createTmpFile(inputStream, baseName, extension, tmpDirFile);
ChannelImageResponse result = new ChannelImageResponse(file, contentType); return new ChannelImageResponse(file, contentType);
return result;
} finally {
httpGet.releaseConnection();
} }
} }

View File

@ -8,10 +8,10 @@ import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestHttp; import me.chanjar.weixin.common.util.http.RequestHttp;
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler; import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig; 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.client.methods.HttpPost;
import org.apache.http.entity.ContentType; import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.HttpMultipartMode;
@ -52,19 +52,15 @@ public class CommonUploadRequestExecutorApacheImpl
.build(); .build();
httpPost.setEntity(entity); httpPost.setEntity(entity);
} }
try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) { String responseContent = requestHttp.getRequestHttpClient().execute(httpPost, Utf8ResponseHandler.INSTANCE);
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); if (StringUtils.isEmpty(responseContent)) {
if (responseContent == null || responseContent.isEmpty()) { throw new WxErrorException(String.format("上传失败,服务器响应空 url:%s param:%s", uri, param));
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();
} }
WxError error = WxError.fromJson(responseContent, wxType);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
}
return responseContent;
} }
/** /**

View File

@ -8,7 +8,6 @@ import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig; 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.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.entity.mime.MultipartEntityBuilder;
@ -43,15 +42,11 @@ public class OcrDiscernApacheHttpRequestExecutor extends OcrDiscernRequestExecut
.build(); .build();
httpPost.setEntity(entity); httpPost.setEntity(entity);
} }
try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) { String responseContent = requestHttp.getRequestHttpClient().execute(httpPost, Utf8ResponseHandler.INSTANCE);
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); WxError error = WxError.fromJson(responseContent, wxType);
WxError error = WxError.fromJson(responseContent, wxType); if (error.getErrorCode() != 0) {
if (error.getErrorCode() != 0) { throw new WxErrorException(error);
throw new WxErrorException(error);
}
return responseContent;
} finally {
httpPost.releaseConnection();
} }
return responseContent;
} }
} }

View File

@ -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();
}

View File

@ -68,11 +68,7 @@ public class ApacheMediaDownloadRequestExecutor extends BaseMediaDownloadRequest
baseName = String.valueOf(System.currentTimeMillis()); baseName = String.valueOf(System.currentTimeMillis());
} }
return FileUtils.createTmpFile(inputStream, baseName, FilenameUtils.getExtension(fileName), return FileUtils.createTmpFile(inputStream, baseName, FilenameUtils.getExtension(fileName), super.tmpDirFile);
super.tmpDirFile);
} finally {
httpGet.releaseConnection();
} }
} }

View File

@ -10,7 +10,6 @@ import me.chanjar.weixin.common.util.http.RequestHttp;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig; 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.client.methods.HttpPost;
import org.apache.http.entity.ContentType; import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.HttpMultipartMode;
@ -45,15 +44,11 @@ public class ApacheMediaInputStreamUploadRequestExecutor extends MediaInputStrea
.build(); .build();
httpPost.setEntity(entity); httpPost.setEntity(entity);
} }
try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) { String responseContent = requestHttp.getRequestHttpClient().execute(httpPost, Utf8ResponseHandler.INSTANCE);
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); WxError error = WxError.fromJson(responseContent, wxType);
WxError error = WxError.fromJson(responseContent, wxType); if (error.getErrorCode() != 0) {
if (error.getErrorCode() != 0) { throw new WxErrorException(error);
throw new WxErrorException(error);
}
return WxMediaUploadResult.fromJson(responseContent);
} finally {
httpPost.releaseConnection();
} }
return WxMediaUploadResult.fromJson(responseContent);
} }
} }

View File

@ -1,7 +1,7 @@
package me.chanjar.weixin.common.util.http.apache; 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.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.common.enums.WxType;
import me.chanjar.weixin.common.error.WxError; import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor; 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.HttpEntity;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig; 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.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.entity.mime.MultipartEntityBuilder;
@ -41,15 +40,11 @@ public class ApacheMediaUploadRequestExecutor extends MediaUploadRequestExecutor
.build(); .build();
httpPost.setEntity(entity); httpPost.setEntity(entity);
} }
try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) { String responseContent = requestHttp.getRequestHttpClient().execute(httpPost, Utf8ResponseHandler.INSTANCE);
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); WxError error = WxError.fromJson(responseContent, wxType);
WxError error = WxError.fromJson(responseContent, wxType); if (error.getErrorCode() != 0) {
if (error.getErrorCode() != 0) { throw new WxErrorException(error);
throw new WxErrorException(error);
}
return WxMediaUploadResult.fromJson(responseContent);
} finally {
httpPost.releaseConnection();
} }
return WxMediaUploadResult.fromJson(responseContent);
} }
} }

View File

@ -10,7 +10,6 @@ import me.chanjar.weixin.common.util.http.RequestHttp;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig; 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.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.entity.mime.MultipartEntityBuilder;
@ -58,16 +57,12 @@ public class ApacheMinishopMediaUploadRequestCustomizeExecutor extends MinishopU
.build(); .build();
httpPost.setEntity(entity); httpPost.setEntity(entity);
} }
try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) { String responseContent = requestHttp.getRequestHttpClient().execute(httpPost, Utf8ResponseHandler.INSTANCE);
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); WxError error = WxError.fromJson(responseContent, wxType);
WxError error = WxError.fromJson(responseContent, wxType); if (error.getErrorCode() != 0) {
if (error.getErrorCode() != 0) { throw new WxErrorException(error);
throw new WxErrorException(error);
}
log.info("responseContent: {}", responseContent);
return WxMinishopImageUploadCustomizeResult.fromJson(responseContent);
} finally {
httpPost.releaseConnection();
} }
log.info("responseContent: {}", responseContent);
return WxMinishopImageUploadCustomizeResult.fromJson(responseContent);
} }
} }

View File

@ -10,7 +10,6 @@ import me.chanjar.weixin.common.util.http.RequestHttp;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig; 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.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.entity.mime.MultipartEntityBuilder;
@ -43,16 +42,12 @@ public class ApacheMinishopMediaUploadRequestExecutor extends MinishopUploadRequ
.build(); .build();
httpPost.setEntity(entity); httpPost.setEntity(entity);
} }
try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) { String responseContent = requestHttp.getRequestHttpClient().execute(httpPost, Utf8ResponseHandler.INSTANCE);
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); WxError error = WxError.fromJson(responseContent, wxType);
WxError error = WxError.fromJson(responseContent, wxType); if (error.getErrorCode() != 0) {
if (error.getErrorCode() != 0) { throw new WxErrorException(error);
throw new WxErrorException(error);
}
log.info("responseContent: {}", responseContent);
return WxMinishopImageUploadResult.fromJson(responseContent);
} finally {
httpPost.releaseConnection();
} }
log.info("responseContent: {}", responseContent);
return WxMinishopImageUploadResult.fromJson(responseContent);
} }
} }

View File

@ -6,7 +6,6 @@ import me.chanjar.weixin.common.util.http.RequestHttp;
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor; import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig; 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.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
@ -37,12 +36,8 @@ public class ApacheSimpleGetRequestExecutor extends SimpleGetRequestExecutor<Clo
httpGet.setConfig(config); httpGet.setConfig(config);
} }
try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpGet)) { String responseContent = requestHttp.getRequestHttpClient().execute(httpGet, Utf8ResponseHandler.INSTANCE);
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); return handleResponse(wxType, responseContent);
return handleResponse(wxType, responseContent);
} finally {
httpGet.releaseConnection();
}
} }
} }

View File

@ -7,7 +7,6 @@ import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
import org.apache.http.Consts; import org.apache.http.Consts;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig; 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.client.methods.HttpPost;
import org.apache.http.entity.StringEntity; import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
@ -39,12 +38,8 @@ public class ApacheSimplePostRequestExecutor extends SimplePostRequestExecutor<C
httpPost.setEntity(entity); httpPost.setEntity(entity);
} }
try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) { String responseContent = requestHttp.getRequestHttpClient().execute(httpPost, Utf8ResponseHandler.INSTANCE);
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); return this.handleResponse(wxType, responseContent);
return this.handleResponse(wxType, responseContent);
} finally {
httpPost.releaseConnection();
}
} }
} }

View File

@ -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);
}
}

View File

@ -1,33 +1,23 @@
package me.chanjar.weixin.common.util.http.apache; 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.IOException;
import java.io.InputStream; 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(); public static final ResponseHandler<InputStream> INSTANCE = new InputStreamResponseHandler();
private static final int STATUS_CODE_300 = 300;
@Override @Override
public InputStream handleResponse(final HttpResponse response) throws IOException { public InputStream handleEntity(HttpEntity entity) throws IOException {
final StatusLine statusLine = response.getStatusLine(); return entity.getContent();
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();
} }
} }

View File

@ -1,33 +1,24 @@
package me.chanjar.weixin.common.util.http.apache; package me.chanjar.weixin.common.util.http.apache;
import org.apache.http.Consts;
import org.apache.http.HttpEntity; 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.client.ResponseHandler;
import org.apache.http.impl.client.AbstractResponseHandler;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
import java.io.IOException; 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(); public static final ResponseHandler<String> INSTANCE = new Utf8ResponseHandler();
@Override @Override
public String handleResponse(final HttpResponse response) throws IOException { public String handleEntity(HttpEntity entity) throws IOException {
final StatusLine statusLine = response.getStatusLine(); return EntityUtils.toString(entity, StandardCharsets.UTF_8);
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);
} }
} }

View File

@ -79,13 +79,13 @@ public class DefaultApacheHttpClientBuilderTest {
HttpUriRequest request = new HttpGet("http://localhost:8080"); HttpUriRequest request = new HttpGet("http://localhost:8080");
HttpContext context = HttpClientContext.create(); HttpContext context = HttpClientContext.create();
try (CloseableHttpResponse resp = client.execute(request, context)) { 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(interceptorOrder.get(0), "requestInterceptor1");
Assert.assertEquals("requestInterceptor2", interceptorOrder.get(1)); Assert.assertEquals(interceptorOrder.get(1), "requestInterceptor2");
Assert.assertEquals("responseInterceptor1", interceptorOrder.get(2)); Assert.assertEquals(interceptorOrder.get(2), "responseInterceptor1");
Assert.assertEquals("responseInterceptor2", interceptorOrder.get(3)); Assert.assertEquals(interceptorOrder.get(3), "responseInterceptor2");
} }
} }
} }

View File

@ -1,21 +1,19 @@
package me.chanjar.weixin.cp.api.impl; package me.chanjar.weixin.cp.api.impl;
import me.chanjar.weixin.common.bean.WxAccessToken; import me.chanjar.weixin.common.bean.WxAccessToken;
import me.chanjar.weixin.common.enums.WxType; import me.chanjar.weixin.common.enums.WxType;
import me.chanjar.weixin.common.error.WxError; import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.error.WxRuntimeException; import me.chanjar.weixin.common.error.WxRuntimeException;
import me.chanjar.weixin.common.util.http.HttpType; import me.chanjar.weixin.common.util.http.HttpType;
import me.chanjar.weixin.common.util.http.apache.ApacheBasicResponseHandler;
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder; import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder; import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder;
import me.chanjar.weixin.cp.config.WxCpConfigStorage; import me.chanjar.weixin.cp.config.WxCpConfigStorage;
import me.chanjar.weixin.cp.constant.WxCpApiPathConsts; import me.chanjar.weixin.cp.constant.WxCpApiPathConsts;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig; 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.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import java.io.IOException; import java.io.IOException;
@ -61,13 +59,7 @@ public class WxCpServiceApacheHttpClientImpl extends BaseWxCpServiceImpl<Closeab
.setProxy(this.httpProxy).build(); .setProxy(this.httpProxy).build();
httpGet.setConfig(config); httpGet.setConfig(config);
} }
String resultContent; String resultContent = getRequestHttpClient().execute(httpGet, ApacheBasicResponseHandler.INSTANCE);
try (CloseableHttpClient httpClient = getRequestHttpClient();
CloseableHttpResponse response = httpClient.execute(httpGet)) {
resultContent = new BasicResponseHandler().handleResponse(response);
} finally {
httpGet.releaseConnection();
}
WxError error = WxError.fromJson(resultContent, WxType.CP); WxError error = WxError.fromJson(resultContent, WxType.CP);
if (error.getErrorCode() != 0) { if (error.getErrorCode() != 0) {
throw new WxErrorException(error); throw new WxErrorException(error);

View File

@ -6,14 +6,12 @@ import me.chanjar.weixin.common.enums.WxType;
import me.chanjar.weixin.common.error.WxError; import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.error.WxRuntimeException; import me.chanjar.weixin.common.error.WxRuntimeException;
import me.chanjar.weixin.common.util.http.apache.ApacheBasicResponseHandler;
import me.chanjar.weixin.common.util.json.GsonParser; import me.chanjar.weixin.common.util.json.GsonParser;
import me.chanjar.weixin.cp.config.WxCpConfigStorage; import me.chanjar.weixin.cp.config.WxCpConfigStorage;
import me.chanjar.weixin.cp.constant.WxCpApiPathConsts; import me.chanjar.weixin.cp.constant.WxCpApiPathConsts;
import org.apache.http.client.config.RequestConfig; 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.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient;
import java.io.IOException; import java.io.IOException;
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.Lock;
@ -55,13 +53,7 @@ public class WxCpServiceImpl extends WxCpServiceApacheHttpClientImpl {
RequestConfig config = RequestConfig.custom().setProxy(getRequestHttpProxy()).build(); RequestConfig config = RequestConfig.custom().setProxy(getRequestHttpProxy()).build();
httpGet.setConfig(config); httpGet.setConfig(config);
} }
String resultContent; String resultContent = getRequestHttpClient().execute(httpGet, ApacheBasicResponseHandler.INSTANCE);
try (CloseableHttpClient httpClient = getRequestHttpClient();
CloseableHttpResponse response = httpClient.execute(httpGet)) {
resultContent = new BasicResponseHandler().handleResponse(response);
} finally {
httpGet.releaseConnection();
}
WxError error = WxError.fromJson(resultContent, WxType.CP); WxError error = WxError.fromJson(resultContent, WxType.CP);
if (error.getErrorCode() != 0) { if (error.getErrorCode() != 0) {
throw new WxErrorException(error); throw new WxErrorException(error);

View File

@ -1,12 +1,12 @@
package me.chanjar.weixin.cp.tp.service.impl; package me.chanjar.weixin.cp.tp.service.impl;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import me.chanjar.weixin.common.enums.WxType; import me.chanjar.weixin.common.enums.WxType;
import me.chanjar.weixin.common.error.WxError; import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.error.WxRuntimeException; import me.chanjar.weixin.common.error.WxRuntimeException;
import me.chanjar.weixin.common.util.http.HttpType; import me.chanjar.weixin.common.util.http.HttpType;
import me.chanjar.weixin.common.util.http.apache.ApacheBasicResponseHandler;
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder; import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder; import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder;
import me.chanjar.weixin.common.util.json.GsonParser; import me.chanjar.weixin.common.util.json.GsonParser;
@ -15,10 +15,8 @@ import me.chanjar.weixin.cp.constant.WxCpApiPathConsts;
import org.apache.http.Consts; import org.apache.http.Consts;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig; 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.client.methods.HttpPost;
import org.apache.http.entity.StringEntity; import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import java.io.IOException; import java.io.IOException;
@ -68,20 +66,14 @@ public class WxCpTpServiceApacheHttpClientImpl extends BaseWxCpTpServiceImpl<Clo
StringEntity entity = new StringEntity(jsonObject.toString(), Consts.UTF_8); StringEntity entity = new StringEntity(jsonObject.toString(), Consts.UTF_8);
httpPost.setEntity(entity); httpPost.setEntity(entity);
String resultContent; String resultContent = getRequestHttpClient().execute(httpPost, ApacheBasicResponseHandler.INSTANCE);
try (CloseableHttpClient httpclient = getRequestHttpClient();
CloseableHttpResponse response = httpclient.execute(httpPost)) {
resultContent = new BasicResponseHandler().handleResponse(response);
} finally {
httpPost.releaseConnection();
}
WxError error = WxError.fromJson(resultContent, WxType.CP); WxError error = WxError.fromJson(resultContent, WxType.CP);
if (error.getErrorCode() != 0) { if (error.getErrorCode() != 0) {
throw new WxErrorException(error); throw new WxErrorException(error);
} }
jsonObject = GsonParser.parse(resultContent); jsonObject = GsonParser.parse(resultContent);
String suiteAccussToken = jsonObject.get("suite_access_token").getAsString(); String suiteAccussToken = jsonObject.get("suite_access_token").getAsString();
Integer expiresIn = jsonObject.get("expires_in").getAsInt(); int expiresIn = jsonObject.get("expires_in").getAsInt();
this.configStorage.updateSuiteAccessToken(suiteAccussToken, expiresIn); this.configStorage.updateSuiteAccessToken(suiteAccussToken, expiresIn);
} catch (IOException e) { } catch (IOException e) {
throw new WxRuntimeException(e); throw new WxRuntimeException(e);

View File

@ -5,18 +5,16 @@ import cn.binarywang.wx.miniapp.bean.WxMaStableAccessTokenRequest;
import cn.binarywang.wx.miniapp.config.WxMaConfig; import cn.binarywang.wx.miniapp.config.WxMaConfig;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.util.http.HttpType; import me.chanjar.weixin.common.util.http.HttpType;
import me.chanjar.weixin.common.util.http.apache.ApacheBasicResponseHandler;
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder; import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder; import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig; 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.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType; import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity; import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import java.io.IOException; import java.io.IOException;
@ -75,22 +73,12 @@ public class WxMaServiceHttpClientImpl extends BaseWxMaServiceImpl {
url = String.format(url, this.getWxMaConfig().getAppid(), this.getWxMaConfig().getSecret()); url = String.format(url, this.getWxMaConfig().getAppid(), this.getWxMaConfig().getSecret());
HttpGet httpGet = null; HttpGet httpGet = new HttpGet(url);
CloseableHttpResponse response = null; if (this.getRequestHttpProxy() != null) {
try { RequestConfig config = RequestConfig.custom().setProxy(this.getRequestHttpProxy()).build();
httpGet = new HttpGet(url); httpGet.setConfig(config);
if (this.getRequestHttpProxy() != null) {
RequestConfig config = RequestConfig.custom().setProxy(this.getRequestHttpProxy()).build();
httpGet.setConfig(config);
}
response = getRequestHttpClient().execute(httpGet);
return new BasicResponseHandler().handleResponse(response);
} finally {
if (httpGet != null) {
httpGet.releaseConnection();
}
IOUtils.closeQuietly(response);
} }
return getRequestHttpClient().execute(httpGet, ApacheBasicResponseHandler.INSTANCE);
} }
@Override @Override
@ -100,28 +88,18 @@ public class WxMaServiceHttpClientImpl extends BaseWxMaServiceImpl {
GET_STABLE_ACCESS_TOKEN.replace("https://api.weixin.qq.com", this.getWxMaConfig().getApiHostUrl()) : GET_STABLE_ACCESS_TOKEN.replace("https://api.weixin.qq.com", this.getWxMaConfig().getApiHostUrl()) :
GET_STABLE_ACCESS_TOKEN; GET_STABLE_ACCESS_TOKEN;
HttpPost httpPost = null; HttpPost httpPost = new HttpPost(url);
CloseableHttpResponse response = null; if (this.getRequestHttpProxy() != null) {
try { RequestConfig config = RequestConfig.custom().setProxy(this.getRequestHttpProxy()).build();
httpPost = new HttpPost(url); httpPost.setConfig(config);
if (this.getRequestHttpProxy() != null) {
RequestConfig config = RequestConfig.custom().setProxy(this.getRequestHttpProxy()).build();
httpPost.setConfig(config);
}
WxMaStableAccessTokenRequest wxMaAccessTokenRequest = new WxMaStableAccessTokenRequest();
wxMaAccessTokenRequest.setAppid(this.getWxMaConfig().getAppid());
wxMaAccessTokenRequest.setSecret(this.getWxMaConfig().getSecret());
wxMaAccessTokenRequest.setGrantType("client_credential");
wxMaAccessTokenRequest.setForceRefresh(forceRefresh);
httpPost.setEntity(new StringEntity(wxMaAccessTokenRequest.toJson(), ContentType.APPLICATION_JSON));
response = getRequestHttpClient().execute(httpPost);
return new BasicResponseHandler().handleResponse(response);
} finally {
if (httpPost != null) {
httpPost.releaseConnection();
}
IOUtils.closeQuietly(response);
} }
WxMaStableAccessTokenRequest wxMaAccessTokenRequest = new WxMaStableAccessTokenRequest();
wxMaAccessTokenRequest.setAppid(this.getWxMaConfig().getAppid());
wxMaAccessTokenRequest.setSecret(this.getWxMaConfig().getSecret());
wxMaAccessTokenRequest.setGrantType("client_credential");
wxMaAccessTokenRequest.setForceRefresh(forceRefresh);
httpPost.setEntity(new StringEntity(wxMaAccessTokenRequest.toJson(), ContentType.APPLICATION_JSON));
return getRequestHttpClient().execute(httpPost, ApacheBasicResponseHandler.INSTANCE);
} }
} }

View File

@ -1,9 +1,6 @@
package cn.binarywang.wx.miniapp.executor; package cn.binarywang.wx.miniapp.executor;
import cn.binarywang.wx.miniapp.bean.WxMaApiResponse; import cn.binarywang.wx.miniapp.bean.WxMaApiResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import me.chanjar.weixin.common.enums.WxType; import me.chanjar.weixin.common.enums.WxType;
import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestHttp; import me.chanjar.weixin.common.util.http.RequestHttp;
@ -19,6 +16,10 @@ import org.apache.http.impl.client.CloseableHttpClient;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class ApacheApiSignaturePostRequestExecutor public class ApacheApiSignaturePostRequestExecutor
extends ApiSignaturePostRequestExecutor<CloseableHttpClient, HttpHost> { extends ApiSignaturePostRequestExecutor<CloseableHttpClient, HttpHost> {
private static final Logger logger = private static final Logger logger =
@ -64,8 +65,6 @@ public class ApacheApiSignaturePostRequestExecutor
} }
} }
return this.handleResponse(wxType, responseContent, respHeaders); return this.handleResponse(wxType, responseContent, respHeaders);
} finally {
httpPost.releaseConnection();
} }
} }
} }

View File

@ -63,8 +63,6 @@ public class ApacheQrcodeBytesRequestExecutor extends QrcodeBytesRequestExecutor
} }
return IOUtils.toByteArray(inputStream); return IOUtils.toByteArray(inputStream);
} finally {
httpPost.releaseConnection();
} }
} }
} }

View File

@ -71,8 +71,6 @@ public class ApacheQrcodeFileRequestExecutor extends QrcodeRequestExecutor<Close
return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg"); return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg");
} }
return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg", Paths.get(filePath).toFile()); return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg", Paths.get(filePath).toFile());
} finally {
httpPost.releaseConnection();
} }
} }
} }

View File

@ -9,7 +9,6 @@ import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig; 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.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.entity.mime.MultipartEntityBuilder;
@ -24,34 +23,30 @@ import java.io.IOException;
*/ */
public class ApacheUploadAuthMaterialRequestExecutor extends UploadAuthMaterialRequestExecutor<CloseableHttpClient, HttpHost> { public class ApacheUploadAuthMaterialRequestExecutor extends UploadAuthMaterialRequestExecutor<CloseableHttpClient, HttpHost> {
public ApacheUploadAuthMaterialRequestExecutor(RequestHttp<CloseableHttpClient, HttpHost> requestHttp) { public ApacheUploadAuthMaterialRequestExecutor(RequestHttp<CloseableHttpClient, HttpHost> requestHttp) {
super(requestHttp); super(requestHttp);
} }
@Override @Override
public WxMaUploadAuthMaterialResult execute(String uri, File file, WxType wxType) throws WxErrorException, IOException { public WxMaUploadAuthMaterialResult execute(String uri, File file, WxType wxType) throws WxErrorException, IOException {
HttpPost httpPost = new HttpPost(uri); HttpPost httpPost = new HttpPost(uri);
if (requestHttp.getRequestHttpProxy() != null) { if (requestHttp.getRequestHttpProxy() != null) {
RequestConfig config = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build(); RequestConfig config = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build();
httpPost.setConfig(config); httpPost.setConfig(config);
}
if (file != null) {
HttpEntity entity = MultipartEntityBuilder
.create()
.addBinaryBody("media", file)
.setMode(HttpMultipartMode.RFC6532)
.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 WxMaUploadAuthMaterialResult.fromJson(responseContent);
} finally {
httpPost.releaseConnection();
}
} }
if (file != null) {
HttpEntity entity = MultipartEntityBuilder
.create()
.addBinaryBody("media", file)
.setMode(HttpMultipartMode.RFC6532)
.build();
httpPost.setEntity(entity);
}
String responseContent = requestHttp.getRequestHttpClient().execute(httpPost, Utf8ResponseHandler.INSTANCE);
WxError error = WxError.fromJson(responseContent, wxType);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
}
return WxMaUploadAuthMaterialResult.fromJson(responseContent);
}
} }

View File

@ -8,7 +8,6 @@ import me.chanjar.weixin.common.util.http.RequestHttp;
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler; import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig; 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.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.entity.mime.MultipartEntityBuilder;
@ -24,7 +23,6 @@ public class ApacheVodSingleUploadRequestExecutor extends VodSingleUploadRequest
public ApacheVodSingleUploadRequestExecutor(RequestHttp<CloseableHttpClient, HttpHost> requestHttp, String mediaName, String mediaType, String coverType, File coverData, String sourceContext) { public ApacheVodSingleUploadRequestExecutor(RequestHttp<CloseableHttpClient, HttpHost> requestHttp, String mediaName, String mediaType, String coverType, File coverData, String sourceContext) {
super(requestHttp, mediaName, mediaType, coverType, coverData, sourceContext); super(requestHttp, mediaName, mediaType, coverType, coverData, sourceContext);
} }
@Override @Override
@ -54,15 +52,11 @@ public class ApacheVodSingleUploadRequestExecutor extends VodSingleUploadRequest
httpPost.setEntity(entityBuilder.build()); httpPost.setEntity(entityBuilder.build());
} }
try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) { String responseContent = requestHttp.getRequestHttpClient().execute(httpPost, Utf8ResponseHandler.INSTANCE);
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); WxError error = WxError.fromJson(responseContent, wxType);
WxError error = WxError.fromJson(responseContent, wxType); if (error.getErrorCode() != 0) {
if (error.getErrorCode() != 0) { throw new WxErrorException(error);
throw new WxErrorException(error);
}
return WxMaVodSingleFileUploadResult.fromJson(responseContent);
} finally {
httpPost.releaseConnection();
} }
return WxMaVodSingleFileUploadResult.fromJson(responseContent);
} }
} }

View File

@ -8,7 +8,6 @@ import me.chanjar.weixin.common.util.http.RequestHttp;
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler; import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig; 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.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.entity.mime.MultipartEntityBuilder;
@ -45,15 +44,12 @@ public class ApacheVodUploadPartRequestExecutor extends VodUploadPartRequestExec
httpPost.setEntity(entityBuilder.build()); httpPost.setEntity(entityBuilder.build());
} }
try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) {
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); String responseContent = requestHttp.getRequestHttpClient().execute(httpPost, Utf8ResponseHandler.INSTANCE);
WxError error = WxError.fromJson(responseContent, wxType); WxError error = WxError.fromJson(responseContent, wxType);
if (error.getErrorCode() != 0) { if (error.getErrorCode() != 0) {
throw new WxErrorException(error); throw new WxErrorException(error);
}
return WxMaVodUploadPartResult.fromJson(responseContent);
} finally {
httpPost.releaseConnection();
} }
return WxMaVodUploadPartResult.fromJson(responseContent);
} }
} }

View File

@ -1,18 +1,17 @@
package me.chanjar.weixin.mp.api.impl; package me.chanjar.weixin.mp.api.impl;
import me.chanjar.weixin.common.util.http.HttpType; import me.chanjar.weixin.common.util.http.HttpType;
import me.chanjar.weixin.common.util.http.apache.ApacheBasicResponseHandler;
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder; import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder; import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder;
import me.chanjar.weixin.mp.bean.WxMpStableAccessTokenRequest; import me.chanjar.weixin.mp.bean.WxMpStableAccessTokenRequest;
import me.chanjar.weixin.mp.config.WxMpConfigStorage; import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig; 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.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType; import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity; import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import java.io.IOException; import java.io.IOException;
@ -68,61 +67,31 @@ public class WxMpServiceHttpClientImpl extends BaseWxMpServiceImpl<CloseableHttp
protected String doGetAccessTokenRequest() throws IOException { protected String doGetAccessTokenRequest() throws IOException {
String url = String.format(GET_ACCESS_TOKEN_URL.getUrl(getWxMpConfigStorage()), getWxMpConfigStorage().getAppId(), getWxMpConfigStorage().getSecret()); String url = String.format(GET_ACCESS_TOKEN_URL.getUrl(getWxMpConfigStorage()), getWxMpConfigStorage().getAppId(), getWxMpConfigStorage().getSecret());
HttpGet httpGet = null; HttpGet httpGet = new HttpGet(url);
CloseableHttpResponse response = null; if (this.getRequestHttpProxy() != null) {
try { RequestConfig config = RequestConfig.custom().setProxy(this.getRequestHttpProxy()).build();
httpGet = new HttpGet(url); httpGet.setConfig(config);
if (this.getRequestHttpProxy() != null) {
RequestConfig config = RequestConfig.custom().setProxy(this.getRequestHttpProxy()).build();
httpGet.setConfig(config);
}
response = getRequestHttpClient().execute(httpGet);
return new BasicResponseHandler().handleResponse(response);
} finally {
if (httpGet != null) {
httpGet.releaseConnection();
}
if (response != null) {
try {
response.close();
} catch (IOException e) {
}
}
} }
return getRequestHttpClient().execute(httpGet, ApacheBasicResponseHandler.INSTANCE);
} }
@Override @Override
protected String doGetStableAccessTokenRequest(boolean forceRefresh) throws IOException { protected String doGetStableAccessTokenRequest(boolean forceRefresh) throws IOException {
String url = GET_STABLE_ACCESS_TOKEN_URL.getUrl(getWxMpConfigStorage()); String url = GET_STABLE_ACCESS_TOKEN_URL.getUrl(getWxMpConfigStorage());
HttpPost httpPost = null; HttpPost httpPost = new HttpPost(url);
CloseableHttpResponse response = null; if (this.getRequestHttpProxy() != null) {
try { RequestConfig config = RequestConfig.custom().setProxy(this.getRequestHttpProxy()).build();
httpPost = new HttpPost(url); httpPost.setConfig(config);
if (this.getRequestHttpProxy() != null) {
RequestConfig config = RequestConfig.custom().setProxy(this.getRequestHttpProxy()).build();
httpPost.setConfig(config);
}
WxMpStableAccessTokenRequest wxMaAccessTokenRequest = new WxMpStableAccessTokenRequest();
wxMaAccessTokenRequest.setAppid(this.getWxMpConfigStorage().getAppId());
wxMaAccessTokenRequest.setSecret(this.getWxMpConfigStorage().getSecret());
wxMaAccessTokenRequest.setGrantType("client_credential");
wxMaAccessTokenRequest.setForceRefresh(forceRefresh);
httpPost.setEntity(new StringEntity(wxMaAccessTokenRequest.toJson(), ContentType.APPLICATION_JSON));
response = getRequestHttpClient().execute(httpPost);
return new BasicResponseHandler().handleResponse(response);
} finally {
if (httpPost != null) {
httpPost.releaseConnection();
}
if (response != null) {
try {
response.close();
} catch (IOException e) {
}
}
} }
WxMpStableAccessTokenRequest wxMaAccessTokenRequest = new WxMpStableAccessTokenRequest();
wxMaAccessTokenRequest.setAppid(this.getWxMpConfigStorage().getAppId());
wxMaAccessTokenRequest.setSecret(this.getWxMpConfigStorage().getSecret());
wxMaAccessTokenRequest.setGrantType("client_credential");
wxMaAccessTokenRequest.setForceRefresh(forceRefresh);
httpPost.setEntity(new StringEntity(wxMaAccessTokenRequest.toJson(), ContentType.APPLICATION_JSON));
return getRequestHttpClient().execute(httpPost, ApacheBasicResponseHandler.INSTANCE);
} }
} }

View File

@ -8,7 +8,6 @@ import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
import me.chanjar.weixin.common.util.json.WxGsonBuilder; import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig; 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.client.methods.HttpPost;
import org.apache.http.entity.StringEntity; import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
@ -36,16 +35,11 @@ public class MaterialDeleteApacheHttpRequestExecutor extends MaterialDeleteReque
Map<String, String> params = new HashMap<>(); Map<String, String> params = new HashMap<>();
params.put("media_id", materialId); params.put("media_id", materialId);
httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params))); httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params)));
try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) { String responseContent = requestHttp.getRequestHttpClient().execute(httpPost, Utf8ResponseHandler.INSTANCE);
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); WxError error = WxError.fromJson(responseContent, WxType.MP);
WxError error = WxError.fromJson(responseContent, WxType.MP); if (error.getErrorCode() != 0) {
if (error.getErrorCode() != 0) { throw new WxErrorException(error);
throw new WxErrorException(error);
} else {
return true;
}
} finally {
httpPost.releaseConnection();
} }
return true;
} }
} }

View File

@ -51,8 +51,6 @@ public class MaterialNewsInfoApacheHttpRequestExecutor
} else { } else {
return WxMpGsonBuilder.create().fromJson(responseContent, WxMpMaterialNews.class); return WxMpGsonBuilder.create().fromJson(responseContent, WxMpMaterialNews.class);
} }
} finally {
httpPost.releaseConnection();
} }
} }
} }

View File

@ -68,8 +68,6 @@ public class MaterialUploadApacheHttpRequestExecutor extends MaterialUploadReque
} else { } else {
return WxMpMaterialUploadResult.fromJson(responseContent); return WxMpMaterialUploadResult.fromJson(responseContent);
} }
} finally {
httpPost.releaseConnection();
} }
} }
} }

View File

@ -9,7 +9,6 @@ import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import me.chanjar.weixin.mp.bean.material.WxMpMaterialVideoInfoResult; import me.chanjar.weixin.mp.bean.material.WxMpMaterialVideoInfoResult;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig; 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.client.methods.HttpPost;
import org.apache.http.entity.StringEntity; import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
@ -37,16 +36,12 @@ public class MaterialVideoInfoApacheHttpRequestExecutor extends MaterialVideoInf
Map<String, String> params = new HashMap<>(); Map<String, String> params = new HashMap<>();
params.put("media_id", materialId); params.put("media_id", materialId);
httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params))); httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params)));
try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) { String responseContent = requestHttp.getRequestHttpClient().execute(httpPost, Utf8ResponseHandler.INSTANCE);
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); WxError error = WxError.fromJson(responseContent, WxType.MP);
WxError error = WxError.fromJson(responseContent, WxType.MP); if (error.getErrorCode() != 0) {
if (error.getErrorCode() != 0) { throw new WxErrorException(error);
throw new WxErrorException(error); } else {
} else { return WxMpMaterialVideoInfoResult.fromJson(responseContent);
return WxMpMaterialVideoInfoResult.fromJson(responseContent);
}
} finally {
httpPost.releaseConnection();
} }
} }
} }

View File

@ -57,8 +57,6 @@ public class MaterialVoiceAndImageDownloadApacheHttpRequestExecutor extends Mate
} }
} }
return new ByteArrayInputStream(responseContent); return new ByteArrayInputStream(responseContent);
} finally {
httpPost.releaseConnection();
} }
} }
} }

View File

@ -55,8 +55,6 @@ public class MediaImgUploadApacheHttpRequestExecutor extends MediaImgUploadReque
} }
return WxMediaImgUploadResult.fromJson(responseContent); return WxMediaImgUploadResult.fromJson(responseContent);
} finally {
httpPost.releaseConnection();
} }
} }
} }

View File

@ -59,8 +59,6 @@ public class QrCodeApacheHttpRequestExecutor extends QrCodeRequestExecutor<Close
} }
} }
return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg"); return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg");
} finally {
httpGet.releaseConnection();
} }
} }
} }

View File

@ -8,7 +8,6 @@ import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig; 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.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.entity.mime.MultipartEntityBuilder;
@ -48,16 +47,11 @@ public class VoiceUploadApacheHttpRequestExecutor extends VoiceUploadRequestExec
.build(); .build();
httpPost.setEntity(entity); httpPost.setEntity(entity);
try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) { String responseContent = requestHttp.getRequestHttpClient().execute(httpPost, Utf8ResponseHandler.INSTANCE);
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); WxError error = WxError.fromJson(responseContent, WxType.MP);
WxError error = WxError.fromJson(responseContent, WxType.MP); if (error.getErrorCode() != 0) {
if (error.getErrorCode() != 0) { throw new WxErrorException(error);
throw new WxErrorException(error);
}
return true;
} finally {
httpPost.releaseConnection();
} }
return true;
} }
} }

View File

@ -2,17 +2,17 @@ package me.chanjar.weixin.open.executor;
import lombok.Getter; import lombok.Getter;
import me.chanjar.weixin.common.bean.CommonUploadData; import me.chanjar.weixin.common.bean.CommonUploadData;
import me.chanjar.weixin.open.bean.CommonUploadMultiParam;
import me.chanjar.weixin.common.bean.CommonUploadParam; import me.chanjar.weixin.common.bean.CommonUploadParam;
import me.chanjar.weixin.common.enums.WxType; import me.chanjar.weixin.common.enums.WxType;
import me.chanjar.weixin.common.error.WxError; import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestHttp; import me.chanjar.weixin.common.util.http.RequestHttp;
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler; import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
import me.chanjar.weixin.open.bean.CommonUploadMultiParam;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.Consts; import org.apache.http.Consts;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig; 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.client.methods.HttpPost;
import org.apache.http.entity.ContentType; import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.HttpMultipartMode;
@ -64,19 +64,15 @@ public class CommonUploadMultiRequestExecutorApacheImpl extends CommonUploadMult
httpPost.setEntity(entity.build()); httpPost.setEntity(entity.build());
} }
try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) { String responseContent = requestHttp.getRequestHttpClient().execute(httpPost, Utf8ResponseHandler.INSTANCE);
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); if (StringUtils.isEmpty(responseContent)) {
if (responseContent == null || responseContent.isEmpty()) { throw new WxErrorException(String.format("上传失败,服务器响应空 url:%s param:%s", uri, param));
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();
} }
WxError error = WxError.fromJson(responseContent, wxType);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
}
return responseContent;
} }
/** /**

View File

@ -131,11 +131,7 @@ public class GenericUploadRequestExecutor implements RequestExecutor<String, Inp
bodyRequest.setEntity(entity); bodyRequest.setEntity(entity);
bodyRequest.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString()); bodyRequest.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString());
try (CloseableHttpResponse response = getRequestHttp().getRequestHttpClient().execute(bodyRequest)) { return getRequestHttp().getRequestHttpClient().execute(bodyRequest, Utf8ResponseHandler.INSTANCE);
return Utf8ResponseHandler.INSTANCE.handleResponse(response);
} finally {
bodyRequest.releaseConnection();
}
} }
} }

View File

@ -61,8 +61,6 @@ public class MaQrCodeApacheHttpRequestExecutor extends MaQrCodeRequestExecutor<C
} }
} }
return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg"); return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg");
} finally {
httpGet.releaseConnection();
} }
} }
} }

View File

@ -7,6 +7,7 @@ import com.github.binarywang.wxpay.v3.WxPayV3DownloadHttpGet;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.util.http.apache.ByteArrayResponseHandler;
import me.chanjar.weixin.common.util.json.GsonParser; import me.chanjar.weixin.common.util.json.GsonParser;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.http.*; import org.apache.http.*;
@ -54,15 +55,11 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
HttpClientBuilder httpClientBuilder = createHttpClientBuilder(useKey); HttpClientBuilder httpClientBuilder = createHttpClientBuilder(useKey);
HttpPost httpPost = this.createHttpPost(url, requestStr); HttpPost httpPost = this.createHttpPost(url, requestStr);
try (CloseableHttpClient httpClient = httpClientBuilder.build()) { try (CloseableHttpClient httpClient = httpClientBuilder.build()) {
try (CloseableHttpResponse response = httpClient.execute(httpPost)) { final byte[] bytes = httpClient.execute(httpPost, ByteArrayResponseHandler.INSTANCE);
final byte[] bytes = EntityUtils.toByteArray(response.getEntity()); final String responseData = Base64.getEncoder().encodeToString(bytes);
final String responseData = Base64.getEncoder().encodeToString(bytes); this.logRequestAndResponse(url, requestStr, responseData);
this.logRequestAndResponse(url, requestStr, responseData); wxApiData.set(new WxPayApiData(url, requestStr, responseData, null));
wxApiData.set(new WxPayApiData(url, requestStr, responseData, null)); return bytes;
return bytes;
}
} finally {
httpPost.releaseConnection();
} }
} catch (Exception e) { } catch (Exception e) {
this.logError(url, requestStr, e); this.logError(url, requestStr, e);
@ -134,7 +131,7 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
@Override @Override
public String patchV3(String url, String requestStr) throws WxPayException { public String patchV3(String url, String requestStr) throws WxPayException {
HttpPatch httpPatch = new HttpPatch(url); HttpPatch httpPatch = new HttpPatch(url);
httpPatch.setEntity(this.createEntry(requestStr)); httpPatch.setEntity(createEntry(requestStr));
return this.requestV3(url, requestStr, httpPatch); return this.requestV3(url, requestStr, httpPatch);
} }
@ -187,7 +184,7 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
} }
if (HttpStatus.SC_OK == statusCode || HttpStatus.SC_NO_CONTENT == statusCode) { if (HttpStatus.SC_OK == statusCode || HttpStatus.SC_NO_CONTENT == statusCode) {
this.log.info("\n【请求地址】{}\n【响应数据】{}", url, responseString); log.info("\n【请求地址】{}\n【响应数据】{}", url, responseString);
return responseString; return responseString;
} }
@ -249,7 +246,7 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
@Override @Override
public String putV3(String url, String requestStr) throws WxPayException { public String putV3(String url, String requestStr) throws WxPayException {
HttpPut httpPut = new HttpPut(url); HttpPut httpPut = new HttpPut(url);
StringEntity entity = this.createEntry(requestStr); StringEntity entity = createEntry(requestStr);
httpPut.setEntity(entity); httpPut.setEntity(entity);
return requestV3(url, httpPut); return requestV3(url, httpPut);
} }
@ -284,8 +281,8 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
return apiV3HttpClient; return apiV3HttpClient;
} }
private StringEntity createEntry(String requestStr) { private static StringEntity createEntry(String requestStr) {
return new StringEntity(requestStr, ContentType.create(APPLICATION_JSON, "utf-8")); return new StringEntity(requestStr, ContentType.create(APPLICATION_JSON, StandardCharsets.UTF_8));
//return new StringEntity(new String(requestStr.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1)); //return new StringEntity(new String(requestStr.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
} }
@ -320,7 +317,7 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
private HttpPost createHttpPost(String url, String requestStr) { private HttpPost createHttpPost(String url, String requestStr) {
HttpPost httpPost = new HttpPost(url); HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(this.createEntry(requestStr)); httpPost.setEntity(createEntry(requestStr));
httpPost.setConfig(RequestConfig.custom() httpPost.setConfig(RequestConfig.custom()
.setConnectionRequestTimeout(this.getConfig().getHttpConnectionTimeout()) .setConnectionRequestTimeout(this.getConfig().getHttpConnectionTimeout())

View File

@ -3,14 +3,13 @@ package me.chanjar.weixin.qidian.api.impl;
import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.error.WxRuntimeException; import me.chanjar.weixin.common.error.WxRuntimeException;
import me.chanjar.weixin.common.util.http.HttpType; import me.chanjar.weixin.common.util.http.HttpType;
import me.chanjar.weixin.common.util.http.apache.ApacheBasicResponseHandler;
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder; import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder; import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder;
import me.chanjar.weixin.qidian.config.WxQidianConfigStorage; import me.chanjar.weixin.qidian.config.WxQidianConfigStorage;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig; 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.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import java.io.IOException; import java.io.IOException;
@ -86,11 +85,8 @@ public class WxQidianServiceHttpClientImpl extends BaseWxQidianServiceImpl<Close
RequestConfig requestConfig = RequestConfig.custom().setProxy(this.getRequestHttpProxy()).build(); RequestConfig requestConfig = RequestConfig.custom().setProxy(this.getRequestHttpProxy()).build();
httpGet.setConfig(requestConfig); httpGet.setConfig(requestConfig);
} }
try (CloseableHttpResponse response = getRequestHttpClient().execute(httpGet)) { String responseContent = getRequestHttpClient().execute(httpGet, ApacheBasicResponseHandler.INSTANCE);
return this.extractAccessToken(new BasicResponseHandler().handleResponse(response)); return this.extractAccessToken(responseContent);
} finally {
httpGet.releaseConnection();
}
} catch (IOException e) { } catch (IOException e) {
throw new WxRuntimeException(e); throw new WxRuntimeException(e);
} }