mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2026-03-10 00:13:40 +08:00
1、提取了公共代码,添加AbstractWxMPService、AbstractWxCPService类
2、实现了okhttp请求方式 3、RequestExecute接口添加executeApache、executeJodd、executeOkhttp方法
This commit is contained in:
@@ -6,10 +6,14 @@ 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.AbstractRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkhttpProxyInfo;
|
||||
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
|
||||
import okhttp3.*;
|
||||
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
@@ -21,7 +25,7 @@ import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class MaterialDeleteRequestExecutor implements RequestExecutor<Boolean, String> {
|
||||
public class MaterialDeleteRequestExecutor extends AbstractRequestExecutor<Boolean, String> {
|
||||
|
||||
|
||||
public MaterialDeleteRequestExecutor() {
|
||||
@@ -29,44 +33,8 @@ public class MaterialDeleteRequestExecutor implements RequestExecutor<Boolean, S
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean execute(RequestHttp requestHttp, String uri, String materialId) throws WxErrorException, IOException {
|
||||
if (requestHttp.getRequestHttpClient() instanceof CloseableHttpClient) {
|
||||
CloseableHttpClient httpClient = (CloseableHttpClient) requestHttp.getRequestHttpClient();
|
||||
HttpHost httpProxy = (HttpHost) requestHttp.getRequestHttpProxy();
|
||||
return executeApache(httpClient, httpProxy, uri, materialId);
|
||||
}
|
||||
if (requestHttp.getRequestHttpClient() instanceof HttpConnectionProvider) {
|
||||
HttpConnectionProvider provider = (HttpConnectionProvider) requestHttp.getRequestHttpClient();
|
||||
ProxyInfo proxyInfo = (ProxyInfo) requestHttp.getRequestHttpProxy();
|
||||
return executeJodd(provider, proxyInfo, uri, materialId);
|
||||
} else {
|
||||
//这里需要抛出异常,需要优化
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
private Boolean executeJodd(HttpConnectionProvider provider, ProxyInfo proxyInfo, String uri, String materialId) throws WxErrorException, IOException {
|
||||
HttpRequest request = HttpRequest.post(uri);
|
||||
if (proxyInfo != null) {
|
||||
provider.useProxy(proxyInfo);
|
||||
}
|
||||
request.withConnectionProvider(provider);
|
||||
|
||||
request.query("media_id", materialId);
|
||||
HttpResponse response = request.send();
|
||||
String responseContent = response.bodyText();
|
||||
WxError error = WxError.fromJson(responseContent);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
private Boolean executeApache(CloseableHttpClient httpclient, HttpHost httpProxy, String uri,
|
||||
String materialId) throws WxErrorException, IOException {
|
||||
public Boolean executeApache(CloseableHttpClient httpclient, HttpHost httpProxy, String uri,
|
||||
String materialId) throws WxErrorException, IOException {
|
||||
HttpPost httpPost = new HttpPost(uri);
|
||||
if (httpProxy != null) {
|
||||
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
|
||||
@@ -89,5 +57,56 @@ public class MaterialDeleteRequestExecutor implements RequestExecutor<Boolean, S
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean executeJodd(HttpConnectionProvider provider, ProxyInfo proxyInfo, String uri, String materialId) throws WxErrorException, IOException {
|
||||
HttpRequest request = HttpRequest.post(uri);
|
||||
if (proxyInfo != null) {
|
||||
provider.useProxy(proxyInfo);
|
||||
}
|
||||
request.withConnectionProvider(provider);
|
||||
|
||||
request.query("media_id", materialId);
|
||||
HttpResponse response = request.send();
|
||||
String responseContent = response.bodyText();
|
||||
WxError error = WxError.fromJson(responseContent);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean executeOkhttp(ConnectionPool pool, final OkhttpProxyInfo proxyInfo, String uri, String materialId) 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 requestBody = new FormBody.Builder().add("media_id", materialId).build();
|
||||
Request request = new Request.Builder().url(uri).post(requestBody).build();
|
||||
Response response = client.newCall(request).execute();
|
||||
String responseContent = response.body().toString();
|
||||
WxError error = WxError.fromJson(responseContent);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -6,12 +6,16 @@ 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.AbstractRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkhttpProxyInfo;
|
||||
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
|
||||
import me.chanjar.weixin.mp.bean.material.WxMpMaterialNews;
|
||||
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
|
||||
import okhttp3.*;
|
||||
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
@@ -23,30 +27,13 @@ import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class MaterialNewsInfoRequestExecutor implements RequestExecutor<WxMpMaterialNews, String> {
|
||||
public class MaterialNewsInfoRequestExecutor extends AbstractRequestExecutor<WxMpMaterialNews, String> {
|
||||
|
||||
public MaterialNewsInfoRequestExecutor() {
|
||||
super();
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpMaterialNews execute(RequestHttp requestHttp, String uri,
|
||||
String materialId) throws WxErrorException, IOException {
|
||||
if (requestHttp.getRequestHttpClient() instanceof CloseableHttpClient) {
|
||||
CloseableHttpClient httpClient = (CloseableHttpClient) requestHttp.getRequestHttpClient();
|
||||
HttpHost httpProxy = (HttpHost) requestHttp.getRequestHttpProxy();
|
||||
return executeApache(httpClient, httpProxy, uri, materialId);
|
||||
}
|
||||
if (requestHttp.getRequestHttpClient() instanceof HttpConnectionProvider) {
|
||||
HttpConnectionProvider provider = (HttpConnectionProvider) requestHttp.getRequestHttpClient();
|
||||
ProxyInfo proxyInfo = (ProxyInfo) requestHttp.getRequestHttpProxy();
|
||||
return executeJodd(provider, proxyInfo, uri, materialId);
|
||||
} else {
|
||||
//这里需要抛出异常,需要优化
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public WxMpMaterialNews executeApache(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String materialId) throws WxErrorException, IOException {
|
||||
HttpPost httpPost = new HttpPost(uri);
|
||||
if (httpProxy != null) {
|
||||
@@ -70,7 +57,7 @@ public class MaterialNewsInfoRequestExecutor implements RequestExecutor<WxMpMate
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public WxMpMaterialNews executeJodd(HttpConnectionProvider httpclient, ProxyInfo httpProxy, String uri, String materialId) throws WxErrorException, IOException {
|
||||
HttpRequest request = HttpRequest.post(uri);
|
||||
if (httpProxy != null) {
|
||||
@@ -81,7 +68,40 @@ public class MaterialNewsInfoRequestExecutor implements RequestExecutor<WxMpMate
|
||||
request.query("media_id", materialId);
|
||||
HttpResponse response = request.send();
|
||||
|
||||
String responseContent = request.bodyText();
|
||||
String responseContent = response.bodyText();
|
||||
WxError error = WxError.fromJson(responseContent);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
} else {
|
||||
return WxMpGsonBuilder.create().fromJson(responseContent, WxMpMaterialNews.class);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpMaterialNews executeOkhttp(ConnectionPool pool, final OkhttpProxyInfo proxyInfo, String uri, String materialId) 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 requestBody = new FormBody.Builder().add("media_id", materialId).build();
|
||||
Request request = new Request.Builder().url(uri).post(requestBody).build();
|
||||
|
||||
Response response = client.newCall(request).execute();
|
||||
String responseContent = response.body().toString();
|
||||
WxError error = WxError.fromJson(responseContent);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
|
||||
@@ -6,12 +6,16 @@ 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.AbstractRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkhttpProxyInfo;
|
||||
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
|
||||
import me.chanjar.weixin.mp.bean.material.WxMpMaterial;
|
||||
import me.chanjar.weixin.mp.bean.material.WxMpMaterialUploadResult;
|
||||
import okhttp3.*;
|
||||
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
@@ -26,26 +30,10 @@ import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
public class MaterialUploadRequestExecutor implements RequestExecutor<WxMpMaterialUploadResult, WxMpMaterial> {
|
||||
public class MaterialUploadRequestExecutor extends AbstractRequestExecutor<WxMpMaterialUploadResult, WxMpMaterial> {
|
||||
|
||||
@Override
|
||||
public WxMpMaterialUploadResult execute(RequestHttp requestHttp, String uri, WxMpMaterial material) throws WxErrorException, IOException {
|
||||
if (requestHttp.getRequestHttpClient() instanceof CloseableHttpClient) {
|
||||
CloseableHttpClient httpClient = (CloseableHttpClient) requestHttp.getRequestHttpClient();
|
||||
HttpHost httpProxy = (HttpHost) requestHttp.getRequestHttpProxy();
|
||||
return executeApache(httpClient, httpProxy, uri, material);
|
||||
}
|
||||
if (requestHttp.getRequestHttpClient() instanceof HttpConnectionProvider) {
|
||||
HttpConnectionProvider provider = (HttpConnectionProvider) requestHttp.getRequestHttpClient();
|
||||
ProxyInfo proxyInfo = (ProxyInfo) requestHttp.getRequestHttpProxy();
|
||||
return executeJodd(provider, proxyInfo, uri, material);
|
||||
} else {
|
||||
//这里需要抛出异常,需要优化
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private WxMpMaterialUploadResult executeJodd(HttpConnectionProvider provider, ProxyInfo httpProxy, String uri, WxMpMaterial material) throws WxErrorException, IOException {
|
||||
public WxMpMaterialUploadResult executeJodd(HttpConnectionProvider provider, ProxyInfo httpProxy, String uri, WxMpMaterial material) throws WxErrorException, IOException {
|
||||
HttpRequest request = HttpRequest.post(uri);
|
||||
if (httpProxy != null) {
|
||||
provider.useProxy(httpProxy);
|
||||
@@ -76,8 +64,56 @@ public class MaterialUploadRequestExecutor implements RequestExecutor<WxMpMateri
|
||||
}
|
||||
}
|
||||
|
||||
private WxMpMaterialUploadResult executeApache(CloseableHttpClient httpclient, HttpHost httpProxy, String uri,
|
||||
WxMpMaterial material) throws WxErrorException, IOException {
|
||||
@Override
|
||||
public WxMpMaterialUploadResult executeOkhttp(ConnectionPool pool, final OkhttpProxyInfo proxyInfo, String uri, WxMpMaterial material) 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();
|
||||
|
||||
|
||||
if (material == null) {
|
||||
throw new WxErrorException(WxError.newBuilder().setErrorMsg("非法请求,material参数为空").build());
|
||||
}
|
||||
|
||||
File file = material.getFile();
|
||||
if (file == null || !file.exists()) {
|
||||
throw new FileNotFoundException();
|
||||
}
|
||||
RequestBody fileBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
|
||||
MultipartBody.Builder bodyBuilder = new MultipartBody.Builder().addFormDataPart("media", null, fileBody);
|
||||
Map<String, String> form = material.getForm();
|
||||
if (material.getForm() != null) {
|
||||
bodyBuilder.addFormDataPart("description", WxGsonBuilder.create().toJson(form));
|
||||
}
|
||||
RequestBody body =bodyBuilder.build();
|
||||
Request request = new Request.Builder().url(uri).post(body).build();
|
||||
Response response = client.newCall(request).execute();
|
||||
String responseContent = response.body().toString();
|
||||
WxError error = WxError.fromJson(responseContent);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
} else {
|
||||
return WxMpMaterialUploadResult.fromJson(responseContent);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpMaterialUploadResult executeApache(CloseableHttpClient httpclient, HttpHost httpProxy, String uri,
|
||||
WxMpMaterial material) throws WxErrorException, IOException {
|
||||
HttpPost httpPost = new HttpPost(uri);
|
||||
if (httpProxy != null) {
|
||||
RequestConfig response = RequestConfig.custom().setProxy(httpProxy).build();
|
||||
|
||||
@@ -6,11 +6,16 @@ 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.AbstractRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkhttpProxyInfo;
|
||||
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
|
||||
import me.chanjar.weixin.mp.bean.material.WxMediaImgUploadResult;
|
||||
import me.chanjar.weixin.mp.bean.material.WxMpMaterialVideoInfoResult;
|
||||
import okhttp3.*;
|
||||
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
@@ -22,31 +27,14 @@ import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class MaterialVideoInfoRequestExecutor implements RequestExecutor<WxMpMaterialVideoInfoResult, String> {
|
||||
public class MaterialVideoInfoRequestExecutor extends AbstractRequestExecutor<WxMpMaterialVideoInfoResult, String> {
|
||||
|
||||
public MaterialVideoInfoRequestExecutor() {
|
||||
super();
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpMaterialVideoInfoResult execute(RequestHttp requestHttp, String uri, String materialId) throws WxErrorException, IOException {
|
||||
if (requestHttp.getRequestHttpClient() instanceof CloseableHttpClient) {
|
||||
CloseableHttpClient httpClient = (CloseableHttpClient) requestHttp.getRequestHttpClient();
|
||||
HttpHost httpProxy = (HttpHost) requestHttp.getRequestHttpProxy();
|
||||
return executeApache(httpClient, httpProxy, uri, materialId);
|
||||
}
|
||||
if (requestHttp.getRequestHttpClient() instanceof HttpConnectionProvider) {
|
||||
HttpConnectionProvider provider = (HttpConnectionProvider) requestHttp.getRequestHttpClient();
|
||||
ProxyInfo proxyInfo = (ProxyInfo) requestHttp.getRequestHttpProxy();
|
||||
return executeJodd(provider, proxyInfo, uri, materialId);
|
||||
} else {
|
||||
//这里需要抛出异常,需要优化
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private WxMpMaterialVideoInfoResult executeJodd(HttpConnectionProvider provider, ProxyInfo proxyInfo, String uri, String materialId) throws WxErrorException, IOException {
|
||||
public WxMpMaterialVideoInfoResult executeJodd(HttpConnectionProvider provider, ProxyInfo proxyInfo, String uri, String materialId) throws WxErrorException, IOException {
|
||||
HttpRequest request = HttpRequest.post(uri);
|
||||
if (proxyInfo != null) {
|
||||
provider.useProxy(proxyInfo);
|
||||
@@ -64,8 +52,41 @@ public class MaterialVideoInfoRequestExecutor implements RequestExecutor<WxMpMat
|
||||
}
|
||||
}
|
||||
|
||||
private WxMpMaterialVideoInfoResult executeApache(CloseableHttpClient httpclient, HttpHost httpProxy, String uri,
|
||||
String materialId) throws WxErrorException, IOException {
|
||||
@Override
|
||||
public WxMpMaterialVideoInfoResult executeOkhttp(ConnectionPool pool, final OkhttpProxyInfo proxyInfo, String uri, String materialId) 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 requestBody =new FormBody.Builder().add("media_id", materialId).build();
|
||||
Request request = new Request.Builder().url(uri).post(requestBody).build();
|
||||
Response response = client.newCall(request).execute();
|
||||
String responseContent = response.body().toString();
|
||||
WxError error = WxError.fromJson(responseContent);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
} else {
|
||||
return WxMpMaterialVideoInfoResult.fromJson(responseContent);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpMaterialVideoInfoResult executeApache(CloseableHttpClient httpclient, HttpHost httpProxy, String uri,
|
||||
String materialId) throws WxErrorException, IOException {
|
||||
HttpPost httpPost = new HttpPost(uri);
|
||||
if (httpProxy != null) {
|
||||
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
|
||||
|
||||
@@ -6,10 +6,15 @@ 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.AbstractRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import me.chanjar.weixin.common.util.http.apache.InputStreamResponseHandler;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkhttpProxyInfo;
|
||||
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
|
||||
import me.chanjar.weixin.mp.bean.material.WxMediaImgUploadResult;
|
||||
import okhttp3.*;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
@@ -25,8 +30,7 @@ import java.io.InputStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class MaterialVoiceAndImageDownloadRequestExecutor implements RequestExecutor<InputStream, String> {
|
||||
|
||||
public class MaterialVoiceAndImageDownloadRequestExecutor extends AbstractRequestExecutor<InputStream, String> {
|
||||
|
||||
public MaterialVoiceAndImageDownloadRequestExecutor() {
|
||||
super();
|
||||
@@ -36,24 +40,9 @@ public class MaterialVoiceAndImageDownloadRequestExecutor implements RequestExec
|
||||
super();
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream execute(RequestHttp requestHttp, String uri, String materialId) throws WxErrorException, IOException {
|
||||
if (requestHttp.getRequestHttpClient() instanceof CloseableHttpClient) {
|
||||
CloseableHttpClient httpClient = (CloseableHttpClient) requestHttp.getRequestHttpClient();
|
||||
HttpHost httpProxy = (HttpHost) requestHttp.getRequestHttpProxy();
|
||||
return executeApache(httpClient, httpProxy, uri, materialId);
|
||||
}
|
||||
if (requestHttp.getRequestHttpClient() instanceof HttpConnectionProvider) {
|
||||
HttpConnectionProvider provider = (HttpConnectionProvider) requestHttp.getRequestHttpClient();
|
||||
ProxyInfo proxyInfo = (ProxyInfo) requestHttp.getRequestHttpProxy();
|
||||
return executeJodd(provider, proxyInfo, uri, materialId);
|
||||
} else {
|
||||
//这里需要抛出异常,需要优化
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private InputStream executeJodd(HttpConnectionProvider provider, ProxyInfo proxyInfo, String uri, String materialId) throws WxErrorException, IOException {
|
||||
@Override
|
||||
public InputStream executeJodd(HttpConnectionProvider provider, ProxyInfo proxyInfo, String uri, String materialId) throws WxErrorException, IOException {
|
||||
HttpRequest request = HttpRequest.post(uri);
|
||||
if (proxyInfo != null) {
|
||||
provider.useProxy(proxyInfo);
|
||||
@@ -81,8 +70,51 @@ public class MaterialVoiceAndImageDownloadRequestExecutor implements RequestExec
|
||||
|
||||
}
|
||||
|
||||
private InputStream executeApache(CloseableHttpClient httpclient, HttpHost httpProxy, String uri,
|
||||
String materialId) throws WxErrorException, IOException {
|
||||
@Override
|
||||
public InputStream executeOkhttp(ConnectionPool pool, final OkhttpProxyInfo proxyInfo, String uri, String materialId) 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 requestBody = new FormBody.Builder().add("media_id", materialId).build();
|
||||
Request request = new Request.Builder().url(uri).get().post(requestBody).build();
|
||||
Response response = client.newCall(request).execute();
|
||||
|
||||
try (InputStream inputStream = new ByteArrayInputStream(response.body().bytes())) {
|
||||
// 下载媒体文件出错
|
||||
byte[] responseContent = IOUtils.toByteArray(inputStream);
|
||||
String responseContentString = new String(responseContent, "UTF-8");
|
||||
if (responseContentString.length() < 100) {
|
||||
try {
|
||||
WxError wxError = WxGsonBuilder.create().fromJson(responseContentString, WxError.class);
|
||||
if (wxError.getErrorCode() != 0) {
|
||||
throw new WxErrorException(wxError);
|
||||
}
|
||||
} catch (com.google.gson.JsonSyntaxException ex) {
|
||||
return new ByteArrayInputStream(responseContent);
|
||||
}
|
||||
}
|
||||
return new ByteArrayInputStream(responseContent);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream executeApache(CloseableHttpClient httpclient, HttpHost httpProxy, String uri,
|
||||
String materialId) throws WxErrorException, IOException {
|
||||
HttpPost httpPost = new HttpPost(uri);
|
||||
if (httpProxy != null) {
|
||||
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
|
||||
|
||||
@@ -4,12 +4,19 @@ import jodd.http.HttpConnectionProvider;
|
||||
import jodd.http.HttpRequest;
|
||||
import jodd.http.HttpResponse;
|
||||
import jodd.http.ProxyInfo;
|
||||
import jodd.util.MimeTypes;
|
||||
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.AbstractRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkhttpProxyInfo;
|
||||
import me.chanjar.weixin.mp.bean.material.WxMediaImgUploadResult;
|
||||
import okhttp3.*;
|
||||
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
@@ -20,34 +27,20 @@ import org.apache.http.entity.mime.HttpMultipartMode;
|
||||
import org.apache.http.entity.mime.MultipartEntityBuilder;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* @author miller
|
||||
*/
|
||||
public class MediaImgUploadRequestExecutor implements RequestExecutor<WxMediaImgUploadResult, File> {
|
||||
public class MediaImgUploadRequestExecutor extends AbstractRequestExecutor<WxMediaImgUploadResult, File> {
|
||||
|
||||
|
||||
@Override
|
||||
public WxMediaImgUploadResult execute(RequestHttp requestHttp, String uri, File data) throws WxErrorException, IOException {
|
||||
if (requestHttp.getRequestHttpClient() instanceof CloseableHttpClient) {
|
||||
CloseableHttpClient httpClient = (CloseableHttpClient) requestHttp.getRequestHttpClient();
|
||||
HttpHost httpProxy = (HttpHost) requestHttp.getRequestHttpProxy();
|
||||
return executeApache(httpClient, httpProxy, uri, data);
|
||||
}
|
||||
if (requestHttp.getRequestHttpClient() instanceof HttpConnectionProvider) {
|
||||
HttpConnectionProvider provider = (HttpConnectionProvider) requestHttp.getRequestHttpClient();
|
||||
ProxyInfo proxyInfo = (ProxyInfo) requestHttp.getRequestHttpProxy();
|
||||
return executeJodd(provider, proxyInfo, uri, data);
|
||||
} else {
|
||||
//这里需要抛出异常,需要优化
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
private WxMediaImgUploadResult executeJodd(HttpConnectionProvider provider, ProxyInfo proxyInfo, String uri, File data) throws WxErrorException, IOException {
|
||||
public WxMediaImgUploadResult executeJodd(HttpConnectionProvider provider, ProxyInfo proxyInfo, String uri, File data) throws WxErrorException, IOException {
|
||||
if (data == null) {
|
||||
throw new WxErrorException(WxError.newBuilder().setErrorMsg("文件对象为空").build());
|
||||
}
|
||||
@@ -69,8 +62,43 @@ public class MediaImgUploadRequestExecutor implements RequestExecutor<WxMediaImg
|
||||
return WxMediaImgUploadResult.fromJson(responseContent);
|
||||
}
|
||||
|
||||
private WxMediaImgUploadResult executeApache(CloseableHttpClient httpclient, HttpHost httpProxy, String uri,
|
||||
File data) throws WxErrorException, IOException {
|
||||
@Override
|
||||
public WxMediaImgUploadResult executeOkhttp(ConnectionPool pool, final OkhttpProxyInfo proxyInfo, String uri, File data) 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"), data);
|
||||
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().toString();
|
||||
WxError error = WxError.fromJson(responseContent);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
}
|
||||
|
||||
return WxMediaImgUploadResult.fromJson(responseContent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMediaImgUploadResult executeApache(CloseableHttpClient httpclient, HttpHost httpProxy, String uri,
|
||||
File data) throws WxErrorException, IOException {
|
||||
if (data == null) {
|
||||
throw new WxErrorException(WxError.newBuilder().setErrorMsg("文件对象为空").build());
|
||||
}
|
||||
|
||||
@@ -8,11 +8,14 @@ import jodd.util.MimeTypes;
|
||||
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.RequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.AbstractRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
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.okhttp.OkhttpProxyInfo;
|
||||
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
|
||||
import okhttp3.*;
|
||||
|
||||
import org.apache.http.Header;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
@@ -33,27 +36,10 @@ import java.util.UUID;
|
||||
*
|
||||
* @author chanjarster
|
||||
*/
|
||||
public class QrCodeRequestExecutor implements RequestExecutor<File, WxMpQrCodeTicket> {
|
||||
public class QrCodeRequestExecutor extends AbstractRequestExecutor<File, WxMpQrCodeTicket> {
|
||||
|
||||
@Override
|
||||
public File execute(RequestHttp requestHttp, String uri,
|
||||
WxMpQrCodeTicket ticket) throws WxErrorException, IOException {
|
||||
if (requestHttp.getRequestHttpClient() instanceof CloseableHttpClient) {
|
||||
CloseableHttpClient httpClient = (CloseableHttpClient) requestHttp.getRequestHttpClient();
|
||||
HttpHost httpProxy = (HttpHost) requestHttp.getRequestHttpProxy();
|
||||
return executeApache(httpClient, httpProxy, uri, ticket);
|
||||
}
|
||||
if (requestHttp.getRequestHttpClient() instanceof HttpConnectionProvider) {
|
||||
HttpConnectionProvider provider = (HttpConnectionProvider) requestHttp.getRequestHttpClient();
|
||||
ProxyInfo proxyInfo = (ProxyInfo) requestHttp.getRequestHttpProxy();
|
||||
return executeJodd(provider, proxyInfo, uri, ticket);
|
||||
} else {
|
||||
//这里需要抛出异常,需要优化
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private File executeJodd(HttpConnectionProvider provider, ProxyInfo proxyInfo, String uri, WxMpQrCodeTicket ticket) throws WxErrorException, IOException {
|
||||
@Override
|
||||
public File executeJodd(HttpConnectionProvider provider, ProxyInfo proxyInfo, String uri, WxMpQrCodeTicket ticket) throws WxErrorException, IOException {
|
||||
if (ticket != null) {
|
||||
if (uri.indexOf('?') == -1) {
|
||||
uri += '?';
|
||||
@@ -80,8 +66,41 @@ public class QrCodeRequestExecutor implements RequestExecutor<File, WxMpQrCodeTi
|
||||
}
|
||||
}
|
||||
|
||||
private File executeApache(CloseableHttpClient httpclient, HttpHost httpProxy, String uri,
|
||||
WxMpQrCodeTicket ticket) throws WxErrorException, IOException {
|
||||
@Override
|
||||
public File executeOkhttp(ConnectionPool pool, final OkhttpProxyInfo proxyInfo, String uri, WxMpQrCodeTicket data) 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();
|
||||
|
||||
Request request = new Request.Builder().url(uri).get().build();
|
||||
Response response = client.newCall(request).execute();
|
||||
String contentTypeHeader = response.header("Content-Type");
|
||||
if (MimeTypes.MIME_TEXT_PLAIN.equals(contentTypeHeader)) {
|
||||
String responseContent = response.body().toString();
|
||||
throw new WxErrorException(WxError.fromJson(responseContent));
|
||||
}
|
||||
try (InputStream inputStream = new ByteArrayInputStream(response.body().bytes())) {
|
||||
return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public File executeApache(CloseableHttpClient httpclient, HttpHost httpProxy, String uri,
|
||||
WxMpQrCodeTicket ticket) throws WxErrorException, IOException {
|
||||
if (ticket != null) {
|
||||
if (uri.indexOf('?') == -1) {
|
||||
uri += '?';
|
||||
@@ -113,4 +132,5 @@ public class QrCodeRequestExecutor implements RequestExecutor<File, WxMpQrCodeTi
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user