🎨 添加 Apache HttpComponents Client 5.x 为可选的 http client
This commit is contained in:
@@ -114,7 +114,7 @@ public class WxMpCardServiceImpl implements WxMpCardService {
|
||||
param.addProperty("code", code);
|
||||
param.addProperty("check_consume", checkConsume);
|
||||
String responseContent = this.wxMpService.post(WxMpApiUrl.Card.CARD_CODE_GET, param.toString());
|
||||
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
|
||||
JsonElement tmpJsonElement = JsonParser.parseString(responseContent);
|
||||
return WxMpGsonBuilder.create().fromJson(tmpJsonElement,
|
||||
new TypeToken<WxMpCardResult>() {
|
||||
}.getType());
|
||||
@@ -145,7 +145,7 @@ public class WxMpCardServiceImpl implements WxMpCardService {
|
||||
param.addProperty("openid", openId);
|
||||
param.addProperty("is_mark", isMark);
|
||||
String responseContent = this.getWxMpService().post(WxMpApiUrl.Card.CARD_CODE_MARK, param.toString());
|
||||
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
|
||||
JsonElement tmpJsonElement = JsonParser.parseString(responseContent);
|
||||
WxMpCardResult cardResult = WxMpGsonBuilder.create().fromJson(tmpJsonElement,
|
||||
new TypeToken<WxMpCardResult>() {
|
||||
}.getType());
|
||||
|
||||
@@ -217,7 +217,7 @@ public class WxMpMemberCardServiceImpl implements WxMpMemberCardService {
|
||||
|
||||
String responseContent = this.getWxMpService().post(WxMpApiUrl.MemberCard.MEMBER_CARD_USER_INFO_GET, jsonObject.toString());
|
||||
log.debug("{}", responseContent);
|
||||
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
|
||||
JsonElement tmpJsonElement = JsonParser.parseString(responseContent);
|
||||
return WxMpGsonBuilder.create().fromJson(tmpJsonElement,
|
||||
new TypeToken<WxMpMemberCardUserInfoResult>() {
|
||||
}.getType());
|
||||
@@ -229,7 +229,7 @@ public class WxMpMemberCardServiceImpl implements WxMpMemberCardService {
|
||||
|
||||
String responseContent = this.getWxMpService().post(WxMpApiUrl.MemberCard.MEMBER_CARD_UPDATE_USER, GSON.toJson(updateUserMessage));
|
||||
|
||||
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
|
||||
JsonElement tmpJsonElement = JsonParser.parseString(responseContent);
|
||||
return WxMpGsonBuilder.create().fromJson(tmpJsonElement,
|
||||
new TypeToken<WxMpMemberCardUpdateResult>() {
|
||||
}.getType());
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
package me.chanjar.weixin.mp.api.impl;
|
||||
|
||||
import me.chanjar.weixin.common.util.http.HttpClientType;
|
||||
import me.chanjar.weixin.common.util.http.hc.BasicResponseHandler;
|
||||
import me.chanjar.weixin.common.util.http.hc.DefaultHttpComponentsClientBuilder;
|
||||
import me.chanjar.weixin.common.util.http.hc.HttpComponentsClientBuilder;
|
||||
import me.chanjar.weixin.mp.bean.WxMpStableAccessTokenRequest;
|
||||
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
|
||||
import org.apache.hc.client5.http.classic.methods.HttpGet;
|
||||
import org.apache.hc.client5.http.classic.methods.HttpPost;
|
||||
import org.apache.hc.client5.http.config.RequestConfig;
|
||||
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
|
||||
import org.apache.hc.core5.http.ContentType;
|
||||
import org.apache.hc.core5.http.HttpHost;
|
||||
import org.apache.hc.core5.http.io.entity.StringEntity;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static me.chanjar.weixin.mp.enums.WxMpApiUrl.Other.GET_ACCESS_TOKEN_URL;
|
||||
import static me.chanjar.weixin.mp.enums.WxMpApiUrl.Other.GET_STABLE_ACCESS_TOKEN_URL;
|
||||
|
||||
/**
|
||||
* apache http client方式实现.
|
||||
*
|
||||
* @author altusea
|
||||
*/
|
||||
public class WxMpServiceHttpComponentsImpl extends BaseWxMpServiceImpl<CloseableHttpClient, HttpHost> {
|
||||
private CloseableHttpClient httpClient;
|
||||
private HttpHost httpProxy;
|
||||
|
||||
@Override
|
||||
public CloseableHttpClient getRequestHttpClient() {
|
||||
return httpClient;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpHost getRequestHttpProxy() {
|
||||
return httpProxy;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpClientType getRequestType() {
|
||||
return HttpClientType.HTTP_COMPONENTS;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initHttp() {
|
||||
WxMpConfigStorage configStorage = this.getWxMpConfigStorage();
|
||||
HttpComponentsClientBuilder apacheHttpClientBuilder = DefaultHttpComponentsClientBuilder.get();
|
||||
|
||||
apacheHttpClientBuilder.httpProxyHost(configStorage.getHttpProxyHost())
|
||||
.httpProxyPort(configStorage.getHttpProxyPort())
|
||||
.httpProxyUsername(configStorage.getHttpProxyUsername())
|
||||
.httpProxyPassword(configStorage.getHttpProxyPassword().toCharArray());
|
||||
|
||||
if (configStorage.getHttpProxyHost() != null && configStorage.getHttpProxyPort() > 0) {
|
||||
this.httpProxy = new HttpHost(configStorage.getHttpProxyHost(), configStorage.getHttpProxyPort());
|
||||
}
|
||||
|
||||
this.httpClient = apacheHttpClientBuilder.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String doGetAccessTokenRequest() throws IOException {
|
||||
String url = String.format(GET_ACCESS_TOKEN_URL.getUrl(getWxMpConfigStorage()), getWxMpConfigStorage().getAppId(), getWxMpConfigStorage().getSecret());
|
||||
|
||||
HttpGet httpGet = new HttpGet(url);
|
||||
if (this.getRequestHttpProxy() != null) {
|
||||
RequestConfig config = RequestConfig.custom().setProxy(this.getRequestHttpProxy()).build();
|
||||
httpGet.setConfig(config);
|
||||
}
|
||||
return getRequestHttpClient().execute(httpGet, BasicResponseHandler.INSTANCE);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String doGetStableAccessTokenRequest(boolean forceRefresh) throws IOException {
|
||||
String url = GET_STABLE_ACCESS_TOKEN_URL.getUrl(getWxMpConfigStorage());
|
||||
|
||||
HttpPost httpPost = new HttpPost(url);
|
||||
if (this.getRequestHttpProxy() != null) {
|
||||
RequestConfig config = RequestConfig.custom().setProxy(this.getRequestHttpProxy()).build();
|
||||
httpPost.setConfig(config);
|
||||
}
|
||||
WxMpStableAccessTokenRequest wxMaAccessTokenRequest = new WxMpStableAccessTokenRequest();
|
||||
wxMaAccessTokenRequest.setAppid(this.getWxMpConfigStorage().getAppId());
|
||||
wxMaAccessTokenRequest.setSecret(this.getWxMpConfigStorage().getSecret());
|
||||
wxMaAccessTokenRequest.setGrantType("client_credential");
|
||||
wxMaAccessTokenRequest.setForceRefresh(forceRefresh);
|
||||
|
||||
httpPost.setEntity(new StringEntity(wxMaAccessTokenRequest.toJson(), ContentType.APPLICATION_JSON));
|
||||
return getRequestHttpClient().execute(httpPost, BasicResponseHandler.INSTANCE);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package me.chanjar.weixin.mp.util.requestexecuter.material;
|
||||
|
||||
import me.chanjar.weixin.common.enums.WxType;
|
||||
import me.chanjar.weixin.common.error.WxError;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import me.chanjar.weixin.common.util.http.hc.Utf8ResponseHandler;
|
||||
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
|
||||
import org.apache.hc.client5.http.classic.methods.HttpPost;
|
||||
import org.apache.hc.client5.http.config.RequestConfig;
|
||||
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
|
||||
import org.apache.hc.core5.http.HttpHost;
|
||||
import org.apache.hc.core5.http.io.entity.StringEntity;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class MaterialDeleteHttpComponentsRequestExecutor extends MaterialDeleteRequestExecutor<CloseableHttpClient, HttpHost> {
|
||||
public MaterialDeleteHttpComponentsRequestExecutor(RequestHttp<CloseableHttpClient, HttpHost> requestHttp) {
|
||||
super(requestHttp);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean execute(String uri, String materialId, WxType wxType) throws WxErrorException, IOException {
|
||||
HttpPost httpPost = new HttpPost(uri);
|
||||
if (requestHttp.getRequestHttpProxy() != null) {
|
||||
RequestConfig config = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build();
|
||||
httpPost.setConfig(config);
|
||||
}
|
||||
|
||||
Map<String, String> params = new HashMap<>();
|
||||
params.put("media_id", materialId);
|
||||
httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params)));
|
||||
String responseContent = requestHttp.getRequestHttpClient().execute(httpPost, Utf8ResponseHandler.INSTANCE);
|
||||
WxError error = WxError.fromJson(responseContent, WxType.MP);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,5 @@
|
||||
package me.chanjar.weixin.mp.util.requestexecuter.material;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import jodd.http.HttpConnectionProvider;
|
||||
import jodd.http.ProxyInfo;
|
||||
import me.chanjar.weixin.common.enums.WxType;
|
||||
@@ -11,8 +9,8 @@ import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import me.chanjar.weixin.common.util.http.ResponseHandler;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
|
||||
import okhttp3.OkHttpClient;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public abstract class MaterialDeleteRequestExecutor<H, P> implements RequestExecutor<Boolean, String> {
|
||||
protected RequestHttp<H, P> requestHttp;
|
||||
@@ -30,13 +28,17 @@ public abstract class MaterialDeleteRequestExecutor<H, P> implements RequestExec
|
||||
public static RequestExecutor<Boolean, String> create(RequestHttp<?, ?> requestHttp) {
|
||||
switch (requestHttp.getRequestType()) {
|
||||
case APACHE_HTTP:
|
||||
return new MaterialDeleteApacheHttpRequestExecutor((RequestHttp<CloseableHttpClient, HttpHost>) requestHttp);
|
||||
return new MaterialDeleteApacheHttpRequestExecutor(
|
||||
(RequestHttp<org.apache.http.impl.client.CloseableHttpClient, org.apache.http.HttpHost>) requestHttp);
|
||||
case JODD_HTTP:
|
||||
return new MaterialDeleteJoddHttpRequestExecutor((RequestHttp<HttpConnectionProvider, ProxyInfo>) requestHttp);
|
||||
case OK_HTTP:
|
||||
return new MaterialDeleteOkhttpRequestExecutor((RequestHttp<OkHttpClient, OkHttpProxyInfo>) requestHttp);
|
||||
case HTTP_COMPONENTS:
|
||||
return new MaterialDeleteHttpComponentsRequestExecutor(
|
||||
(RequestHttp<org.apache.hc.client5.http.impl.classic.CloseableHttpClient, org.apache.hc.core5.http.HttpHost>) requestHttp);
|
||||
default:
|
||||
return null;
|
||||
throw new IllegalArgumentException("不支持的http执行器类型:" + requestHttp.getRequestType());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package me.chanjar.weixin.mp.util.requestexecuter.material;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.chanjar.weixin.common.enums.WxType;
|
||||
import me.chanjar.weixin.common.error.WxError;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import me.chanjar.weixin.common.util.http.hc.Utf8ResponseHandler;
|
||||
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 org.apache.hc.client5.http.classic.methods.HttpPost;
|
||||
import org.apache.hc.client5.http.config.RequestConfig;
|
||||
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
|
||||
import org.apache.hc.core5.http.HttpHost;
|
||||
import org.apache.hc.core5.http.io.entity.StringEntity;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@Slf4j
|
||||
public class MaterialNewsInfoHttpComponentsRequestExecutor extends MaterialNewsInfoRequestExecutor<CloseableHttpClient, HttpHost> {
|
||||
|
||||
public MaterialNewsInfoHttpComponentsRequestExecutor(RequestHttp<CloseableHttpClient, HttpHost> requestHttp) {
|
||||
super(requestHttp);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpMaterialNews execute(String uri, String materialId, WxType wxType) throws WxErrorException, IOException {
|
||||
HttpPost httpPost = new HttpPost(uri);
|
||||
if (requestHttp.getRequestHttpProxy() != null) {
|
||||
RequestConfig config = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build();
|
||||
httpPost.setConfig(config);
|
||||
}
|
||||
|
||||
httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(ImmutableMap.of("media_id", materialId))));
|
||||
String responseContent = requestHttp.getRequestHttpClient().execute(httpPost, Utf8ResponseHandler.INSTANCE);
|
||||
log.debug("响应原始数据:{}", responseContent);
|
||||
WxError error = WxError.fromJson(responseContent, WxType.MP);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
} else {
|
||||
return WxMpGsonBuilder.create().fromJson(responseContent, WxMpMaterialNews.class);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,5 @@
|
||||
package me.chanjar.weixin.mp.util.requestexecuter.material;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import jodd.http.HttpConnectionProvider;
|
||||
import jodd.http.ProxyInfo;
|
||||
import me.chanjar.weixin.common.enums.WxType;
|
||||
@@ -12,8 +10,8 @@ import me.chanjar.weixin.common.util.http.ResponseHandler;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
|
||||
import me.chanjar.weixin.mp.bean.material.WxMpMaterialNews;
|
||||
import okhttp3.OkHttpClient;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public abstract class MaterialNewsInfoRequestExecutor<H, P> implements RequestExecutor<WxMpMaterialNews, String> {
|
||||
protected RequestHttp<H, P> requestHttp;
|
||||
@@ -31,14 +29,17 @@ public abstract class MaterialNewsInfoRequestExecutor<H, P> implements RequestEx
|
||||
public static RequestExecutor<WxMpMaterialNews, String> create(RequestHttp<?, ?> requestHttp) {
|
||||
switch (requestHttp.getRequestType()) {
|
||||
case APACHE_HTTP:
|
||||
return new MaterialNewsInfoApacheHttpRequestExecutor((RequestHttp<CloseableHttpClient, HttpHost>) requestHttp);
|
||||
return new MaterialNewsInfoApacheHttpRequestExecutor(
|
||||
(RequestHttp<org.apache.http.impl.client.CloseableHttpClient, org.apache.http.HttpHost>) requestHttp);
|
||||
case JODD_HTTP:
|
||||
return new MaterialNewsInfoJoddHttpRequestExecutor((RequestHttp<HttpConnectionProvider, ProxyInfo>) requestHttp);
|
||||
case OK_HTTP:
|
||||
return new MaterialNewsInfoOkhttpRequestExecutor((RequestHttp<OkHttpClient, OkHttpProxyInfo>) requestHttp);
|
||||
case HTTP_COMPONENTS:
|
||||
return new MaterialNewsInfoHttpComponentsRequestExecutor(
|
||||
(RequestHttp<org.apache.hc.client5.http.impl.classic.CloseableHttpClient, org.apache.hc.core5.http.HttpHost>) requestHttp);
|
||||
default:
|
||||
//TODO 需要优化抛出异常
|
||||
return null;
|
||||
throw new IllegalArgumentException("不支持的http执行器类型:" + requestHttp.getRequestType());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,6 @@ import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
|
||||
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
|
||||
import me.chanjar.weixin.mp.bean.material.WxMpMaterial;
|
||||
import me.chanjar.weixin.mp.bean.material.WxMpMaterialUploadResult;
|
||||
import org.apache.http.Consts;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
@@ -22,6 +21,7 @@ import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@@ -56,7 +56,7 @@ public class MaterialUploadApacheHttpRequestExecutor extends MaterialUploadReque
|
||||
Map<String, String> form = material.getForm();
|
||||
if (material.getForm() != null) {
|
||||
multipartEntityBuilder.addPart("description",
|
||||
new StringBody(WxGsonBuilder.create().toJson(form), ContentType.create("text/plain", Consts.UTF_8)));
|
||||
new StringBody(WxGsonBuilder.create().toJson(form), ContentType.TEXT_PLAIN.withCharset(StandardCharsets.UTF_8)));
|
||||
}
|
||||
httpPost.setEntity(multipartEntityBuilder.build());
|
||||
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
package me.chanjar.weixin.mp.util.requestexecuter.material;
|
||||
|
||||
import me.chanjar.weixin.common.enums.WxType;
|
||||
import me.chanjar.weixin.common.error.WxError;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import me.chanjar.weixin.common.util.http.hc.Utf8ResponseHandler;
|
||||
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 org.apache.hc.client5.http.classic.methods.HttpPost;
|
||||
import org.apache.hc.client5.http.config.RequestConfig;
|
||||
import org.apache.hc.client5.http.entity.mime.HttpMultipartMode;
|
||||
import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder;
|
||||
import org.apache.hc.client5.http.entity.mime.StringBody;
|
||||
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
|
||||
import org.apache.hc.core5.http.ContentType;
|
||||
import org.apache.hc.core5.http.HttpHost;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
|
||||
public class MaterialUploadHttpComponentsRequestExecutor extends MaterialUploadRequestExecutor<CloseableHttpClient, HttpHost> {
|
||||
public MaterialUploadHttpComponentsRequestExecutor(RequestHttp<CloseableHttpClient, HttpHost> requestHttp) {
|
||||
super(requestHttp);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpMaterialUploadResult execute(String uri, WxMpMaterial material, WxType wxType) throws WxErrorException, IOException {
|
||||
HttpPost httpPost = new HttpPost(uri);
|
||||
if (requestHttp.getRequestHttpProxy() != null) {
|
||||
RequestConfig response = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build();
|
||||
httpPost.setConfig(response);
|
||||
}
|
||||
|
||||
if (material == null) {
|
||||
throw new WxErrorException("非法请求,material参数为空");
|
||||
}
|
||||
|
||||
File file = material.getFile();
|
||||
if (file == null || !file.exists()) {
|
||||
throw new FileNotFoundException();
|
||||
}
|
||||
|
||||
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
|
||||
multipartEntityBuilder
|
||||
.addBinaryBody("media", file)
|
||||
.setMode(HttpMultipartMode.EXTENDED);
|
||||
Map<String, String> form = material.getForm();
|
||||
if (material.getForm() != null) {
|
||||
multipartEntityBuilder.addPart("description",
|
||||
new StringBody(WxGsonBuilder.create().toJson(form), ContentType.TEXT_PLAIN.withCharset(StandardCharsets.UTF_8)));
|
||||
}
|
||||
httpPost.setEntity(multipartEntityBuilder.build());
|
||||
|
||||
String responseContent = requestHttp.getRequestHttpClient().execute(httpPost, Utf8ResponseHandler.INSTANCE);
|
||||
WxError error = WxError.fromJson(responseContent, WxType.MP);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
} else {
|
||||
return WxMpMaterialUploadResult.fromJson(responseContent);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,5 @@
|
||||
package me.chanjar.weixin.mp.util.requestexecuter.material;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import jodd.http.HttpConnectionProvider;
|
||||
import jodd.http.ProxyInfo;
|
||||
import me.chanjar.weixin.common.enums.WxType;
|
||||
@@ -13,8 +11,8 @@ import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
|
||||
import me.chanjar.weixin.mp.bean.material.WxMpMaterial;
|
||||
import me.chanjar.weixin.mp.bean.material.WxMpMaterialUploadResult;
|
||||
import okhttp3.OkHttpClient;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @author codepiano
|
||||
@@ -35,13 +33,17 @@ public abstract class MaterialUploadRequestExecutor<H, P> implements RequestExec
|
||||
public static RequestExecutor<WxMpMaterialUploadResult, WxMpMaterial> create(RequestHttp<?, ?> requestHttp) {
|
||||
switch (requestHttp.getRequestType()) {
|
||||
case APACHE_HTTP:
|
||||
return new MaterialUploadApacheHttpRequestExecutor((RequestHttp<CloseableHttpClient, HttpHost>) requestHttp);
|
||||
return new MaterialUploadApacheHttpRequestExecutor(
|
||||
(RequestHttp<org.apache.http.impl.client.CloseableHttpClient, org.apache.http.HttpHost>) requestHttp);
|
||||
case JODD_HTTP:
|
||||
return new MaterialUploadJoddHttpRequestExecutor((RequestHttp<HttpConnectionProvider, ProxyInfo>) requestHttp);
|
||||
case OK_HTTP:
|
||||
return new MaterialUploadOkhttpRequestExecutor((RequestHttp<OkHttpClient, OkHttpProxyInfo>) requestHttp);
|
||||
case HTTP_COMPONENTS:
|
||||
return new MaterialUploadHttpComponentsRequestExecutor(
|
||||
(RequestHttp<org.apache.hc.client5.http.impl.classic.CloseableHttpClient, org.apache.hc.core5.http.HttpHost>) requestHttp);
|
||||
default:
|
||||
return null;
|
||||
throw new IllegalArgumentException("不支持的http执行器类型:" + requestHttp.getRequestType());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package me.chanjar.weixin.mp.util.requestexecuter.material;
|
||||
|
||||
import me.chanjar.weixin.common.enums.WxType;
|
||||
import me.chanjar.weixin.common.error.WxError;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import me.chanjar.weixin.common.util.http.hc.Utf8ResponseHandler;
|
||||
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
|
||||
import me.chanjar.weixin.mp.bean.material.WxMpMaterialVideoInfoResult;
|
||||
import org.apache.hc.client5.http.classic.methods.HttpPost;
|
||||
import org.apache.hc.client5.http.config.RequestConfig;
|
||||
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
|
||||
import org.apache.hc.core5.http.HttpHost;
|
||||
import org.apache.hc.core5.http.io.entity.StringEntity;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class MaterialVideoInfoHttpComponentsRequestExecutor extends MaterialVideoInfoRequestExecutor<CloseableHttpClient, HttpHost> {
|
||||
public MaterialVideoInfoHttpComponentsRequestExecutor(RequestHttp<CloseableHttpClient, HttpHost> requestHttp) {
|
||||
super(requestHttp);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpMaterialVideoInfoResult execute(String uri, String materialId, WxType wxType) throws WxErrorException, IOException {
|
||||
HttpPost httpPost = new HttpPost(uri);
|
||||
if (requestHttp.getRequestHttpProxy() != null) {
|
||||
RequestConfig config = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build();
|
||||
httpPost.setConfig(config);
|
||||
}
|
||||
|
||||
Map<String, String> params = new HashMap<>();
|
||||
params.put("media_id", materialId);
|
||||
httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params)));
|
||||
String responseContent = requestHttp.getRequestHttpClient().execute(httpPost, Utf8ResponseHandler.INSTANCE);
|
||||
WxError error = WxError.fromJson(responseContent, WxType.MP);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
} else {
|
||||
return WxMpMaterialVideoInfoResult.fromJson(responseContent);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,5 @@
|
||||
package me.chanjar.weixin.mp.util.requestexecuter.material;
|
||||
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import jodd.http.HttpConnectionProvider;
|
||||
import jodd.http.ProxyInfo;
|
||||
import me.chanjar.weixin.common.enums.WxType;
|
||||
@@ -13,8 +10,8 @@ import me.chanjar.weixin.common.util.http.ResponseHandler;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
|
||||
import me.chanjar.weixin.mp.bean.material.WxMpMaterialVideoInfoResult;
|
||||
import okhttp3.OkHttpClient;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public abstract class MaterialVideoInfoRequestExecutor<H, P> implements RequestExecutor<WxMpMaterialVideoInfoResult, String> {
|
||||
protected RequestHttp<H, P> requestHttp;
|
||||
@@ -32,13 +29,17 @@ public abstract class MaterialVideoInfoRequestExecutor<H, P> implements RequestE
|
||||
public static RequestExecutor<WxMpMaterialVideoInfoResult, String> create(RequestHttp<?, ?> requestHttp) {
|
||||
switch (requestHttp.getRequestType()) {
|
||||
case APACHE_HTTP:
|
||||
return new MaterialVideoInfoApacheHttpRequestExecutor((RequestHttp<CloseableHttpClient, HttpHost>) requestHttp);
|
||||
return new MaterialVideoInfoApacheHttpRequestExecutor(
|
||||
(RequestHttp<org.apache.http.impl.client.CloseableHttpClient, org.apache.http.HttpHost>) requestHttp);
|
||||
case JODD_HTTP:
|
||||
return new MaterialVideoInfoJoddHttpRequestExecutor((RequestHttp<HttpConnectionProvider, ProxyInfo>) requestHttp);
|
||||
case OK_HTTP:
|
||||
return new MaterialVideoInfoOkhttpRequestExecutor((RequestHttp<OkHttpClient, OkHttpProxyInfo>) requestHttp);
|
||||
case HTTP_COMPONENTS:
|
||||
return new MaterialVideoInfoHttpComponentsRequestExecutor(
|
||||
(RequestHttp<org.apache.hc.client5.http.impl.classic.CloseableHttpClient, org.apache.hc.core5.http.HttpHost>) requestHttp);
|
||||
default:
|
||||
return null;
|
||||
throw new IllegalArgumentException("不支持的http执行器类型:" + requestHttp.getRequestType());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
package me.chanjar.weixin.mp.util.requestexecuter.material;
|
||||
|
||||
import me.chanjar.weixin.common.enums.WxType;
|
||||
import me.chanjar.weixin.common.error.WxError;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import me.chanjar.weixin.common.util.http.hc.InputStreamResponseHandler;
|
||||
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.hc.client5.http.ClientProtocolException;
|
||||
import org.apache.hc.client5.http.classic.methods.HttpPost;
|
||||
import org.apache.hc.client5.http.config.RequestConfig;
|
||||
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
|
||||
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
|
||||
import org.apache.hc.core5.http.HttpException;
|
||||
import org.apache.hc.core5.http.HttpHost;
|
||||
import org.apache.hc.core5.http.io.entity.StringEntity;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class MaterialVoiceAndImageDownloadHttpComponentsRequestExecutor
|
||||
extends MaterialVoiceAndImageDownloadRequestExecutor<CloseableHttpClient, HttpHost> {
|
||||
public MaterialVoiceAndImageDownloadHttpComponentsRequestExecutor(RequestHttp<CloseableHttpClient, HttpHost> requestHttp, File tmpDirFile) {
|
||||
super(requestHttp, tmpDirFile);
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream execute(String uri, String materialId, WxType wxType) throws WxErrorException, IOException {
|
||||
HttpPost httpPost = new HttpPost(uri);
|
||||
if (requestHttp.getRequestHttpProxy() != null) {
|
||||
RequestConfig config = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build();
|
||||
httpPost.setConfig(config);
|
||||
}
|
||||
|
||||
Map<String, String> params = new HashMap<>();
|
||||
params.put("media_id", materialId);
|
||||
httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params)));
|
||||
try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost);
|
||||
InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response)) {
|
||||
// 下载媒体文件出错
|
||||
byte[] responseContent = IOUtils.toByteArray(inputStream);
|
||||
String responseContentString = new String(responseContent, StandardCharsets.UTF_8);
|
||||
if (responseContentString.length() <= 215) {
|
||||
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);
|
||||
} catch (HttpException httpException) {
|
||||
throw new ClientProtocolException(httpException.getMessage(), httpException);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,8 +13,6 @@ import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import me.chanjar.weixin.common.util.http.ResponseHandler;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
|
||||
import okhttp3.OkHttpClient;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
|
||||
public abstract class MaterialVoiceAndImageDownloadRequestExecutor<H, P> implements RequestExecutor<InputStream, String> {
|
||||
protected RequestHttp<H, P> requestHttp;
|
||||
@@ -34,13 +32,17 @@ public abstract class MaterialVoiceAndImageDownloadRequestExecutor<H, P> impleme
|
||||
public static RequestExecutor<InputStream, String> create(RequestHttp<?, ?> requestHttp, File tmpDirFile) {
|
||||
switch (requestHttp.getRequestType()) {
|
||||
case APACHE_HTTP:
|
||||
return new MaterialVoiceAndImageDownloadApacheHttpRequestExecutor((RequestHttp<CloseableHttpClient, HttpHost>) requestHttp, tmpDirFile);
|
||||
return new MaterialVoiceAndImageDownloadApacheHttpRequestExecutor(
|
||||
(RequestHttp<org.apache.http.impl.client.CloseableHttpClient, org.apache.http.HttpHost>) requestHttp, tmpDirFile);
|
||||
case JODD_HTTP:
|
||||
return new MaterialVoiceAndImageDownloadJoddHttpRequestExecutor((RequestHttp<HttpConnectionProvider, ProxyInfo>) requestHttp, tmpDirFile);
|
||||
case OK_HTTP:
|
||||
return new MaterialVoiceAndImageDownloadOkhttpRequestExecutor((RequestHttp<OkHttpClient, OkHttpProxyInfo>) requestHttp, tmpDirFile);
|
||||
case HTTP_COMPONENTS:
|
||||
return new MaterialVoiceAndImageDownloadHttpComponentsRequestExecutor(
|
||||
(RequestHttp<org.apache.hc.client5.http.impl.classic.CloseableHttpClient, org.apache.hc.core5.http.HttpHost>) requestHttp, tmpDirFile);
|
||||
default:
|
||||
return null;
|
||||
throw new IllegalArgumentException("不支持的http执行器类型:" + requestHttp.getRequestType());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
package me.chanjar.weixin.mp.util.requestexecuter.media;
|
||||
|
||||
import me.chanjar.weixin.common.enums.WxType;
|
||||
import me.chanjar.weixin.common.error.WxError;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import me.chanjar.weixin.common.util.http.hc.Utf8ResponseHandler;
|
||||
import me.chanjar.weixin.mp.bean.material.WxMediaImgUploadResult;
|
||||
import org.apache.hc.client5.http.classic.methods.HttpPost;
|
||||
import org.apache.hc.client5.http.config.RequestConfig;
|
||||
import org.apache.hc.client5.http.entity.mime.HttpMultipartMode;
|
||||
import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder;
|
||||
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
|
||||
import org.apache.hc.core5.http.HttpEntity;
|
||||
import org.apache.hc.core5.http.HttpHost;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class MediaImgUploadHttpComponentsRequestExecutor extends MediaImgUploadRequestExecutor<CloseableHttpClient, HttpHost> {
|
||||
public MediaImgUploadHttpComponentsRequestExecutor(RequestHttp<CloseableHttpClient, HttpHost> requestHttp) {
|
||||
super(requestHttp);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMediaImgUploadResult execute(String uri, File data, WxType wxType) throws WxErrorException, IOException {
|
||||
if (data == null) {
|
||||
throw new WxErrorException("文件对象为空");
|
||||
}
|
||||
|
||||
HttpPost httpPost = new HttpPost(uri);
|
||||
if (requestHttp.getRequestHttpProxy() != null) {
|
||||
RequestConfig config = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build();
|
||||
httpPost.setConfig(config);
|
||||
}
|
||||
|
||||
HttpEntity entity = MultipartEntityBuilder
|
||||
.create()
|
||||
.addBinaryBody("media", data)
|
||||
.setMode(HttpMultipartMode.EXTENDED)
|
||||
.build();
|
||||
httpPost.setEntity(entity);
|
||||
|
||||
String responseContent = requestHttp.getRequestHttpClient().execute(httpPost, Utf8ResponseHandler.INSTANCE);
|
||||
WxError error = WxError.fromJson(responseContent, WxType.MP);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
}
|
||||
|
||||
return WxMediaImgUploadResult.fromJson(responseContent);
|
||||
}
|
||||
}
|
||||
@@ -13,8 +13,6 @@ import me.chanjar.weixin.common.util.http.ResponseHandler;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
|
||||
import me.chanjar.weixin.mp.bean.material.WxMediaImgUploadResult;
|
||||
import okhttp3.OkHttpClient;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
|
||||
/**
|
||||
* @author miller
|
||||
@@ -35,13 +33,17 @@ public abstract class MediaImgUploadRequestExecutor<H, P> implements RequestExec
|
||||
public static RequestExecutor<WxMediaImgUploadResult, File> create(RequestHttp<?, ?> requestHttp) {
|
||||
switch (requestHttp.getRequestType()) {
|
||||
case APACHE_HTTP:
|
||||
return new MediaImgUploadApacheHttpRequestExecutor((RequestHttp<CloseableHttpClient, HttpHost>) requestHttp);
|
||||
return new MediaImgUploadApacheHttpRequestExecutor(
|
||||
(RequestHttp<org.apache.http.impl.client.CloseableHttpClient, org.apache.http.HttpHost>) requestHttp);
|
||||
case JODD_HTTP:
|
||||
return new MediaImgUploadHttpRequestExecutor((RequestHttp<HttpConnectionProvider, ProxyInfo>) requestHttp);
|
||||
case OK_HTTP:
|
||||
return new MediaImgUploadOkhttpRequestExecutor((RequestHttp<OkHttpClient, OkHttpProxyInfo>) requestHttp);
|
||||
case HTTP_COMPONENTS:
|
||||
return new MediaImgUploadHttpComponentsRequestExecutor(
|
||||
(RequestHttp<org.apache.hc.client5.http.impl.classic.CloseableHttpClient, org.apache.hc.core5.http.HttpHost>) requestHttp);
|
||||
default:
|
||||
return null;
|
||||
throw new IllegalArgumentException("不支持的http执行器类型:" + requestHttp.getRequestType());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
package me.chanjar.weixin.mp.util.requestexecuter.qrcode;
|
||||
|
||||
import me.chanjar.weixin.common.enums.WxType;
|
||||
import me.chanjar.weixin.common.error.WxError;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.fs.FileUtils;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import me.chanjar.weixin.common.util.http.hc.InputStreamResponseHandler;
|
||||
import me.chanjar.weixin.common.util.http.hc.Utf8ResponseHandler;
|
||||
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
|
||||
import org.apache.hc.client5.http.ClientProtocolException;
|
||||
import org.apache.hc.client5.http.classic.methods.HttpGet;
|
||||
import org.apache.hc.client5.http.config.RequestConfig;
|
||||
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
|
||||
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
|
||||
import org.apache.hc.core5.http.ContentType;
|
||||
import org.apache.hc.core5.http.Header;
|
||||
import org.apache.hc.core5.http.HttpException;
|
||||
import org.apache.hc.core5.http.HttpHost;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* @author altusea
|
||||
*/
|
||||
public class QrCodeHttpComponentsRequestExecutor extends QrCodeRequestExecutor<CloseableHttpClient, HttpHost> {
|
||||
public QrCodeHttpComponentsRequestExecutor(RequestHttp<CloseableHttpClient, HttpHost> requestHttp) {
|
||||
super(requestHttp);
|
||||
}
|
||||
|
||||
@Override
|
||||
public File execute(String uri, WxMpQrCodeTicket ticket, WxType wxType) throws WxErrorException, IOException {
|
||||
if (ticket != null) {
|
||||
if (uri.indexOf('?') == -1) {
|
||||
uri += '?';
|
||||
}
|
||||
uri += uri.endsWith("?")
|
||||
? "ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8")
|
||||
: "&ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8");
|
||||
}
|
||||
|
||||
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 (ContentType.TEXT_PLAIN.getMimeType().equals(ContentType.parse(contentTypeHeader[0].getValue()).getMimeType())) {
|
||||
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
|
||||
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MP));
|
||||
}
|
||||
}
|
||||
return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg");
|
||||
} catch (HttpException httpException) {
|
||||
throw new ClientProtocolException(httpException.getMessage(), httpException);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,8 +13,6 @@ import me.chanjar.weixin.common.util.http.ResponseHandler;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
|
||||
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
|
||||
import okhttp3.OkHttpClient;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
|
||||
/**
|
||||
* 获得QrCode图片 请求执行器.
|
||||
@@ -34,16 +32,20 @@ public abstract class QrCodeRequestExecutor<H, P> implements RequestExecutor<Fil
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static RequestExecutor<File, WxMpQrCodeTicket> create(RequestHttp<?, ?> requestHttp) throws WxErrorException {
|
||||
public static RequestExecutor<File, WxMpQrCodeTicket> create(RequestHttp<?, ?> requestHttp) {
|
||||
switch (requestHttp.getRequestType()) {
|
||||
case APACHE_HTTP:
|
||||
return new QrCodeApacheHttpRequestExecutor((RequestHttp<CloseableHttpClient, HttpHost>) requestHttp);
|
||||
return new QrCodeApacheHttpRequestExecutor(
|
||||
(RequestHttp<org.apache.http.impl.client.CloseableHttpClient, org.apache.http.HttpHost>) requestHttp);
|
||||
case JODD_HTTP:
|
||||
return new QrCodeJoddHttpRequestExecutor((RequestHttp<HttpConnectionProvider, ProxyInfo>) requestHttp);
|
||||
case OK_HTTP:
|
||||
return new QrCodeOkhttpRequestExecutor((RequestHttp<OkHttpClient, OkHttpProxyInfo>) requestHttp);
|
||||
case HTTP_COMPONENTS:
|
||||
return new QrCodeHttpComponentsRequestExecutor(
|
||||
(RequestHttp<org.apache.hc.client5.http.impl.classic.CloseableHttpClient, org.apache.hc.core5.http.HttpHost>) requestHttp);
|
||||
default:
|
||||
throw new WxErrorException("不支持的http框架");
|
||||
throw new IllegalArgumentException("不支持的http执行器类型:" + requestHttp.getRequestType());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
package me.chanjar.weixin.mp.util.requestexecuter.voice;
|
||||
|
||||
import me.chanjar.weixin.common.enums.WxType;
|
||||
import me.chanjar.weixin.common.error.WxError;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import me.chanjar.weixin.common.util.http.hc.Utf8ResponseHandler;
|
||||
import org.apache.hc.client5.http.classic.methods.HttpPost;
|
||||
import org.apache.hc.client5.http.config.RequestConfig;
|
||||
import org.apache.hc.client5.http.entity.mime.HttpMultipartMode;
|
||||
import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder;
|
||||
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
|
||||
import org.apache.hc.core5.http.HttpEntity;
|
||||
import org.apache.hc.core5.http.HttpHost;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class VoiceUploadHttpComponentsRequestExecutor extends VoiceUploadRequestExecutor<CloseableHttpClient, HttpHost> {
|
||||
public VoiceUploadHttpComponentsRequestExecutor(RequestHttp<CloseableHttpClient, HttpHost> requestHttp) {
|
||||
super(requestHttp);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean execute(String uri, File data, WxType wxType) throws WxErrorException, IOException {
|
||||
if (data == null) {
|
||||
throw new WxErrorException("文件对象为空");
|
||||
}
|
||||
|
||||
HttpPost httpPost = new HttpPost(uri);
|
||||
if (requestHttp.getRequestHttpProxy() != null) {
|
||||
RequestConfig config = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build();
|
||||
httpPost.setConfig(config);
|
||||
}
|
||||
|
||||
HttpEntity entity = MultipartEntityBuilder
|
||||
.create()
|
||||
.addBinaryBody("media", data)
|
||||
.setMode(HttpMultipartMode.EXTENDED)
|
||||
.build();
|
||||
httpPost.setEntity(entity);
|
||||
|
||||
String responseContent = requestHttp.getRequestHttpClient().execute(httpPost, Utf8ResponseHandler.INSTANCE);
|
||||
WxError error = WxError.fromJson(responseContent, WxType.MP);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,13 @@
|
||||
package me.chanjar.weixin.mp.util.requestexecuter.voice;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import me.chanjar.weixin.common.enums.WxType;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import me.chanjar.weixin.common.util.http.ResponseHandler;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
@@ -34,11 +32,13 @@ public abstract class VoiceUploadRequestExecutor<H, P> implements RequestExecuto
|
||||
public static RequestExecutor<Boolean, File> create(RequestHttp<?, ?> requestHttp) {
|
||||
switch (requestHttp.getRequestType()) {
|
||||
case APACHE_HTTP:
|
||||
return new VoiceUploadApacheHttpRequestExecutor((RequestHttp<CloseableHttpClient, HttpHost>) requestHttp);
|
||||
case JODD_HTTP:
|
||||
case OK_HTTP:
|
||||
return new VoiceUploadApacheHttpRequestExecutor(
|
||||
(RequestHttp<org.apache.http.impl.client.CloseableHttpClient, org.apache.http.HttpHost>) requestHttp);
|
||||
case HTTP_COMPONENTS:
|
||||
return new VoiceUploadHttpComponentsRequestExecutor(
|
||||
(RequestHttp<org.apache.hc.client5.http.impl.classic.CloseableHttpClient, org.apache.hc.core5.http.HttpHost>) requestHttp);
|
||||
default:
|
||||
return null;
|
||||
throw new IllegalArgumentException("不支持的http执行器类型:" + requestHttp.getRequestType());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user