mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-10-30 18:19:04 +08:00
#293 重构OkHttp的实现代码,同时修复JSApi的bug
* update travis settings * feat(okhttp): 修改okhttp底层调用方法 直接用OkHttpClient代替connect.使客户端单一化.Okhttp 自动管理连接池优化 * feat(log,jsApi): 添加log debug 标记明确下调用底层效果,修复jsAPI Lock 为null 问题 添加log debug 标记明确下调用底层效果,修复jsAPI Lock 为null 问题 #293
This commit is contained in:
@ -6,7 +6,11 @@ import me.chanjar.weixin.common.util.fs.FileUtils;
|
|||||||
import me.chanjar.weixin.common.util.http.MediaDownloadRequestExecutor;
|
import me.chanjar.weixin.common.util.http.MediaDownloadRequestExecutor;
|
||||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||||
import okhttp3.*;
|
import okhttp3.*;
|
||||||
|
import okio.BufferedSink;
|
||||||
|
import okio.Okio;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@ -18,7 +22,8 @@ import java.util.regex.Pattern;
|
|||||||
/**
|
/**
|
||||||
* Created by ecoolper on 2017/5/5.
|
* Created by ecoolper on 2017/5/5.
|
||||||
*/
|
*/
|
||||||
public class OkHttpMediaDownloadRequestExecutor extends MediaDownloadRequestExecutor<ConnectionPool, OkHttpProxyInfo> {
|
public class OkHttpMediaDownloadRequestExecutor extends MediaDownloadRequestExecutor<OkHttpClient, OkHttpProxyInfo> {
|
||||||
|
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||||
|
|
||||||
|
|
||||||
public OkHttpMediaDownloadRequestExecutor(RequestHttp requestHttp, File tmpDirFile) {
|
public OkHttpMediaDownloadRequestExecutor(RequestHttp requestHttp, File tmpDirFile) {
|
||||||
@ -27,6 +32,7 @@ public class OkHttpMediaDownloadRequestExecutor extends MediaDownloadRequestExec
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public File execute(String uri, String queryParam) throws WxErrorException, IOException {
|
public File execute(String uri, String queryParam) throws WxErrorException, IOException {
|
||||||
|
logger.debug("OkHttpMediaDownloadRequestExecutor is running");
|
||||||
if (queryParam != null) {
|
if (queryParam != null) {
|
||||||
if (uri.indexOf('?') == -1) {
|
if (uri.indexOf('?') == -1) {
|
||||||
uri += '?';
|
uri += '?';
|
||||||
@ -34,23 +40,8 @@ public class OkHttpMediaDownloadRequestExecutor extends MediaDownloadRequestExec
|
|||||||
uri += uri.endsWith("?") ? queryParam : '&' + queryParam;
|
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
|
//得到httpClient
|
||||||
OkHttpClient client = clientBuilder.build();
|
OkHttpClient client = requestHttp.getRequestHttpClient();
|
||||||
|
|
||||||
Request request = new Request.Builder().url(uri).get().build();
|
Request request = new Request.Builder().url(uri).get().build();
|
||||||
|
|
||||||
@ -66,10 +57,12 @@ public class OkHttpMediaDownloadRequestExecutor extends MediaDownloadRequestExec
|
|||||||
if (StringUtils.isBlank(fileName)) {
|
if (StringUtils.isBlank(fileName)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
InputStream inputStream = new ByteArrayInputStream(response.body().bytes());
|
|
||||||
String[] nameAndExt = fileName.split("\\.");
|
String[] nameAndExt = fileName.split("\\.");
|
||||||
return FileUtils.createTmpFile(inputStream, nameAndExt[0], nameAndExt[1], super.tmpDirFile);
|
File file = File.createTempFile(nameAndExt[0], nameAndExt[1], super.tmpDirFile);
|
||||||
|
try (BufferedSink sink = Okio.buffer(Okio.sink(file))) {
|
||||||
|
sink.writeAll(response.body().source());
|
||||||
|
}
|
||||||
|
return file;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getFileName(Response response) throws WxErrorException {
|
private String getFileName(Response response) throws WxErrorException {
|
||||||
|
|||||||
@ -6,6 +6,8 @@ import me.chanjar.weixin.common.exception.WxErrorException;
|
|||||||
import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor;
|
import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor;
|
||||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||||
import okhttp3.*;
|
import okhttp3.*;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -13,7 +15,8 @@ import java.io.IOException;
|
|||||||
/**
|
/**
|
||||||
* Created by ecoolper on 2017/5/5.
|
* Created by ecoolper on 2017/5/5.
|
||||||
*/
|
*/
|
||||||
public class OkHttpMediaUploadRequestExecutor extends MediaUploadRequestExecutor<ConnectionPool, OkHttpProxyInfo> {
|
public class OkHttpMediaUploadRequestExecutor extends MediaUploadRequestExecutor<OkHttpClient, OkHttpProxyInfo> {
|
||||||
|
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||||
|
|
||||||
public OkHttpMediaUploadRequestExecutor(RequestHttp requestHttp) {
|
public OkHttpMediaUploadRequestExecutor(RequestHttp requestHttp) {
|
||||||
super(requestHttp);
|
super(requestHttp);
|
||||||
@ -21,23 +24,9 @@ public class OkHttpMediaUploadRequestExecutor extends MediaUploadRequestExecutor
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public WxMediaUploadResult execute(String uri, File file) throws WxErrorException, IOException {
|
public WxMediaUploadResult execute(String uri, File file) throws WxErrorException, IOException {
|
||||||
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().connectionPool(requestHttp.getRequestHttpClient());
|
logger.debug("OkHttpMediaUploadRequestExecutor is running");
|
||||||
//设置代理
|
|
||||||
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
|
//得到httpClient
|
||||||
OkHttpClient client = clientBuilder.build();
|
OkHttpClient client = requestHttp.getRequestHttpClient();
|
||||||
|
|
||||||
RequestBody body = new MultipartBody.Builder()
|
RequestBody body = new MultipartBody.Builder()
|
||||||
.setType(MediaType.parse("multipart/form-data"))
|
.setType(MediaType.parse("multipart/form-data"))
|
||||||
|
|||||||
@ -5,13 +5,16 @@ import me.chanjar.weixin.common.exception.WxErrorException;
|
|||||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||||
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
|
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
|
||||||
import okhttp3.*;
|
import okhttp3.*;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by ecoolper on 2017/5/4.
|
* Created by ecoolper on 2017/5/4.
|
||||||
*/
|
*/
|
||||||
public class OkHttpSimpleGetRequestExecutor extends SimpleGetRequestExecutor<ConnectionPool, OkHttpProxyInfo> {
|
public class OkHttpSimpleGetRequestExecutor extends SimpleGetRequestExecutor<OkHttpClient, OkHttpProxyInfo> {
|
||||||
|
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||||
|
|
||||||
public OkHttpSimpleGetRequestExecutor(RequestHttp requestHttp) {
|
public OkHttpSimpleGetRequestExecutor(RequestHttp requestHttp) {
|
||||||
super(requestHttp);
|
super(requestHttp);
|
||||||
@ -19,6 +22,7 @@ public class OkHttpSimpleGetRequestExecutor extends SimpleGetRequestExecutor<Con
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String execute(String uri, String queryParam) throws WxErrorException, IOException {
|
public String execute(String uri, String queryParam) throws WxErrorException, IOException {
|
||||||
|
logger.debug("OkHttpSimpleGetRequestExecutor is running");
|
||||||
if (queryParam != null) {
|
if (queryParam != null) {
|
||||||
if (uri.indexOf('?') == -1) {
|
if (uri.indexOf('?') == -1) {
|
||||||
uri += '?';
|
uri += '?';
|
||||||
@ -26,26 +30,9 @@ public class OkHttpSimpleGetRequestExecutor extends SimpleGetRequestExecutor<Con
|
|||||||
uri += uri.endsWith("?") ? queryParam : '&' + queryParam;
|
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
|
//得到httpClient
|
||||||
OkHttpClient client = clientBuilder.build();
|
OkHttpClient client = requestHttp.getRequestHttpClient();
|
||||||
|
|
||||||
Request request = new Request.Builder().url(uri).build();
|
Request request = new Request.Builder().url(uri).build();
|
||||||
|
|
||||||
Response response = client.newCall(request).execute();
|
Response response = client.newCall(request).execute();
|
||||||
String responseContent = response.body().string();
|
String responseContent = response.body().string();
|
||||||
WxError error = WxError.fromJson(responseContent);
|
WxError error = WxError.fromJson(responseContent);
|
||||||
|
|||||||
@ -5,13 +5,16 @@ import me.chanjar.weixin.common.exception.WxErrorException;
|
|||||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||||
import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
|
import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
|
||||||
import okhttp3.*;
|
import okhttp3.*;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by ecoolper on 2017/5/4.
|
* Created by ecoolper on 2017/5/4.
|
||||||
*/
|
*/
|
||||||
public class OkHttpSimplePostRequestExecutor extends SimplePostRequestExecutor<ConnectionPool, OkHttpProxyInfo> {
|
public class OkHttpSimplePostRequestExecutor extends SimplePostRequestExecutor<OkHttpClient, OkHttpProxyInfo> {
|
||||||
|
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||||
|
|
||||||
public OkHttpSimplePostRequestExecutor(RequestHttp requestHttp) {
|
public OkHttpSimplePostRequestExecutor(RequestHttp requestHttp) {
|
||||||
super(requestHttp);
|
super(requestHttp);
|
||||||
@ -19,27 +22,9 @@ public class OkHttpSimplePostRequestExecutor extends SimplePostRequestExecutor<C
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String execute(String uri, String postEntity) throws WxErrorException, IOException {
|
public String execute(String uri, String postEntity) throws WxErrorException, IOException {
|
||||||
ConnectionPool pool = requestHttp.getRequestHttpClient();
|
logger.debug("OkHttpSimplePostRequestExecutor running");
|
||||||
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
|
//得到httpClient
|
||||||
OkHttpClient client = clientBuilder.build();
|
OkHttpClient client = requestHttp.getRequestHttpClient();
|
||||||
|
|
||||||
|
|
||||||
MediaType mediaType = MediaType.parse("text/plain; charset=utf-8");
|
MediaType mediaType = MediaType.parse("text/plain; charset=utf-8");
|
||||||
RequestBody body = RequestBody.create(mediaType, postEntity);
|
RequestBody body = RequestBody.create(mediaType, postEntity);
|
||||||
|
|||||||
@ -7,16 +7,20 @@ import me.chanjar.weixin.common.util.http.HttpType;
|
|||||||
import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
|
import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
|
||||||
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
|
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
|
||||||
import okhttp3.*;
|
import okhttp3.*;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
public class WxCpServiceOkHttpImpl extends AbstractWxCpServiceImpl<ConnectionPool, OkHttpProxyInfo> {
|
public class WxCpServiceOkHttpImpl extends AbstractWxCpServiceImpl<OkHttpClient, OkHttpProxyInfo> {
|
||||||
protected ConnectionPool httpClient;
|
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||||
|
|
||||||
|
protected OkHttpClient httpClient;
|
||||||
protected OkHttpProxyInfo httpProxy;
|
protected OkHttpProxyInfo httpProxy;
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ConnectionPool getRequestHttpClient() {
|
public OkHttpClient getRequestHttpClient() {
|
||||||
return httpClient;
|
return httpClient;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -32,6 +36,7 @@ public class WxCpServiceOkHttpImpl extends AbstractWxCpServiceImpl<ConnectionPoo
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getAccessToken(boolean forceRefresh) throws WxErrorException {
|
public String getAccessToken(boolean forceRefresh) throws WxErrorException {
|
||||||
|
logger.debug("WxCpServiceOkHttpImpl is running");
|
||||||
if (forceRefresh) {
|
if (forceRefresh) {
|
||||||
this.configStorage.expireAccessToken();
|
this.configStorage.expireAccessToken();
|
||||||
}
|
}
|
||||||
@ -41,24 +46,8 @@ public class WxCpServiceOkHttpImpl extends AbstractWxCpServiceImpl<ConnectionPoo
|
|||||||
String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?"
|
String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?"
|
||||||
+ "&corpid=" + this.configStorage.getCorpId()
|
+ "&corpid=" + this.configStorage.getCorpId()
|
||||||
+ "&corpsecret=" + this.configStorage.getCorpSecret();
|
+ "&corpsecret=" + this.configStorage.getCorpSecret();
|
||||||
|
|
||||||
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().connectionPool(httpClient);
|
|
||||||
//设置代理
|
|
||||||
if (httpProxy != null) {
|
|
||||||
clientBuilder.proxy(getRequestHttpProxy().getProxy());
|
|
||||||
}
|
|
||||||
//设置授权
|
|
||||||
clientBuilder.authenticator(new Authenticator() {
|
|
||||||
@Override
|
|
||||||
public Request authenticate(Route route, Response response) throws IOException {
|
|
||||||
String credential = Credentials.basic(httpProxy.getProxyUsername(), httpProxy.getProxyPassword());
|
|
||||||
return response.request().newBuilder()
|
|
||||||
.header("Authorization", credential)
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
//得到httpClient
|
//得到httpClient
|
||||||
OkHttpClient client = clientBuilder.build();
|
OkHttpClient client = getRequestHttpClient();
|
||||||
//请求的request
|
//请求的request
|
||||||
Request request = new Request.Builder().url(url).get().build();
|
Request request = new Request.Builder().url(url).get().build();
|
||||||
Response response = null;
|
Response response = null;
|
||||||
@ -88,13 +77,24 @@ public class WxCpServiceOkHttpImpl extends AbstractWxCpServiceImpl<ConnectionPoo
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void initHttp() {
|
public void initHttp() {
|
||||||
WxCpConfigStorage configStorage = this.configStorage;
|
logger.debug("WxCpServiceOkHttpImpl initHttp");
|
||||||
|
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
|
||||||
|
//设置代理
|
||||||
|
if (httpProxy != null) {
|
||||||
|
clientBuilder.proxy(getRequestHttpProxy().getProxy());
|
||||||
|
|
||||||
if (configStorage.getHttpProxyHost() != null && configStorage.getHttpProxyPort() > 0) {
|
//设置授权
|
||||||
httpProxy = new OkHttpProxyInfo(OkHttpProxyInfo.ProxyType.SOCKS5, configStorage.getHttpProxyHost(), configStorage.getHttpProxyPort(), configStorage.getHttpProxyUsername(), configStorage.getHttpProxyPassword());
|
clientBuilder.authenticator(new Authenticator() {
|
||||||
|
@Override
|
||||||
|
public Request authenticate(Route route, Response response) throws IOException {
|
||||||
|
String credential = Credentials.basic(httpProxy.getProxyUsername(), httpProxy.getProxyPassword());
|
||||||
|
return response.request().newBuilder()
|
||||||
|
.header("Authorization", credential)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
httpClient = clientBuilder.build();
|
||||||
httpClient = new ConnectionPool();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@ -66,10 +66,9 @@ public abstract class AbstractWxMpServiceImpl<H, P> implements WxMpService, Requ
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getJsapiTicket(boolean forceRefresh) throws WxErrorException {
|
public String getJsapiTicket(boolean forceRefresh) throws WxErrorException {
|
||||||
Lock lock = this.getWxMpConfigStorage().getJsapiTicketLock();
|
Lock lock = this.getWxMpConfigStorage().getAccessTokenLock();
|
||||||
try {
|
try {
|
||||||
lock.lock();
|
lock.lock();
|
||||||
|
|
||||||
if (forceRefresh) {
|
if (forceRefresh) {
|
||||||
this.getWxMpConfigStorage().expireJsapiTicket();
|
this.getWxMpConfigStorage().expireJsapiTicket();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,16 +8,21 @@ import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
|
|||||||
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
|
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
|
||||||
import me.chanjar.weixin.mp.api.WxMpService;
|
import me.chanjar.weixin.mp.api.WxMpService;
|
||||||
import okhttp3.*;
|
import okhttp3.*;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.concurrent.locks.Lock;
|
import java.util.concurrent.locks.Lock;
|
||||||
|
|
||||||
public class WxMpServiceOkHttpImpl extends AbstractWxMpServiceImpl<ConnectionPool, OkHttpProxyInfo> {
|
public class WxMpServiceOkHttpImpl extends AbstractWxMpServiceImpl<OkHttpClient, OkHttpProxyInfo> {
|
||||||
private ConnectionPool httpClient;
|
|
||||||
|
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||||
|
|
||||||
|
private OkHttpClient httpClient;
|
||||||
private OkHttpProxyInfo httpProxy;
|
private OkHttpProxyInfo httpProxy;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ConnectionPool getRequestHttpClient() {
|
public OkHttpClient getRequestHttpClient() {
|
||||||
return httpClient;
|
return httpClient;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -33,6 +38,7 @@ public class WxMpServiceOkHttpImpl extends AbstractWxMpServiceImpl<ConnectionPoo
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getAccessToken(boolean forceRefresh) throws WxErrorException {
|
public String getAccessToken(boolean forceRefresh) throws WxErrorException {
|
||||||
|
logger.debug("WxMpServiceOkHttpImpl is running");
|
||||||
Lock lock = this.getWxMpConfigStorage().getAccessTokenLock();
|
Lock lock = this.getWxMpConfigStorage().getAccessTokenLock();
|
||||||
try {
|
try {
|
||||||
lock.lock();
|
lock.lock();
|
||||||
@ -45,26 +51,8 @@ public class WxMpServiceOkHttpImpl extends AbstractWxMpServiceImpl<ConnectionPoo
|
|||||||
String url = String.format(WxMpService.GET_ACCESS_TOKEN_URL,
|
String url = String.format(WxMpService.GET_ACCESS_TOKEN_URL,
|
||||||
this.getWxMpConfigStorage().getAppId(), this.getWxMpConfigStorage().getSecret());
|
this.getWxMpConfigStorage().getAppId(), this.getWxMpConfigStorage().getSecret());
|
||||||
|
|
||||||
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().connectionPool(httpClient);
|
|
||||||
//设置代理
|
|
||||||
if (httpProxy != null) {
|
|
||||||
clientBuilder.proxy(getRequestHttpProxy().getProxy());
|
|
||||||
}
|
|
||||||
//设置授权
|
|
||||||
clientBuilder.authenticator(new Authenticator() {
|
|
||||||
@Override
|
|
||||||
public Request authenticate(Route route, Response response) throws IOException {
|
|
||||||
String credential = Credentials.basic(httpProxy.getProxyUsername(), httpProxy.getProxyPassword());
|
|
||||||
return response.request().newBuilder()
|
|
||||||
.header("Authorization", credential)
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
//得到httpClient
|
|
||||||
OkHttpClient client = clientBuilder.build();
|
|
||||||
|
|
||||||
Request request = new Request.Builder().url(url).get().build();
|
Request request = new Request.Builder().url(url).get().build();
|
||||||
Response response = client.newCall(request).execute();
|
Response response = getRequestHttpClient().newCall(request).execute();
|
||||||
String resultContent = response.body().string();
|
String resultContent = response.body().string();
|
||||||
WxError error = WxError.fromJson(resultContent);
|
WxError error = WxError.fromJson(resultContent);
|
||||||
if (error.getErrorCode() != 0) {
|
if (error.getErrorCode() != 0) {
|
||||||
@ -84,13 +72,29 @@ public class WxMpServiceOkHttpImpl extends AbstractWxMpServiceImpl<ConnectionPoo
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void initHttp() {
|
public void initHttp() {
|
||||||
|
logger.debug("WxMpServiceOkHttpImpl initHttp");
|
||||||
WxMpConfigStorage configStorage = this.getWxMpConfigStorage();
|
WxMpConfigStorage configStorage = this.getWxMpConfigStorage();
|
||||||
|
|
||||||
if (configStorage.getHttpProxyHost() != null && configStorage.getHttpProxyPort() > 0) {
|
if (configStorage.getHttpProxyHost() != null && configStorage.getHttpProxyPort() > 0) {
|
||||||
httpProxy = new OkHttpProxyInfo(OkHttpProxyInfo.ProxyType.SOCKS5, configStorage.getHttpProxyHost(), configStorage.getHttpProxyPort(), configStorage.getHttpProxyUsername(), configStorage.getHttpProxyPassword());
|
httpProxy = OkHttpProxyInfo.socks5Proxy(configStorage.getHttpProxyHost(), configStorage.getHttpProxyPort(), configStorage.getHttpProxyUsername(), configStorage.getHttpProxyPassword());
|
||||||
}
|
}
|
||||||
|
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
|
||||||
|
//设置代理
|
||||||
|
if (httpProxy != null) {
|
||||||
|
clientBuilder.proxy(getRequestHttpProxy().getProxy());
|
||||||
|
|
||||||
httpClient = new ConnectionPool();
|
//设置授权
|
||||||
|
clientBuilder.authenticator(new Authenticator() {
|
||||||
|
@Override
|
||||||
|
public Request authenticate(Route route, Response response) throws IOException {
|
||||||
|
String credential = Credentials.basic(httpProxy.getProxyUsername(), httpProxy.getProxyPassword());
|
||||||
|
return response.request().newBuilder()
|
||||||
|
.header("Authorization", credential)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
httpClient = clientBuilder.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,13 +6,16 @@ import me.chanjar.weixin.common.util.http.RequestHttp;
|
|||||||
import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
|
import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
|
||||||
import me.chanjar.weixin.mp.util.http.MaterialDeleteRequestExecutor;
|
import me.chanjar.weixin.mp.util.http.MaterialDeleteRequestExecutor;
|
||||||
import okhttp3.*;
|
import okhttp3.*;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by ecoolper on 2017/5/5.
|
* Created by ecoolper on 2017/5/5.
|
||||||
*/
|
*/
|
||||||
public class OkhttpMaterialDeleteRequestExecutor extends MaterialDeleteRequestExecutor<ConnectionPool, OkHttpProxyInfo> {
|
public class OkhttpMaterialDeleteRequestExecutor extends MaterialDeleteRequestExecutor<OkHttpClient, OkHttpProxyInfo> {
|
||||||
|
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||||
|
|
||||||
|
|
||||||
public OkhttpMaterialDeleteRequestExecutor(RequestHttp requestHttp) {
|
public OkhttpMaterialDeleteRequestExecutor(RequestHttp requestHttp) {
|
||||||
@ -21,23 +24,9 @@ public class OkhttpMaterialDeleteRequestExecutor extends MaterialDeleteRequestEx
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Boolean execute(String uri, String materialId) throws WxErrorException, IOException {
|
public Boolean execute(String uri, String materialId) throws WxErrorException, IOException {
|
||||||
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().connectionPool(requestHttp.getRequestHttpClient());
|
logger.debug("OkhttpMaterialDeleteRequestExecutor is running");
|
||||||
//设置代理
|
|
||||||
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
|
//得到httpClient
|
||||||
OkHttpClient client = clientBuilder.build();
|
OkHttpClient client = requestHttp.getRequestHttpClient();
|
||||||
|
|
||||||
RequestBody requestBody = new FormBody.Builder().add("media_id", materialId).build();
|
RequestBody requestBody = new FormBody.Builder().add("media_id", materialId).build();
|
||||||
Request request = new Request.Builder().url(uri).post(requestBody).build();
|
Request request = new Request.Builder().url(uri).post(requestBody).build();
|
||||||
|
|||||||
@ -16,7 +16,7 @@ import java.io.IOException;
|
|||||||
/**
|
/**
|
||||||
* Created by ecoolper on 2017/5/5.
|
* Created by ecoolper on 2017/5/5.
|
||||||
*/
|
*/
|
||||||
public class OkhttpMaterialNewsInfoRequestExecutor extends MaterialNewsInfoRequestExecutor<ConnectionPool, OkHttpProxyInfo> {
|
public class OkhttpMaterialNewsInfoRequestExecutor extends MaterialNewsInfoRequestExecutor<OkHttpClient, OkHttpProxyInfo> {
|
||||||
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||||
public OkhttpMaterialNewsInfoRequestExecutor(RequestHttp requestHttp) {
|
public OkhttpMaterialNewsInfoRequestExecutor(RequestHttp requestHttp) {
|
||||||
super(requestHttp);
|
super(requestHttp);
|
||||||
@ -24,23 +24,9 @@ public class OkhttpMaterialNewsInfoRequestExecutor extends MaterialNewsInfoReque
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public WxMpMaterialNews execute(String uri, String materialId) throws WxErrorException, IOException {
|
public WxMpMaterialNews execute(String uri, String materialId) 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
|
//得到httpClient
|
||||||
OkHttpClient client = clientBuilder.build();
|
OkHttpClient client = requestHttp.getRequestHttpClient();
|
||||||
|
|
||||||
RequestBody requestBody = new FormBody.Builder().add("media_id", materialId).build();
|
RequestBody requestBody = new FormBody.Builder().add("media_id", materialId).build();
|
||||||
Request request = new Request.Builder().url(uri).post(requestBody).build();
|
Request request = new Request.Builder().url(uri).post(requestBody).build();
|
||||||
|
|||||||
@ -9,6 +9,8 @@ import me.chanjar.weixin.mp.bean.material.WxMpMaterial;
|
|||||||
import me.chanjar.weixin.mp.bean.material.WxMpMaterialUploadResult;
|
import me.chanjar.weixin.mp.bean.material.WxMpMaterialUploadResult;
|
||||||
import me.chanjar.weixin.mp.util.http.MaterialUploadRequestExecutor;
|
import me.chanjar.weixin.mp.util.http.MaterialUploadRequestExecutor;
|
||||||
import okhttp3.*;
|
import okhttp3.*;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
@ -18,13 +20,16 @@ import java.util.Map;
|
|||||||
/**
|
/**
|
||||||
* Created by ecoolper on 2017/5/5.
|
* Created by ecoolper on 2017/5/5.
|
||||||
*/
|
*/
|
||||||
public class OkhttpMaterialUploadRequestExecutor extends MaterialUploadRequestExecutor<ConnectionPool, OkHttpProxyInfo> {
|
public class OkhttpMaterialUploadRequestExecutor extends MaterialUploadRequestExecutor<OkHttpClient, OkHttpProxyInfo> {
|
||||||
|
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||||
|
|
||||||
public OkhttpMaterialUploadRequestExecutor(RequestHttp requestHttp) {
|
public OkhttpMaterialUploadRequestExecutor(RequestHttp requestHttp) {
|
||||||
super(requestHttp);
|
super(requestHttp);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public WxMpMaterialUploadResult execute(String uri, WxMpMaterial material) throws WxErrorException, IOException {
|
public WxMpMaterialUploadResult execute(String uri, WxMpMaterial material) throws WxErrorException, IOException {
|
||||||
|
logger.debug("OkhttpMaterialUploadRequestExecutor is running");
|
||||||
if (material == null) {
|
if (material == null) {
|
||||||
throw new WxErrorException(WxError.newBuilder().setErrorMsg("非法请求,material参数为空").build());
|
throw new WxErrorException(WxError.newBuilder().setErrorMsg("非法请求,material参数为空").build());
|
||||||
}
|
}
|
||||||
@ -33,21 +38,9 @@ public class OkhttpMaterialUploadRequestExecutor extends MaterialUploadRequestEx
|
|||||||
throw new FileNotFoundException();
|
throw new FileNotFoundException();
|
||||||
}
|
}
|
||||||
|
|
||||||
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().connectionPool(requestHttp.getRequestHttpClient());
|
//得到httpClient
|
||||||
//设置代理
|
|
||||||
if (requestHttp.getRequestHttpProxy() != null) {
|
OkHttpClient client = requestHttp.getRequestHttpClient();
|
||||||
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();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
MultipartBody.Builder bodyBuilder = new MultipartBody.Builder()
|
MultipartBody.Builder bodyBuilder = new MultipartBody.Builder()
|
||||||
.setType(MediaType.parse("multipart/form-data"))
|
.setType(MediaType.parse("multipart/form-data"))
|
||||||
@ -60,7 +53,7 @@ public class OkhttpMaterialUploadRequestExecutor extends MaterialUploadRequestEx
|
|||||||
}
|
}
|
||||||
|
|
||||||
Request request = new Request.Builder().url(uri).post(bodyBuilder.build()).build();
|
Request request = new Request.Builder().url(uri).post(bodyBuilder.build()).build();
|
||||||
Response response = clientBuilder.build().newCall(request).execute();
|
Response response = client.newCall(request).execute();
|
||||||
String responseContent = response.body().string();
|
String responseContent = response.body().string();
|
||||||
WxError error = WxError.fromJson(responseContent);
|
WxError error = WxError.fromJson(responseContent);
|
||||||
if (error.getErrorCode() != 0) {
|
if (error.getErrorCode() != 0) {
|
||||||
|
|||||||
@ -7,36 +7,26 @@ import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
|
|||||||
import me.chanjar.weixin.mp.bean.material.WxMpMaterialVideoInfoResult;
|
import me.chanjar.weixin.mp.bean.material.WxMpMaterialVideoInfoResult;
|
||||||
import me.chanjar.weixin.mp.util.http.MaterialVideoInfoRequestExecutor;
|
import me.chanjar.weixin.mp.util.http.MaterialVideoInfoRequestExecutor;
|
||||||
import okhttp3.*;
|
import okhttp3.*;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by ecoolper on 2017/5/5.
|
* Created by ecoolper on 2017/5/5.
|
||||||
*/
|
*/
|
||||||
public class OkhttpMaterialVideoInfoRequestExecutor extends MaterialVideoInfoRequestExecutor<ConnectionPool, OkHttpProxyInfo> {
|
public class OkhttpMaterialVideoInfoRequestExecutor extends MaterialVideoInfoRequestExecutor<OkHttpClient, OkHttpProxyInfo> {
|
||||||
|
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||||
|
|
||||||
public OkhttpMaterialVideoInfoRequestExecutor(RequestHttp requestHttp) {
|
public OkhttpMaterialVideoInfoRequestExecutor(RequestHttp requestHttp) {
|
||||||
super(requestHttp);
|
super(requestHttp);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public WxMpMaterialVideoInfoResult execute(String uri, String materialId) throws WxErrorException, IOException {
|
public WxMpMaterialVideoInfoResult execute(String uri, String materialId) throws WxErrorException, IOException {
|
||||||
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().connectionPool(requestHttp.getRequestHttpClient());
|
logger.debug("OkhttpMaterialVideoInfoRequestExecutor is running");
|
||||||
//设置代理
|
|
||||||
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
|
//得到httpClient
|
||||||
OkHttpClient client = clientBuilder.build();
|
OkHttpClient client = requestHttp.getRequestHttpClient();
|
||||||
|
|
||||||
RequestBody requestBody = new FormBody.Builder().add("media_id", materialId).build();
|
RequestBody requestBody = new FormBody.Builder().add("media_id", materialId).build();
|
||||||
Request request = new Request.Builder().url(uri).post(requestBody).build();
|
Request request = new Request.Builder().url(uri).post(requestBody).build();
|
||||||
|
|||||||
@ -7,62 +7,39 @@ import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
|
|||||||
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
|
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
|
||||||
import me.chanjar.weixin.mp.util.http.MaterialVoiceAndImageDownloadRequestExecutor;
|
import me.chanjar.weixin.mp.util.http.MaterialVoiceAndImageDownloadRequestExecutor;
|
||||||
import okhttp3.*;
|
import okhttp3.*;
|
||||||
|
import okio.BufferedSink;
|
||||||
|
import okio.Okio;
|
||||||
import org.apache.commons.io.IOUtils;
|
import org.apache.commons.io.IOUtils;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.*;
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by ecoolper on 2017/5/5.
|
* Created by ecoolper on 2017/5/5.
|
||||||
*/
|
*/
|
||||||
public class OkhttpMaterialVoiceAndImageDownloadRequestExecutor extends MaterialVoiceAndImageDownloadRequestExecutor<ConnectionPool, OkHttpProxyInfo> {
|
public class OkhttpMaterialVoiceAndImageDownloadRequestExecutor extends MaterialVoiceAndImageDownloadRequestExecutor<OkHttpClient, OkHttpProxyInfo> {
|
||||||
|
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||||
|
|
||||||
public OkhttpMaterialVoiceAndImageDownloadRequestExecutor(RequestHttp requestHttp, File tmpDirFile) {
|
public OkhttpMaterialVoiceAndImageDownloadRequestExecutor(RequestHttp requestHttp, File tmpDirFile) {
|
||||||
super(requestHttp, tmpDirFile);
|
super(requestHttp, tmpDirFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public InputStream execute(String uri, String materialId) throws WxErrorException, IOException {
|
public InputStream execute(String uri, String materialId) throws WxErrorException, IOException {
|
||||||
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().connectionPool(requestHttp.getRequestHttpClient());
|
logger.debug("OkhttpMaterialVoiceAndImageDownloadRequestExecutor is running");
|
||||||
//设置代理
|
OkHttpClient client = 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 requestBody = new FormBody.Builder().add("media_id", materialId).build();
|
RequestBody requestBody = new FormBody.Builder().add("media_id", materialId).build();
|
||||||
Request request = new Request.Builder().url(uri).get().post(requestBody).build();
|
Request request = new Request.Builder().url(uri).get().post(requestBody).build();
|
||||||
Response response = client.newCall(request).execute();
|
Response response = client.newCall(request).execute();
|
||||||
|
String contentTypeHeader = response.header("Content-Type");
|
||||||
try (InputStream inputStream = new ByteArrayInputStream(response.body().bytes())) {
|
if ("text/plain".equals(contentTypeHeader)) {
|
||||||
|
String responseContent = response.body().string();
|
||||||
// 下载媒体文件出错
|
throw new WxErrorException(WxError.fromJson(responseContent));
|
||||||
byte[] responseContent = IOUtils.toByteArray(inputStream);
|
}
|
||||||
String responseContentString = new String(responseContent, StandardCharsets.UTF_8);
|
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); BufferedSink sink = Okio.buffer(Okio.sink(outputStream))) {
|
||||||
if (responseContentString.length() < 100) {
|
sink.writeAll(response.body().source());
|
||||||
try {
|
return new ByteArrayInputStream(outputStream.toByteArray());
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,6 +7,8 @@ import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
|
|||||||
import me.chanjar.weixin.mp.bean.material.WxMediaImgUploadResult;
|
import me.chanjar.weixin.mp.bean.material.WxMediaImgUploadResult;
|
||||||
import me.chanjar.weixin.mp.util.http.MediaImgUploadRequestExecutor;
|
import me.chanjar.weixin.mp.util.http.MediaImgUploadRequestExecutor;
|
||||||
import okhttp3.*;
|
import okhttp3.*;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -14,7 +16,8 @@ import java.io.IOException;
|
|||||||
/**
|
/**
|
||||||
* Created by ecoolper on 2017/5/5.
|
* Created by ecoolper on 2017/5/5.
|
||||||
*/
|
*/
|
||||||
public class OkhttpMediaImgUploadRequestExecutor extends MediaImgUploadRequestExecutor<ConnectionPool, OkHttpProxyInfo> {
|
public class OkhttpMediaImgUploadRequestExecutor extends MediaImgUploadRequestExecutor<OkHttpClient, OkHttpProxyInfo> {
|
||||||
|
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||||
|
|
||||||
public OkhttpMediaImgUploadRequestExecutor(RequestHttp requestHttp) {
|
public OkhttpMediaImgUploadRequestExecutor(RequestHttp requestHttp) {
|
||||||
super(requestHttp);
|
super(requestHttp);
|
||||||
@ -22,21 +25,9 @@ public class OkhttpMediaImgUploadRequestExecutor extends MediaImgUploadRequestEx
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public WxMediaImgUploadResult execute(String uri, File file) throws WxErrorException, IOException {
|
public WxMediaImgUploadResult execute(String uri, File file) throws WxErrorException, IOException {
|
||||||
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().connectionPool(requestHttp.getRequestHttpClient());
|
logger.debug("OkhttpMediaImgUploadRequestExecutor is running");
|
||||||
//设置代理
|
//得到httpClient
|
||||||
if (requestHttp.getRequestHttpProxy() != null) {
|
OkHttpClient client = requestHttp.getRequestHttpClient();
|
||||||
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();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
RequestBody body = new MultipartBody.Builder()
|
RequestBody body = new MultipartBody.Builder()
|
||||||
.setType(MediaType.parse("multipart/form-data"))
|
.setType(MediaType.parse("multipart/form-data"))
|
||||||
@ -46,7 +37,7 @@ public class OkhttpMediaImgUploadRequestExecutor extends MediaImgUploadRequestEx
|
|||||||
.build();
|
.build();
|
||||||
|
|
||||||
Request request = new Request.Builder().url(uri).post(body).build();
|
Request request = new Request.Builder().url(uri).post(body).build();
|
||||||
Response response = clientBuilder.build().newCall(request).execute();
|
Response response = client.newCall(request).execute();
|
||||||
String responseContent = response.body().string();
|
String responseContent = response.body().string();
|
||||||
WxError error = WxError.fromJson(responseContent);
|
WxError error = WxError.fromJson(responseContent);
|
||||||
if (error.getErrorCode() != 0) {
|
if (error.getErrorCode() != 0) {
|
||||||
|
|||||||
@ -9,6 +9,10 @@ import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
|
|||||||
import me.chanjar.weixin.mp.util.http.QrCodeRequestExecutor;
|
import me.chanjar.weixin.mp.util.http.QrCodeRequestExecutor;
|
||||||
|
|
||||||
import okhttp3.*;
|
import okhttp3.*;
|
||||||
|
import okio.BufferedSink;
|
||||||
|
import okio.Okio;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@ -19,31 +23,18 @@ import java.util.UUID;
|
|||||||
/**
|
/**
|
||||||
* Created by ecoolper on 2017/5/5.
|
* Created by ecoolper on 2017/5/5.
|
||||||
*/
|
*/
|
||||||
public class OkhttpQrCodeRequestExecutor extends QrCodeRequestExecutor<ConnectionPool, OkHttpProxyInfo> {
|
public class OkhttpQrCodeRequestExecutor extends QrCodeRequestExecutor<OkHttpClient, OkHttpProxyInfo> {
|
||||||
|
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||||
|
|
||||||
public OkhttpQrCodeRequestExecutor(RequestHttp requestHttp) {
|
public OkhttpQrCodeRequestExecutor(RequestHttp requestHttp) {
|
||||||
super(requestHttp);
|
super(requestHttp);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public File execute(String uri, WxMpQrCodeTicket data) throws WxErrorException, IOException {
|
public File execute(String uri, WxMpQrCodeTicket data) throws WxErrorException, IOException {
|
||||||
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().connectionPool(requestHttp.getRequestHttpClient());
|
logger.debug("OkhttpQrCodeRequestExecutor is running");
|
||||||
//设置代理
|
|
||||||
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
|
//得到httpClient
|
||||||
OkHttpClient client = clientBuilder.build();
|
OkHttpClient client = requestHttp.getRequestHttpClient();
|
||||||
|
|
||||||
Request request = new Request.Builder().url(uri).get().build();
|
Request request = new Request.Builder().url(uri).get().build();
|
||||||
Response response = client.newCall(request).execute();
|
Response response = client.newCall(request).execute();
|
||||||
String contentTypeHeader = response.header("Content-Type");
|
String contentTypeHeader = response.header("Content-Type");
|
||||||
@ -51,8 +42,10 @@ public class OkhttpQrCodeRequestExecutor extends QrCodeRequestExecutor<Connectio
|
|||||||
String responseContent = response.body().string();
|
String responseContent = response.body().string();
|
||||||
throw new WxErrorException(WxError.fromJson(responseContent));
|
throw new WxErrorException(WxError.fromJson(responseContent));
|
||||||
}
|
}
|
||||||
try (InputStream inputStream = new ByteArrayInputStream(response.body().bytes())) {
|
File temp = File.createTempFile(UUID.randomUUID().toString(), ".png");
|
||||||
return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg");
|
try (BufferedSink sink = Okio.buffer(Okio.sink(temp))) {
|
||||||
|
sink.writeAll(response.body().source());
|
||||||
}
|
}
|
||||||
|
return temp;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -29,4 +29,10 @@ public class WxMpBaseAPITest {
|
|||||||
Assert.assertTrue(StringUtils.isNotBlank(after));
|
Assert.assertTrue(StringUtils.isNotBlank(after));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testJsapiTicket() throws WxErrorException {
|
||||||
|
String jsapiTicket = this.wxService.getJsapiTicket(false);
|
||||||
|
System.out.println(jsapiTicket);
|
||||||
|
Assert.assertNotNull(jsapiTicket);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -20,11 +20,7 @@ public class WxMpJsAPITest {
|
|||||||
protected WxMpService wxService;
|
protected WxMpService wxService;
|
||||||
|
|
||||||
|
|
||||||
public void testJsapiTicket() throws WxErrorException {
|
|
||||||
String jsapiTicket = this.wxService.getJsapiTicket(false);
|
|
||||||
System.out.println(jsapiTicket);
|
|
||||||
Assert.assertNotNull(jsapiTicket);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void test() {
|
public void test() {
|
||||||
long timestamp = 1419835025l;
|
long timestamp = 1419835025l;
|
||||||
|
|||||||
@ -7,6 +7,7 @@ import me.chanjar.weixin.common.util.xml.XStreamInitializer;
|
|||||||
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
|
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
|
||||||
import me.chanjar.weixin.mp.api.WxMpService;
|
import me.chanjar.weixin.mp.api.WxMpService;
|
||||||
import me.chanjar.weixin.mp.api.impl.WxMpServiceApacheHttpClientImpl;
|
import me.chanjar.weixin.mp.api.impl.WxMpServiceApacheHttpClientImpl;
|
||||||
|
import me.chanjar.weixin.mp.api.impl.WxMpServiceOkHttpImpl;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
@ -19,7 +20,7 @@ public class ApiTestModule implements Module {
|
|||||||
try (InputStream is1 = ClassLoader.getSystemResourceAsStream("test-config.xml")) {
|
try (InputStream is1 = ClassLoader.getSystemResourceAsStream("test-config.xml")) {
|
||||||
TestConfigStorage config = this.fromXml(TestConfigStorage.class, is1);
|
TestConfigStorage config = this.fromXml(TestConfigStorage.class, is1);
|
||||||
config.setAccessTokenLock(new ReentrantLock());
|
config.setAccessTokenLock(new ReentrantLock());
|
||||||
WxMpService wxService = new WxMpServiceApacheHttpClientImpl();
|
WxMpService wxService = new WxMpServiceOkHttpImpl();
|
||||||
wxService.setWxMpConfigStorage(config);
|
wxService.setWxMpConfigStorage(config);
|
||||||
|
|
||||||
binder.bind(WxMpService.class).toInstance(wxService);
|
binder.bind(WxMpService.class).toInstance(wxService);
|
||||||
|
|||||||
Reference in New Issue
Block a user