mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2026-02-17 13:49:26 +08:00
RequestExecutor实例修改为通过create方法构建,解决了必须同时引入apache-http、jodd-http、okhttp jar的问题 (#223)
This commit is contained in:
@@ -1,46 +0,0 @@
|
||||
package me.chanjar.weixin.common.util.http;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
|
||||
import jodd.http.HttpConnectionProvider;
|
||||
import jodd.http.ProxyInfo;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkhttpProxyInfo;
|
||||
import okhttp3.ConnectionPool;
|
||||
|
||||
/**
|
||||
* Created by ecoolper on 2017/4/27.
|
||||
*/
|
||||
public abstract class AbstractRequestExecutor<T, E> implements RequestExecutor<T, E> {
|
||||
|
||||
@Override
|
||||
public T execute(RequestHttp requestHttp, String uri, E data) throws WxErrorException, IOException {
|
||||
switch (requestHttp.getRequestType()) {
|
||||
case apacheHttp: {
|
||||
//apache-http请求
|
||||
CloseableHttpClient httpClient = (CloseableHttpClient) requestHttp.getRequestHttpClient();
|
||||
HttpHost httpProxy = (HttpHost) requestHttp.getRequestHttpProxy();
|
||||
return executeApache(httpClient, httpProxy, uri, data);
|
||||
}
|
||||
case joddHttp: {
|
||||
//jodd-http请求
|
||||
HttpConnectionProvider provider = (HttpConnectionProvider) requestHttp.getRequestHttpClient();
|
||||
ProxyInfo proxyInfo = (ProxyInfo) requestHttp.getRequestHttpProxy();
|
||||
return executeJodd(provider, proxyInfo, uri, data);
|
||||
}
|
||||
case okHttp: {
|
||||
//okhttp请求
|
||||
ConnectionPool pool = (ConnectionPool) requestHttp.getRequestHttpClient();
|
||||
OkhttpProxyInfo proxyInfo = (OkhttpProxyInfo) requestHttp.getRequestHttpProxy();
|
||||
return executeOkhttp(pool, proxyInfo, uri, data);
|
||||
}
|
||||
default:
|
||||
//TODO 这里需要抛出异常,需要优化
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -8,8 +8,11 @@ import me.chanjar.weixin.common.bean.result.WxError;
|
||||
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.fs.FileUtils;
|
||||
import me.chanjar.weixin.common.util.http.apache.ApacheMediaDownloadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.apache.InputStreamResponseHandler;
|
||||
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
|
||||
import me.chanjar.weixin.common.util.http.jodd.JoddMediaDownloadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkMediaDownloadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkhttpProxyInfo;
|
||||
import okhttp3.*;
|
||||
|
||||
@@ -37,207 +40,26 @@ import java.util.regex.Pattern;
|
||||
*
|
||||
* @author Daniel Qian
|
||||
*/
|
||||
public class MediaDownloadRequestExecutor extends AbstractRequestExecutor<File, String> {
|
||||
public abstract class MediaDownloadRequestExecutor<H, P> implements RequestExecutor<File, String> {
|
||||
|
||||
private File tmpDirFile;
|
||||
public static RequestExecutor<File, String> create(RequestHttp requestHttp, File tmpDirFile){
|
||||
switch (requestHttp.getRequestType()){
|
||||
case apacheHttp:
|
||||
return new ApacheMediaDownloadRequestExecutor(requestHttp, tmpDirFile);
|
||||
case joddHttp:
|
||||
return new JoddMediaDownloadRequestExecutor(requestHttp, tmpDirFile);
|
||||
case okHttp:
|
||||
return new OkMediaDownloadRequestExecutor(requestHttp, tmpDirFile);
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public MediaDownloadRequestExecutor(File tmpDirFile) {
|
||||
protected RequestHttp<H, P> requestHttp;
|
||||
protected File tmpDirFile;
|
||||
|
||||
public MediaDownloadRequestExecutor(RequestHttp requestHttp, File tmpDirFile) {
|
||||
this.tmpDirFile = tmpDirFile;
|
||||
}
|
||||
|
||||
private String getFileName(HttpResponse response) throws WxErrorException {
|
||||
String content = response.header("Content-disposition");
|
||||
if (content == null || content.length() == 0) {
|
||||
throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build());
|
||||
}
|
||||
|
||||
Pattern p = Pattern.compile(".*filename=\"(.*)\"");
|
||||
Matcher m = p.matcher(content);
|
||||
if (m.matches()) {
|
||||
return m.group(1);
|
||||
}
|
||||
throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build());
|
||||
}
|
||||
|
||||
private String getFileName(CloseableHttpResponse response) throws WxErrorException {
|
||||
Header[] contentDispositionHeader = response.getHeaders("Content-disposition");
|
||||
if (contentDispositionHeader == null || contentDispositionHeader.length == 0) {
|
||||
throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build());
|
||||
}
|
||||
|
||||
Pattern p = Pattern.compile(".*filename=\"(.*)\"");
|
||||
Matcher m = p.matcher(contentDispositionHeader[0].getValue());
|
||||
if (m.matches()) {
|
||||
return m.group(1);
|
||||
}
|
||||
throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* apache-http实现方式
|
||||
*
|
||||
* @param httpclient
|
||||
* @param httpProxy
|
||||
* @param uri
|
||||
* @param queryParam
|
||||
* @return
|
||||
* @throws WxErrorException
|
||||
* @throws IOException
|
||||
*/
|
||||
public File executeApache(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String queryParam) throws WxErrorException, IOException {
|
||||
if (queryParam != null) {
|
||||
if (uri.indexOf('?') == -1) {
|
||||
uri += '?';
|
||||
}
|
||||
uri += uri.endsWith("?") ? queryParam : '&' + queryParam;
|
||||
}
|
||||
|
||||
HttpGet httpGet = new HttpGet(uri);
|
||||
if (httpProxy != null) {
|
||||
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
|
||||
httpGet.setConfig(config);
|
||||
}
|
||||
|
||||
try (CloseableHttpResponse response = httpclient.execute(httpGet);
|
||||
InputStream inputStream = InputStreamResponseHandler.INSTANCE
|
||||
.handleResponse(response)) {
|
||||
Header[] contentTypeHeader = response.getHeaders("Content-Type");
|
||||
if (contentTypeHeader != null && contentTypeHeader.length > 0) {
|
||||
if (contentTypeHeader[0].getValue().startsWith(ContentType.APPLICATION_JSON.getMimeType())) {
|
||||
// application/json; encoding=utf-8 下载媒体文件出错
|
||||
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
|
||||
throw new WxErrorException(WxError.fromJson(responseContent));
|
||||
}
|
||||
}
|
||||
|
||||
String fileName = getFileName(response);
|
||||
if (StringUtils.isBlank(fileName)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String[] nameAndExt = fileName.split("\\.");
|
||||
return FileUtils.createTmpFile(inputStream, nameAndExt[0], nameAndExt[1], this.tmpDirFile);
|
||||
|
||||
} finally {
|
||||
httpGet.releaseConnection();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* jodd-http实现方式
|
||||
*
|
||||
* @param provider
|
||||
* @param proxyInfo
|
||||
* @param uri
|
||||
* @param queryParam
|
||||
* @return
|
||||
* @throws WxErrorException
|
||||
* @throws IOException
|
||||
*/
|
||||
public File executeJodd(HttpConnectionProvider provider, ProxyInfo proxyInfo, String uri, String queryParam) throws WxErrorException, IOException {
|
||||
if (queryParam != null) {
|
||||
if (uri.indexOf('?') == -1) {
|
||||
uri += '?';
|
||||
}
|
||||
uri += uri.endsWith("?") ? queryParam : '&' + queryParam;
|
||||
}
|
||||
|
||||
HttpRequest request = HttpRequest.get(uri);
|
||||
if (proxyInfo != null) {
|
||||
provider.useProxy(proxyInfo);
|
||||
}
|
||||
request.withConnectionProvider(provider);
|
||||
|
||||
HttpResponse response = request.send();
|
||||
String contentType = response.header("Content-Type");
|
||||
if (contentType != null && contentType.startsWith("application/json")) {
|
||||
// application/json; encoding=utf-8 下载媒体文件出错
|
||||
throw new WxErrorException(WxError.fromJson(response.bodyText()));
|
||||
}
|
||||
|
||||
String fileName = getFileName(response);
|
||||
if (StringUtils.isBlank(fileName)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
InputStream inputStream = new ByteArrayInputStream(response.bodyBytes());
|
||||
String[] nameAndExt = fileName.split("\\.");
|
||||
return FileUtils.createTmpFile(inputStream, nameAndExt[0], nameAndExt[1], this.tmpDirFile);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* okhttp现实方式
|
||||
*
|
||||
* @param pool
|
||||
* @param proxyInfo
|
||||
* @param uri
|
||||
* @param queryParam
|
||||
* @return
|
||||
* @throws WxErrorException
|
||||
* @throws IOException
|
||||
*/
|
||||
public File executeOkhttp(ConnectionPool pool, final OkhttpProxyInfo proxyInfo, String uri, String queryParam) throws WxErrorException, IOException {
|
||||
if (queryParam != null) {
|
||||
if (uri.indexOf('?') == -1) {
|
||||
uri += '?';
|
||||
}
|
||||
uri += uri.endsWith("?") ? queryParam : '&' + queryParam;
|
||||
}
|
||||
|
||||
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().connectionPool(pool);
|
||||
//设置代理
|
||||
if (proxyInfo != null) {
|
||||
clientBuilder.proxy(proxyInfo.getProxy());
|
||||
}
|
||||
//设置授权
|
||||
clientBuilder.authenticator(new Authenticator() {
|
||||
@Override
|
||||
public Request authenticate(Route route, Response response) throws IOException {
|
||||
String credential = Credentials.basic(proxyInfo.getProxyUsername(), proxyInfo.getProxyPassword());
|
||||
return response.request().newBuilder()
|
||||
.header("Authorization", credential)
|
||||
.build();
|
||||
}
|
||||
});
|
||||
//得到httpClient
|
||||
OkHttpClient client = clientBuilder.build();
|
||||
|
||||
Request request = new Request.Builder().url(uri).get().build();
|
||||
|
||||
Response response = client.newCall(request).execute();
|
||||
|
||||
String contentType = response.header("Content-Type");
|
||||
if (contentType != null && contentType.startsWith("application/json")) {
|
||||
// application/json; encoding=utf-8 下载媒体文件出错
|
||||
throw new WxErrorException(WxError.fromJson(response.body().string()));
|
||||
}
|
||||
|
||||
String fileName = getFileName(response);
|
||||
if (StringUtils.isBlank(fileName)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
InputStream inputStream = new ByteArrayInputStream(response.body().bytes());
|
||||
String[] nameAndExt = fileName.split("\\.");
|
||||
return FileUtils.createTmpFile(inputStream, nameAndExt[0], nameAndExt[1], this.tmpDirFile);
|
||||
}
|
||||
|
||||
private String getFileName(Response response) throws WxErrorException {
|
||||
String content = response.header("Content-disposition");
|
||||
if (content == null || content.length() == 0) {
|
||||
throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build());
|
||||
}
|
||||
|
||||
Pattern p = Pattern.compile(".*filename=\"(.*)\"");
|
||||
Matcher m = p.matcher(content);
|
||||
if (m.matches()) {
|
||||
return m.group(1);
|
||||
}
|
||||
throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -7,8 +7,11 @@ import jodd.http.ProxyInfo;
|
||||
import me.chanjar.weixin.common.bean.result.WxError;
|
||||
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.apache.ApacheMediaUploadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
|
||||
|
||||
import me.chanjar.weixin.common.util.http.jodd.JoddMediaUploadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkMediaUploadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkhttpProxyInfo;
|
||||
import okhttp3.*;
|
||||
|
||||
@@ -32,117 +35,24 @@ import java.net.Proxy;
|
||||
*
|
||||
* @author Daniel Qian
|
||||
*/
|
||||
public class MediaUploadRequestExecutor extends AbstractRequestExecutor<WxMediaUploadResult, File> {
|
||||
public abstract class MediaUploadRequestExecutor<H, P> implements RequestExecutor<WxMediaUploadResult, File> {
|
||||
protected RequestHttp<H,P> requestHttp;
|
||||
|
||||
/**
|
||||
* apache-http实现方式
|
||||
*
|
||||
* @param httpclient
|
||||
* @param httpProxy
|
||||
* @param uri
|
||||
* @param file
|
||||
* @return
|
||||
* @throws WxErrorException
|
||||
* @throws IOException
|
||||
*/
|
||||
public WxMediaUploadResult executeApache(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, File file) throws WxErrorException, IOException {
|
||||
HttpPost httpPost = new HttpPost(uri);
|
||||
if (httpProxy != null) {
|
||||
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
|
||||
httpPost.setConfig(config);
|
||||
}
|
||||
if (file != null) {
|
||||
HttpEntity entity = MultipartEntityBuilder
|
||||
.create()
|
||||
.addBinaryBody("media", file)
|
||||
.setMode(HttpMultipartMode.RFC6532)
|
||||
.build();
|
||||
httpPost.setEntity(entity);
|
||||
httpPost.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString());
|
||||
}
|
||||
try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
|
||||
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
|
||||
WxError error = WxError.fromJson(responseContent);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
}
|
||||
return WxMediaUploadResult.fromJson(responseContent);
|
||||
} finally {
|
||||
httpPost.releaseConnection();
|
||||
}
|
||||
public MediaUploadRequestExecutor(RequestHttp requestHttp){
|
||||
this.requestHttp =requestHttp;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* jodd-http实现方式
|
||||
*
|
||||
* @param provider
|
||||
* @param proxyInfo
|
||||
* @param uri
|
||||
* @param file
|
||||
* @return
|
||||
* @throws WxErrorException
|
||||
* @throws IOException
|
||||
*/
|
||||
public WxMediaUploadResult executeJodd(HttpConnectionProvider provider, ProxyInfo proxyInfo, String uri, File file) throws WxErrorException, IOException {
|
||||
HttpRequest request = HttpRequest.post(uri);
|
||||
if (proxyInfo != null) {
|
||||
provider.useProxy(proxyInfo);
|
||||
public static RequestExecutor<WxMediaUploadResult, File> create(RequestHttp requestHttp){
|
||||
switch (requestHttp.getRequestType()){
|
||||
case apacheHttp:
|
||||
return new ApacheMediaUploadRequestExecutor(requestHttp);
|
||||
case joddHttp:
|
||||
return new JoddMediaUploadRequestExecutor(requestHttp);
|
||||
case okHttp:
|
||||
return new OkMediaUploadRequestExecutor(requestHttp);
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
request.withConnectionProvider(provider);
|
||||
request.form("media", file);
|
||||
HttpResponse response = request.send();
|
||||
String responseContent = response.bodyText();
|
||||
WxError error = WxError.fromJson(responseContent);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
}
|
||||
return WxMediaUploadResult.fromJson(responseContent);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* okhttp现实方式
|
||||
*
|
||||
* @param pool
|
||||
* @param proxyInfo
|
||||
* @param uri
|
||||
* @param file
|
||||
* @return
|
||||
* @throws WxErrorException
|
||||
* @throws IOException
|
||||
*/
|
||||
public WxMediaUploadResult executeOkhttp(ConnectionPool pool, final OkhttpProxyInfo proxyInfo, String uri, File file) throws WxErrorException, IOException {
|
||||
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().connectionPool(pool);
|
||||
//设置代理
|
||||
if (proxyInfo != null) {
|
||||
clientBuilder.proxy(proxyInfo.getProxy());
|
||||
}
|
||||
//设置授权
|
||||
clientBuilder.authenticator(new Authenticator() {
|
||||
@Override
|
||||
public Request authenticate(Route route, Response response) throws IOException {
|
||||
String credential = Credentials.basic(proxyInfo.getProxyUsername(), proxyInfo.getProxyPassword());
|
||||
return response.request().newBuilder()
|
||||
.header("Authorization", credential)
|
||||
.build();
|
||||
}
|
||||
});
|
||||
//得到httpClient
|
||||
OkHttpClient client = clientBuilder.build();
|
||||
|
||||
RequestBody fileBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
|
||||
RequestBody body = new MultipartBody.Builder().addFormDataPart("media", null, fileBody).build();
|
||||
Request request = new Request.Builder().url(uri).post(body).build();
|
||||
|
||||
Response response = client.newCall(request).execute();
|
||||
String responseContent = response.body().string();
|
||||
WxError error = WxError.fromJson(responseContent);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
}
|
||||
return WxMediaUploadResult.fromJson(responseContent);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ public interface RequestExecutor<T, E> {
|
||||
* @throws WxErrorException
|
||||
* @throws IOException
|
||||
*/
|
||||
T execute(RequestHttp requestHttp, String uri, E data) throws WxErrorException, IOException;
|
||||
T execute(String uri, E data) throws WxErrorException, IOException;
|
||||
|
||||
/**
|
||||
* apache-http实现方式
|
||||
@@ -37,10 +37,10 @@ public interface RequestExecutor<T, E> {
|
||||
* @return
|
||||
* @throws WxErrorException
|
||||
* @throws IOException
|
||||
*/
|
||||
*//*
|
||||
T executeApache(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, E data) throws WxErrorException, IOException;
|
||||
|
||||
/**
|
||||
*//**
|
||||
* jodd-http实现方式
|
||||
* @param provider
|
||||
* @param proxyInfo
|
||||
@@ -49,11 +49,11 @@ public interface RequestExecutor<T, E> {
|
||||
* @return
|
||||
* @throws WxErrorException
|
||||
* @throws IOException
|
||||
*/
|
||||
*//*
|
||||
T executeJodd(HttpConnectionProvider provider, ProxyInfo proxyInfo, String uri, E data) throws WxErrorException, IOException;
|
||||
|
||||
|
||||
/** okhttp实现方式
|
||||
*//** okhttp实现方式
|
||||
* @param pool
|
||||
* @param proxyInfo
|
||||
* @param uri
|
||||
@@ -61,7 +61,7 @@ public interface RequestExecutor<T, E> {
|
||||
* @return
|
||||
* @throws WxErrorException
|
||||
* @throws IOException
|
||||
*/
|
||||
*//*
|
||||
T executeOkhttp(ConnectionPool pool, final OkhttpProxyInfo proxyInfo, String uri, E data) throws WxErrorException, IOException;
|
||||
|
||||
*/
|
||||
}
|
||||
|
||||
@@ -6,8 +6,11 @@ import jodd.http.HttpResponse;
|
||||
import jodd.http.ProxyInfo;
|
||||
import me.chanjar.weixin.common.bean.result.WxError;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.apache.ApacheSimpleGetRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
|
||||
|
||||
import me.chanjar.weixin.common.util.http.jodd.JoddSimpleGetRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkSimpleGetRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkhttpProxyInfo;
|
||||
import okhttp3.*;
|
||||
|
||||
@@ -27,126 +30,25 @@ import java.util.concurrent.TimeUnit;
|
||||
*
|
||||
* @author Daniel Qian
|
||||
*/
|
||||
public class SimpleGetRequestExecutor extends AbstractRequestExecutor<String, String> {
|
||||
public abstract class SimpleGetRequestExecutor<H, P> implements RequestExecutor<String, String> {
|
||||
protected RequestHttp<H,P> requestHttp;
|
||||
|
||||
|
||||
/**
|
||||
* apache-http实现方式
|
||||
*
|
||||
* @param httpclient
|
||||
* @param httpProxy
|
||||
* @param uri
|
||||
* @param queryParam
|
||||
* @return
|
||||
* @throws WxErrorException
|
||||
* @throws IOException
|
||||
*/
|
||||
public String executeApache(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String queryParam) throws WxErrorException, IOException {
|
||||
if (queryParam != null) {
|
||||
if (uri.indexOf('?') == -1) {
|
||||
uri += '?';
|
||||
}
|
||||
uri += uri.endsWith("?") ? queryParam : '&' + queryParam;
|
||||
}
|
||||
HttpGet httpGet = new HttpGet(uri);
|
||||
if (httpProxy != null) {
|
||||
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
|
||||
httpGet.setConfig(config);
|
||||
}
|
||||
|
||||
try (CloseableHttpResponse response = httpclient.execute(httpGet)) {
|
||||
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
|
||||
WxError error = WxError.fromJson(responseContent);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
}
|
||||
return responseContent;
|
||||
} finally {
|
||||
httpGet.releaseConnection();
|
||||
}
|
||||
public SimpleGetRequestExecutor(RequestHttp requestHttp){
|
||||
this.requestHttp =requestHttp;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* jodd-http实现方式
|
||||
*
|
||||
* @param provider
|
||||
* @param proxyInfo
|
||||
* @param uri
|
||||
* @param queryParam
|
||||
* @return
|
||||
* @throws WxErrorException
|
||||
* @throws IOException
|
||||
*/
|
||||
public String executeJodd(HttpConnectionProvider provider, ProxyInfo proxyInfo, String uri, String queryParam) throws WxErrorException, IOException {
|
||||
if (queryParam != null) {
|
||||
if (uri.indexOf('?') == -1) {
|
||||
uri += '?';
|
||||
}
|
||||
uri += uri.endsWith("?") ? queryParam : '&' + queryParam;
|
||||
public static RequestExecutor<String, String> create(RequestHttp requestHttp){
|
||||
switch(requestHttp.getRequestType()){
|
||||
case apacheHttp:
|
||||
return new ApacheSimpleGetRequestExecutor(requestHttp);
|
||||
case joddHttp:
|
||||
return new JoddSimpleGetRequestExecutor(requestHttp);
|
||||
case okHttp:
|
||||
return new OkSimpleGetRequestExecutor(requestHttp);
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
|
||||
HttpRequest request = HttpRequest.get(uri);
|
||||
if (proxyInfo != null) {
|
||||
provider.useProxy(proxyInfo);
|
||||
}
|
||||
request.withConnectionProvider(provider);
|
||||
HttpResponse response = request.send();
|
||||
String responseContent = response.bodyText();
|
||||
WxError error = WxError.fromJson(responseContent);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
}
|
||||
return responseContent;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* okHttp实现方式
|
||||
*
|
||||
* @param pool
|
||||
* @param proxyInfo
|
||||
* @param uri
|
||||
* @param queryParam
|
||||
* @return
|
||||
* @throws WxErrorException
|
||||
* @throws IOException
|
||||
*/
|
||||
public String executeOkhttp(ConnectionPool pool, final OkhttpProxyInfo proxyInfo, String uri, String queryParam) throws WxErrorException, IOException {
|
||||
if (queryParam != null) {
|
||||
if (uri.indexOf('?') == -1) {
|
||||
uri += '?';
|
||||
}
|
||||
uri += uri.endsWith("?") ? queryParam : '&' + queryParam;
|
||||
}
|
||||
|
||||
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().connectionPool(pool);
|
||||
//设置代理
|
||||
if (proxyInfo != null) {
|
||||
clientBuilder.proxy(proxyInfo.getProxy());
|
||||
}
|
||||
//设置授权
|
||||
clientBuilder.authenticator(new Authenticator() {
|
||||
@Override
|
||||
public Request authenticate(Route route, Response response) throws IOException {
|
||||
String credential = Credentials.basic(proxyInfo.getProxyUsername(), proxyInfo.getProxyPassword());
|
||||
return response.request().newBuilder()
|
||||
.header("Authorization", credential)
|
||||
.build();
|
||||
}
|
||||
});
|
||||
//得到httpClient
|
||||
OkHttpClient client =clientBuilder.build();
|
||||
|
||||
Request request = new Request.Builder().url(uri).build();
|
||||
|
||||
Response response = client.newCall(request).execute();
|
||||
String responseContent = response.body().string();
|
||||
WxError error = WxError.fromJson(responseContent);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
}
|
||||
return responseContent;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -6,8 +6,12 @@ import jodd.http.HttpResponse;
|
||||
import jodd.http.ProxyInfo;
|
||||
import me.chanjar.weixin.common.bean.result.WxError;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.apache.ApacheSimpleGetRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.apache.ApacheSimplePostRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
|
||||
|
||||
import me.chanjar.weixin.common.util.http.jodd.JoddSimplePostRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkSimplePostRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkhttpProxyInfo;
|
||||
import okhttp3.*;
|
||||
|
||||
@@ -27,142 +31,24 @@ import java.io.IOException;
|
||||
*
|
||||
* @author Daniel Qian
|
||||
*/
|
||||
public class SimplePostRequestExecutor extends AbstractRequestExecutor<String, String> {
|
||||
public abstract class SimplePostRequestExecutor<H, P> implements RequestExecutor<String, String> {
|
||||
protected RequestHttp<H,P> requestHttp;
|
||||
|
||||
/**
|
||||
* apache-http实现方式
|
||||
*
|
||||
* @param httpclient
|
||||
* @param httpProxy
|
||||
* @param uri
|
||||
* @param postEntity
|
||||
* @return
|
||||
* @throws WxErrorException
|
||||
* @throws IOException
|
||||
*/
|
||||
public String executeApache(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String postEntity) throws WxErrorException, IOException {
|
||||
|
||||
HttpPost httpPost = new HttpPost(uri);
|
||||
if (httpProxy != null) {
|
||||
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
|
||||
httpPost.setConfig(config);
|
||||
}
|
||||
|
||||
if (postEntity != null) {
|
||||
StringEntity entity = new StringEntity(postEntity, Consts.UTF_8);
|
||||
httpPost.setEntity(entity);
|
||||
}
|
||||
|
||||
try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
|
||||
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
|
||||
if (responseContent.isEmpty()) {
|
||||
throw new WxErrorException(
|
||||
WxError.newBuilder().setErrorCode(9999).setErrorMsg("无响应内容")
|
||||
.build());
|
||||
}
|
||||
|
||||
if (responseContent.startsWith("<xml>")) {
|
||||
//xml格式输出直接返回
|
||||
return responseContent;
|
||||
}
|
||||
|
||||
WxError error = WxError.fromJson(responseContent);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
}
|
||||
return responseContent;
|
||||
} finally {
|
||||
httpPost.releaseConnection();
|
||||
}
|
||||
public SimplePostRequestExecutor(RequestHttp requestHttp){
|
||||
this.requestHttp =requestHttp;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* jodd-http实现方式
|
||||
*
|
||||
* @param provider
|
||||
* @param proxyInfo
|
||||
* @param uri
|
||||
* @param postEntity
|
||||
* @return
|
||||
* @throws WxErrorException
|
||||
* @throws IOException
|
||||
*/
|
||||
public String executeJodd(HttpConnectionProvider provider, ProxyInfo proxyInfo, String uri, String postEntity) throws WxErrorException, IOException {
|
||||
HttpRequest request = HttpRequest.post(uri);
|
||||
if (proxyInfo != null) {
|
||||
provider.useProxy(proxyInfo);
|
||||
public static RequestExecutor<String, String> create(RequestHttp requestHttp){
|
||||
switch (requestHttp.getRequestType()){
|
||||
case apacheHttp:
|
||||
return new ApacheSimplePostRequestExecutor(requestHttp);
|
||||
case joddHttp:
|
||||
return new JoddSimplePostRequestExecutor(requestHttp);
|
||||
case okHttp:
|
||||
return new OkSimplePostRequestExecutor(requestHttp);
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
request.withConnectionProvider(provider);
|
||||
if (postEntity != null) {
|
||||
request.bodyText(postEntity);
|
||||
}
|
||||
HttpResponse response = request.send();
|
||||
|
||||
String responseContent = response.bodyText();
|
||||
if (responseContent.isEmpty()) {
|
||||
throw new WxErrorException(
|
||||
WxError.newBuilder().setErrorCode(9999).setErrorMsg("无响应内容")
|
||||
.build());
|
||||
}
|
||||
|
||||
if (responseContent.startsWith("<xml>")) {
|
||||
//xml格式输出直接返回
|
||||
return responseContent;
|
||||
}
|
||||
|
||||
WxError error = WxError.fromJson(responseContent);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
}
|
||||
return responseContent;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* okHttp实现方式
|
||||
*
|
||||
* @param pool
|
||||
* @param proxyInfo
|
||||
* @param uri
|
||||
* @param postEntity
|
||||
* @return
|
||||
* @throws WxErrorException
|
||||
* @throws IOException
|
||||
*/
|
||||
public String executeOkhttp(ConnectionPool pool, final OkhttpProxyInfo proxyInfo, String uri, String postEntity) throws WxErrorException, IOException {
|
||||
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().connectionPool(pool);
|
||||
//设置代理
|
||||
if (proxyInfo != null) {
|
||||
clientBuilder.proxy(proxyInfo.getProxy());
|
||||
}
|
||||
//设置授权
|
||||
clientBuilder.authenticator(new Authenticator() {
|
||||
@Override
|
||||
public Request authenticate(Route route, Response response) throws IOException {
|
||||
String credential = Credentials.basic(proxyInfo.getProxyUsername(), proxyInfo.getProxyPassword());
|
||||
return response.request().newBuilder()
|
||||
.header("Authorization", credential)
|
||||
.build();
|
||||
}
|
||||
});
|
||||
//得到httpClient
|
||||
OkHttpClient client = clientBuilder.build();
|
||||
|
||||
|
||||
MediaType mediaType = MediaType.parse("text/plain; charset=utf-8");
|
||||
RequestBody body = RequestBody.create(mediaType, postEntity);
|
||||
|
||||
Request request = new Request.Builder().url(uri).post(body).build();
|
||||
|
||||
Response response = client.newCall(request).execute();
|
||||
String responseContent = response.body().string();
|
||||
WxError error = WxError.fromJson(responseContent);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
}
|
||||
return responseContent;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
package me.chanjar.weixin.common.util.http.apache;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.http.Header;
|
||||
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.entity.ContentType;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
|
||||
import me.chanjar.weixin.common.bean.result.WxError;
|
||||
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.fs.FileUtils;
|
||||
import me.chanjar.weixin.common.util.http.MediaDownloadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
|
||||
/**
|
||||
* Created by ecoolper on 2017/5/5.
|
||||
*/
|
||||
public class ApacheMediaDownloadRequestExecutor extends MediaDownloadRequestExecutor<CloseableHttpClient, HttpHost> {
|
||||
|
||||
|
||||
public ApacheMediaDownloadRequestExecutor(RequestHttp requestHttp, File tmpDirFile) {
|
||||
super(requestHttp, tmpDirFile);
|
||||
}
|
||||
|
||||
@Override
|
||||
public File execute(String uri, String queryParam) throws WxErrorException, IOException {
|
||||
if (queryParam != null) {
|
||||
if (uri.indexOf('?') == -1) {
|
||||
uri += '?';
|
||||
}
|
||||
uri += uri.endsWith("?") ? queryParam : '&' + queryParam;
|
||||
}
|
||||
|
||||
HttpGet httpGet = new HttpGet(uri);
|
||||
if (requestHttp.getRequestHttpProxy() != null) {
|
||||
RequestConfig config = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build();
|
||||
httpGet.setConfig(config);
|
||||
}
|
||||
|
||||
try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpGet);
|
||||
InputStream inputStream = InputStreamResponseHandler.INSTANCE
|
||||
.handleResponse(response)) {
|
||||
Header[] contentTypeHeader = response.getHeaders("Content-Type");
|
||||
if (contentTypeHeader != null && contentTypeHeader.length > 0) {
|
||||
if (contentTypeHeader[0].getValue().startsWith(ContentType.APPLICATION_JSON.getMimeType())) {
|
||||
// application/json; encoding=utf-8 下载媒体文件出错
|
||||
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
|
||||
throw new WxErrorException(WxError.fromJson(responseContent));
|
||||
}
|
||||
}
|
||||
|
||||
String fileName = getFileName(response);
|
||||
if (StringUtils.isBlank(fileName)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String[] nameAndExt = fileName.split("\\.");
|
||||
return FileUtils.createTmpFile(inputStream, nameAndExt[0], nameAndExt[1], super.tmpDirFile);
|
||||
|
||||
} finally {
|
||||
httpGet.releaseConnection();
|
||||
}
|
||||
}
|
||||
|
||||
private String getFileName(CloseableHttpResponse response) throws WxErrorException {
|
||||
Header[] contentDispositionHeader = response.getHeaders("Content-disposition");
|
||||
if (contentDispositionHeader == null || contentDispositionHeader.length == 0) {
|
||||
throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build());
|
||||
}
|
||||
|
||||
Pattern p = Pattern.compile(".*filename=\"(.*)\"");
|
||||
Matcher m = p.matcher(contentDispositionHeader[0].getValue());
|
||||
if (m.matches()) {
|
||||
return m.group(1);
|
||||
}
|
||||
throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package me.chanjar.weixin.common.util.http.apache;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
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;
|
||||
import org.apache.http.entity.mime.MultipartEntityBuilder;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
|
||||
import me.chanjar.weixin.common.bean.result.WxError;
|
||||
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
|
||||
/**
|
||||
* Created by ecoolper on 2017/5/5.
|
||||
*/
|
||||
public class ApacheMediaUploadRequestExecutor extends MediaUploadRequestExecutor<CloseableHttpClient, HttpHost> {
|
||||
public ApacheMediaUploadRequestExecutor(RequestHttp requestHttp) {
|
||||
super(requestHttp);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMediaUploadResult execute(String uri, File file) throws WxErrorException, IOException {
|
||||
HttpPost httpPost = new HttpPost(uri);
|
||||
if (requestHttp.getRequestHttpProxy() != null) {
|
||||
RequestConfig config = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build();
|
||||
httpPost.setConfig(config);
|
||||
}
|
||||
if (file != null) {
|
||||
HttpEntity entity = MultipartEntityBuilder
|
||||
.create()
|
||||
.addBinaryBody("media", file)
|
||||
.setMode(HttpMultipartMode.RFC6532)
|
||||
.build();
|
||||
httpPost.setEntity(entity);
|
||||
httpPost.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString());
|
||||
}
|
||||
try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) {
|
||||
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
|
||||
WxError error = WxError.fromJson(responseContent);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
}
|
||||
return WxMediaUploadResult.fromJson(responseContent);
|
||||
} finally {
|
||||
httpPost.releaseConnection();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package me.chanjar.weixin.common.util.http.apache;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
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;
|
||||
|
||||
import me.chanjar.weixin.common.bean.result.WxError;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
|
||||
|
||||
/**
|
||||
* Created by ecoolper on 2017/5/4.
|
||||
*/
|
||||
public class ApacheSimpleGetRequestExecutor extends SimpleGetRequestExecutor<CloseableHttpClient, HttpHost> {
|
||||
|
||||
public ApacheSimpleGetRequestExecutor(RequestHttp requestHttp) {
|
||||
super(requestHttp);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String execute(String uri, String queryParam) throws WxErrorException, IOException {
|
||||
if (queryParam != null) {
|
||||
if (uri.indexOf('?') == -1) {
|
||||
uri += '?';
|
||||
}
|
||||
uri += uri.endsWith("?") ? queryParam : '&' + queryParam;
|
||||
}
|
||||
HttpGet httpGet = new HttpGet(uri);
|
||||
if (requestHttp.getRequestHttpProxy() != null) {
|
||||
RequestConfig config = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build();
|
||||
httpGet.setConfig(config);
|
||||
}
|
||||
|
||||
try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpGet)) {
|
||||
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
|
||||
WxError error = WxError.fromJson(responseContent);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
}
|
||||
return responseContent;
|
||||
} finally {
|
||||
httpGet.releaseConnection();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package me.chanjar.weixin.common.util.http.apache;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
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;
|
||||
|
||||
import me.chanjar.weixin.common.bean.result.WxError;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
|
||||
|
||||
/**
|
||||
* Created by ecoolper on 2017/5/4.
|
||||
*/
|
||||
public class ApacheSimplePostRequestExecutor extends SimplePostRequestExecutor<CloseableHttpClient, HttpHost> {
|
||||
|
||||
public ApacheSimplePostRequestExecutor(RequestHttp requestHttp) {
|
||||
super(requestHttp);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String execute(String uri, String postEntity) throws WxErrorException, IOException {
|
||||
HttpPost httpPost = new HttpPost(uri);
|
||||
if (requestHttp.getRequestHttpProxy() != null) {
|
||||
RequestConfig config = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build();
|
||||
httpPost.setConfig(config);
|
||||
}
|
||||
|
||||
if (postEntity != null) {
|
||||
StringEntity entity = new StringEntity(postEntity, Consts.UTF_8);
|
||||
httpPost.setEntity(entity);
|
||||
}
|
||||
|
||||
try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) {
|
||||
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
|
||||
if (responseContent.isEmpty()) {
|
||||
throw new WxErrorException(
|
||||
WxError.newBuilder().setErrorCode(9999).setErrorMsg("无响应内容")
|
||||
.build());
|
||||
}
|
||||
|
||||
if (responseContent.startsWith("<xml>")) {
|
||||
//xml格式输出直接返回
|
||||
return responseContent;
|
||||
}
|
||||
|
||||
WxError error = WxError.fromJson(responseContent);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
}
|
||||
return responseContent;
|
||||
} finally {
|
||||
httpPost.releaseConnection();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package me.chanjar.weixin.common.util.http.jodd;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import jodd.http.HttpConnectionProvider;
|
||||
import jodd.http.HttpRequest;
|
||||
import jodd.http.HttpResponse;
|
||||
import jodd.http.ProxyInfo;
|
||||
import me.chanjar.weixin.common.bean.result.WxError;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.fs.FileUtils;
|
||||
import me.chanjar.weixin.common.util.http.MediaDownloadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
|
||||
/**
|
||||
* Created by ecoolper on 2017/5/5.
|
||||
*/
|
||||
public class JoddMediaDownloadRequestExecutor extends MediaDownloadRequestExecutor<HttpConnectionProvider, ProxyInfo> {
|
||||
|
||||
|
||||
public JoddMediaDownloadRequestExecutor(RequestHttp requestHttp, File tmpDirFile) {
|
||||
super(requestHttp, tmpDirFile);
|
||||
}
|
||||
|
||||
@Override
|
||||
public File execute(String uri, String queryParam) throws WxErrorException, IOException {
|
||||
if (queryParam != null) {
|
||||
if (uri.indexOf('?') == -1) {
|
||||
uri += '?';
|
||||
}
|
||||
uri += uri.endsWith("?") ? queryParam : '&' + queryParam;
|
||||
}
|
||||
|
||||
HttpRequest request = HttpRequest.get(uri);
|
||||
if (requestHttp.getRequestHttpProxy() != null) {
|
||||
requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy());
|
||||
}
|
||||
request.withConnectionProvider(requestHttp.getRequestHttpClient());
|
||||
|
||||
HttpResponse response = request.send();
|
||||
String contentType = response.header("Content-Type");
|
||||
if (contentType != null && contentType.startsWith("application/json")) {
|
||||
// application/json; encoding=utf-8 下载媒体文件出错
|
||||
throw new WxErrorException(WxError.fromJson(response.bodyText()));
|
||||
}
|
||||
|
||||
String fileName = getFileName(response);
|
||||
if (StringUtils.isBlank(fileName)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
InputStream inputStream = new ByteArrayInputStream(response.bodyBytes());
|
||||
String[] nameAndExt = fileName.split("\\.");
|
||||
return FileUtils.createTmpFile(inputStream, nameAndExt[0], nameAndExt[1], super.tmpDirFile);
|
||||
}
|
||||
|
||||
private String getFileName(HttpResponse response) throws WxErrorException {
|
||||
String content = response.header("Content-disposition");
|
||||
if (content == null || content.length() == 0) {
|
||||
throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build());
|
||||
}
|
||||
|
||||
Pattern p = Pattern.compile(".*filename=\"(.*)\"");
|
||||
Matcher m = p.matcher(content);
|
||||
if (m.matches()) {
|
||||
return m.group(1);
|
||||
}
|
||||
throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package me.chanjar.weixin.common.util.http.jodd;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import jodd.http.HttpConnectionProvider;
|
||||
import jodd.http.HttpRequest;
|
||||
import jodd.http.HttpResponse;
|
||||
import jodd.http.ProxyInfo;
|
||||
import me.chanjar.weixin.common.bean.result.WxError;
|
||||
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
|
||||
/**
|
||||
* Created by ecoolper on 2017/5/5.
|
||||
*/
|
||||
public class JoddMediaUploadRequestExecutor extends MediaUploadRequestExecutor<HttpConnectionProvider, ProxyInfo> {
|
||||
public JoddMediaUploadRequestExecutor(RequestHttp requestHttp) {
|
||||
super(requestHttp);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMediaUploadResult execute(String uri, File file) throws WxErrorException, IOException {
|
||||
HttpRequest request = HttpRequest.post(uri);
|
||||
if (requestHttp.getRequestHttpProxy() != null) {
|
||||
requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy());
|
||||
}
|
||||
request.withConnectionProvider(requestHttp.getRequestHttpClient());
|
||||
request.form("media", file);
|
||||
HttpResponse response = request.send();
|
||||
String responseContent = response.bodyText();
|
||||
WxError error = WxError.fromJson(responseContent);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
}
|
||||
return WxMediaUploadResult.fromJson(responseContent);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package me.chanjar.weixin.common.util.http.jodd;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
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;
|
||||
|
||||
import jodd.http.HttpConnectionProvider;
|
||||
import jodd.http.HttpRequest;
|
||||
import jodd.http.HttpResponse;
|
||||
import jodd.http.ProxyInfo;
|
||||
import me.chanjar.weixin.common.bean.result.WxError;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
|
||||
|
||||
/**
|
||||
* Created by ecoolper on 2017/5/4.
|
||||
*/
|
||||
public class JoddSimpleGetRequestExecutor extends SimpleGetRequestExecutor<HttpConnectionProvider, ProxyInfo> {
|
||||
|
||||
public JoddSimpleGetRequestExecutor(RequestHttp requestHttp) {
|
||||
super(requestHttp);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String execute(String uri, String queryParam) throws WxErrorException, IOException {
|
||||
if (queryParam != null) {
|
||||
if (uri.indexOf('?') == -1) {
|
||||
uri += '?';
|
||||
}
|
||||
uri += uri.endsWith("?") ? queryParam : '&' + queryParam;
|
||||
}
|
||||
|
||||
HttpRequest request = HttpRequest.get(uri);
|
||||
if (requestHttp.getRequestHttpProxy() != null) {
|
||||
requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy());
|
||||
}
|
||||
request.withConnectionProvider(requestHttp.getRequestHttpClient());
|
||||
HttpResponse response = request.send();
|
||||
String responseContent = response.bodyText();
|
||||
WxError error = WxError.fromJson(responseContent);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
}
|
||||
return responseContent;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package me.chanjar.weixin.common.util.http.jodd;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import jodd.http.HttpConnectionProvider;
|
||||
import jodd.http.HttpRequest;
|
||||
import jodd.http.HttpResponse;
|
||||
import jodd.http.ProxyInfo;
|
||||
import me.chanjar.weixin.common.bean.result.WxError;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
|
||||
|
||||
/**
|
||||
* Created by ecoolper on 2017/5/4.
|
||||
*/
|
||||
public class JoddSimplePostRequestExecutor extends SimplePostRequestExecutor<HttpConnectionProvider, ProxyInfo> {
|
||||
|
||||
public JoddSimplePostRequestExecutor(RequestHttp requestHttp) {
|
||||
super(requestHttp);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String execute(String uri, String postEntity) throws WxErrorException, IOException {
|
||||
HttpConnectionProvider provider = requestHttp.getRequestHttpClient();
|
||||
ProxyInfo proxyInfo = requestHttp.getRequestHttpProxy();
|
||||
|
||||
HttpRequest request = HttpRequest.post(uri);
|
||||
if (proxyInfo != null) {
|
||||
provider.useProxy(proxyInfo);
|
||||
}
|
||||
request.withConnectionProvider(provider);
|
||||
if (postEntity != null) {
|
||||
request.bodyText(postEntity);
|
||||
}
|
||||
HttpResponse response = request.send();
|
||||
|
||||
String responseContent = response.bodyText();
|
||||
if (responseContent.isEmpty()) {
|
||||
throw new WxErrorException(
|
||||
WxError.newBuilder().setErrorCode(9999).setErrorMsg("无响应内容")
|
||||
.build());
|
||||
}
|
||||
|
||||
if (responseContent.startsWith("<xml>")) {
|
||||
//xml格式输出直接返回
|
||||
return responseContent;
|
||||
}
|
||||
|
||||
WxError error = WxError.fromJson(responseContent);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
}
|
||||
return responseContent;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
package me.chanjar.weixin.common.util.http.okhttp;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import me.chanjar.weixin.common.bean.result.WxError;
|
||||
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.fs.FileUtils;
|
||||
import me.chanjar.weixin.common.util.http.MediaDownloadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import okhttp3.*;
|
||||
|
||||
/**
|
||||
* Created by ecoolper on 2017/5/5.
|
||||
*/
|
||||
public class OkMediaDownloadRequestExecutor extends MediaDownloadRequestExecutor<ConnectionPool, OkhttpProxyInfo> {
|
||||
|
||||
|
||||
public OkMediaDownloadRequestExecutor(RequestHttp requestHttp, File tmpDirFile) {
|
||||
super(requestHttp, tmpDirFile);
|
||||
}
|
||||
|
||||
@Override
|
||||
public File execute(String uri, String queryParam) throws WxErrorException, IOException {
|
||||
if (queryParam != null) {
|
||||
if (uri.indexOf('?') == -1) {
|
||||
uri += '?';
|
||||
}
|
||||
uri += uri.endsWith("?") ? queryParam : '&' + queryParam;
|
||||
}
|
||||
|
||||
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().connectionPool(requestHttp.getRequestHttpClient());
|
||||
//设置代理
|
||||
if (requestHttp.getRequestHttpProxy() != null) {
|
||||
clientBuilder.proxy(requestHttp.getRequestHttpProxy().getProxy());
|
||||
}
|
||||
//设置授权
|
||||
clientBuilder.authenticator(new Authenticator() {
|
||||
@Override
|
||||
public Request authenticate(Route route, Response response) throws IOException {
|
||||
String credential = Credentials.basic(requestHttp.getRequestHttpProxy().getProxyUsername(), requestHttp.getRequestHttpProxy().getProxyPassword());
|
||||
return response.request().newBuilder()
|
||||
.header("Authorization", credential)
|
||||
.build();
|
||||
}
|
||||
});
|
||||
//得到httpClient
|
||||
OkHttpClient client = clientBuilder.build();
|
||||
|
||||
Request request = new Request.Builder().url(uri).get().build();
|
||||
|
||||
Response response = client.newCall(request).execute();
|
||||
|
||||
String contentType = response.header("Content-Type");
|
||||
if (contentType != null && contentType.startsWith("application/json")) {
|
||||
// application/json; encoding=utf-8 下载媒体文件出错
|
||||
throw new WxErrorException(WxError.fromJson(response.body().string()));
|
||||
}
|
||||
|
||||
String fileName = getFileName(response);
|
||||
if (StringUtils.isBlank(fileName)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
InputStream inputStream = new ByteArrayInputStream(response.body().bytes());
|
||||
String[] nameAndExt = fileName.split("\\.");
|
||||
return FileUtils.createTmpFile(inputStream, nameAndExt[0], nameAndExt[1], super.tmpDirFile);
|
||||
}
|
||||
|
||||
private String getFileName(Response response) throws WxErrorException {
|
||||
String content = response.header("Content-disposition");
|
||||
if (content == null || content.length() == 0) {
|
||||
throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build());
|
||||
}
|
||||
|
||||
Pattern p = Pattern.compile(".*filename=\"(.*)\"");
|
||||
Matcher m = p.matcher(content);
|
||||
if (m.matches()) {
|
||||
return m.group(1);
|
||||
}
|
||||
throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package me.chanjar.weixin.common.util.http.okhttp;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import me.chanjar.weixin.common.bean.result.WxError;
|
||||
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import okhttp3.*;
|
||||
|
||||
/**
|
||||
* Created by ecoolper on 2017/5/5.
|
||||
*/
|
||||
public class OkMediaUploadRequestExecutor extends MediaUploadRequestExecutor<ConnectionPool, OkhttpProxyInfo> {
|
||||
|
||||
public OkMediaUploadRequestExecutor(RequestHttp requestHttp) {
|
||||
super(requestHttp);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMediaUploadResult execute(String uri, File file) throws WxErrorException, IOException {
|
||||
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().connectionPool(requestHttp.getRequestHttpClient());
|
||||
//设置代理
|
||||
if (requestHttp.getRequestHttpProxy() != null) {
|
||||
clientBuilder.proxy(requestHttp.getRequestHttpProxy().getProxy());
|
||||
}
|
||||
//设置授权
|
||||
clientBuilder.authenticator(new Authenticator() {
|
||||
@Override
|
||||
public Request authenticate(Route route, Response response) throws IOException {
|
||||
String credential = Credentials.basic(requestHttp.getRequestHttpProxy().getProxyUsername(), requestHttp.getRequestHttpProxy().getProxyPassword());
|
||||
return response.request().newBuilder()
|
||||
.header("Authorization", credential)
|
||||
.build();
|
||||
}
|
||||
});
|
||||
//得到httpClient
|
||||
OkHttpClient client = clientBuilder.build();
|
||||
|
||||
RequestBody fileBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
|
||||
RequestBody body = new MultipartBody.Builder().addFormDataPart("media", null, fileBody).build();
|
||||
Request request = new Request.Builder().url(uri).post(body).build();
|
||||
|
||||
Response response = client.newCall(request).execute();
|
||||
String responseContent = response.body().string();
|
||||
WxError error = WxError.fromJson(responseContent);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
}
|
||||
return WxMediaUploadResult.fromJson(responseContent);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package me.chanjar.weixin.common.util.http.okhttp;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import jodd.http.HttpConnectionProvider;
|
||||
import jodd.http.HttpRequest;
|
||||
import jodd.http.HttpResponse;
|
||||
import jodd.http.ProxyInfo;
|
||||
import me.chanjar.weixin.common.bean.result.WxError;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
|
||||
import okhttp3.*;
|
||||
|
||||
/**
|
||||
* Created by ecoolper on 2017/5/4.
|
||||
*/
|
||||
public class OkSimpleGetRequestExecutor extends SimpleGetRequestExecutor<ConnectionPool, OkhttpProxyInfo> {
|
||||
|
||||
public OkSimpleGetRequestExecutor(RequestHttp requestHttp) {
|
||||
super(requestHttp);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String execute(String uri, String queryParam) throws WxErrorException, IOException {
|
||||
if (queryParam != null) {
|
||||
if (uri.indexOf('?') == -1) {
|
||||
uri += '?';
|
||||
}
|
||||
uri += uri.endsWith("?") ? queryParam : '&' + queryParam;
|
||||
}
|
||||
|
||||
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().connectionPool(requestHttp.getRequestHttpClient());
|
||||
//设置代理
|
||||
if (requestHttp.getRequestHttpProxy() != null) {
|
||||
clientBuilder.proxy(requestHttp.getRequestHttpProxy().getProxy());
|
||||
}
|
||||
//设置授权
|
||||
clientBuilder.authenticator(new Authenticator() {
|
||||
@Override
|
||||
public Request authenticate(Route route, Response response) throws IOException {
|
||||
String credential = Credentials.basic(requestHttp.getRequestHttpProxy().getProxyUsername(), requestHttp.getRequestHttpProxy().getProxyPassword());
|
||||
return response.request().newBuilder()
|
||||
.header("Authorization", credential)
|
||||
.build();
|
||||
}
|
||||
});
|
||||
//得到httpClient
|
||||
OkHttpClient client =clientBuilder.build();
|
||||
|
||||
Request request = new Request.Builder().url(uri).build();
|
||||
|
||||
Response response = client.newCall(request).execute();
|
||||
String responseContent = response.body().string();
|
||||
WxError error = WxError.fromJson(responseContent);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
}
|
||||
return responseContent;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package me.chanjar.weixin.common.util.http.okhttp;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import me.chanjar.weixin.common.bean.result.WxError;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
|
||||
import okhttp3.*;
|
||||
|
||||
/**
|
||||
* Created by ecoolper on 2017/5/4.
|
||||
*/
|
||||
public class OkSimplePostRequestExecutor extends SimplePostRequestExecutor<ConnectionPool,OkhttpProxyInfo>{
|
||||
|
||||
public OkSimplePostRequestExecutor(RequestHttp requestHttp) {
|
||||
super(requestHttp);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String execute(String uri, String postEntity) throws WxErrorException, IOException {
|
||||
ConnectionPool pool = requestHttp.getRequestHttpClient();
|
||||
final OkhttpProxyInfo proxyInfo = requestHttp.getRequestHttpProxy();
|
||||
|
||||
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().connectionPool(pool);
|
||||
//设置代理
|
||||
if (proxyInfo != null) {
|
||||
clientBuilder.proxy(proxyInfo.getProxy());
|
||||
}
|
||||
//设置授权
|
||||
clientBuilder.authenticator(new Authenticator() {
|
||||
@Override
|
||||
public Request authenticate(Route route, Response response) throws IOException {
|
||||
String credential = Credentials.basic(proxyInfo.getProxyUsername(), proxyInfo.getProxyPassword());
|
||||
return response.request().newBuilder()
|
||||
.header("Authorization", credential)
|
||||
.build();
|
||||
}
|
||||
});
|
||||
//得到httpClient
|
||||
OkHttpClient client = clientBuilder.build();
|
||||
|
||||
|
||||
MediaType mediaType = MediaType.parse("text/plain; charset=utf-8");
|
||||
RequestBody body = RequestBody.create(mediaType, postEntity);
|
||||
|
||||
Request request = new Request.Builder().url(uri).post(body).build();
|
||||
|
||||
Response response = client.newCall(request).execute();
|
||||
String responseContent = response.body().string();
|
||||
WxError error = WxError.fromJson(responseContent);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
}
|
||||
return responseContent;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user