🎨 修复CloseableHttpClient相关的误用代码
This commit is contained in:
@@ -1,28 +1,28 @@
|
||||
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 me.chanjar.weixin.channel.bean.token.StableTokenParam;
|
||||
import me.chanjar.weixin.channel.config.WxChannelConfig;
|
||||
import me.chanjar.weixin.channel.util.JsonUtils;
|
||||
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.DefaultApacheHttpClientBuilder;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.client.HttpClient;
|
||||
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.HttpPost;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.BasicResponseHandler;
|
||||
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>
|
||||
*/
|
||||
@@ -76,27 +76,12 @@ public class WxChannelServiceHttpClientImpl extends BaseWxChannelServiceImpl<Htt
|
||||
|
||||
url = String.format(url, config.getAppid(), config.getSecret());
|
||||
|
||||
HttpGet httpGet = null;
|
||||
CloseableHttpResponse response = null;
|
||||
try {
|
||||
httpGet = new HttpGet(url);
|
||||
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) {
|
||||
}
|
||||
}
|
||||
HttpGet httpGet = new HttpGet(url);
|
||||
if (this.getRequestHttpProxy() != null) {
|
||||
RequestConfig requestConfig = RequestConfig.custom().setProxy(this.getRequestHttpProxy()).build();
|
||||
httpGet.setConfig(requestConfig);
|
||||
}
|
||||
return getRequestHttpClient().execute(httpGet, ApacheBasicResponseHandler.INSTANCE);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -125,10 +110,6 @@ public class WxChannelServiceHttpClientImpl extends BaseWxChannelServiceImpl<Htt
|
||||
assert requestJson != null;
|
||||
|
||||
httpPost.setEntity(new StringEntity(requestJson, ContentType.APPLICATION_JSON));
|
||||
try (CloseableHttpResponse response = getRequestHttpClient().execute(httpPost)) {
|
||||
return new BasicResponseHandler().handleResponse(response);
|
||||
} finally {
|
||||
httpPost.releaseConnection();
|
||||
}
|
||||
return getRequestHttpClient().execute(httpPost, ApacheBasicResponseHandler.INSTANCE);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,5 @@
|
||||
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.error.WxErrorException;
|
||||
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.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;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* 视频号小店 图片上传接口 请求的参数是File, 返回的结果是String
|
||||
*
|
||||
@@ -47,11 +45,7 @@ public class ChannelFileUploadRequestExecutor implements RequestExecutor<String,
|
||||
.build();
|
||||
httpPost.setEntity(entity);
|
||||
}
|
||||
try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) {
|
||||
return Utf8ResponseHandler.INSTANCE.handleResponse(response);
|
||||
} finally {
|
||||
httpPost.releaseConnection();
|
||||
}
|
||||
return requestHttp.getRequestHttpClient().execute(httpPost, Utf8ResponseHandler.INSTANCE);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -60,6 +54,7 @@ public class ChannelFileUploadRequestExecutor implements RequestExecutor<String,
|
||||
handler.handle(this.execute(uri, data, wxType));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static RequestExecutor<String, File> create(RequestHttp<?, ?> requestHttp) throws WxErrorException {
|
||||
switch (requestHttp.getRequestType()) {
|
||||
case APACHE_HTTP:
|
||||
|
||||
@@ -1,14 +1,5 @@
|
||||
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 me.chanjar.weixin.channel.bean.image.ChannelImageResponse;
|
||||
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.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";
|
||||
}
|
||||
File file = createTmpFile(inputStream, baseName, extension, tmpDirFile);
|
||||
ChannelImageResponse result = new ChannelImageResponse(file, contentType);
|
||||
return result;
|
||||
} finally {
|
||||
httpGet.releaseConnection();
|
||||
return new ChannelImageResponse(file, contentType);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user