mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-09-19 18:22:27 +08:00
issue #29 http代理支持
This commit is contained in:
@@ -25,4 +25,12 @@ public interface WxMpConfigStorage {
|
||||
|
||||
public int getExpiresIn();
|
||||
|
||||
public String getHttp_proxy_host();
|
||||
|
||||
public int getHttp_proxy_port();
|
||||
|
||||
public String getHttp_proxy_username();
|
||||
|
||||
public String getHttp_proxy_password();
|
||||
|
||||
}
|
||||
|
@@ -16,6 +16,11 @@ public class WxMpInMemoryConfigStorage implements WxMpConfigStorage {
|
||||
protected String aesKey;
|
||||
protected int expiresIn;
|
||||
|
||||
protected String http_proxy_host;
|
||||
protected int http_proxy_port;
|
||||
protected String http_proxy_username;
|
||||
protected String http_proxy_password;
|
||||
|
||||
public void updateAccessToken(WxAccessToken accessToken) {
|
||||
updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
|
||||
}
|
||||
@@ -73,5 +78,52 @@ public class WxMpInMemoryConfigStorage implements WxMpConfigStorage {
|
||||
this.expiresIn = expiresIn;
|
||||
}
|
||||
|
||||
public String getHttp_proxy_host() {
|
||||
return http_proxy_host;
|
||||
}
|
||||
|
||||
public void setHttp_proxy_host(String http_proxy_host) {
|
||||
this.http_proxy_host = http_proxy_host;
|
||||
}
|
||||
|
||||
public int getHttp_proxy_port() {
|
||||
return http_proxy_port;
|
||||
}
|
||||
|
||||
public void setHttp_proxy_port(int http_proxy_port) {
|
||||
this.http_proxy_port = http_proxy_port;
|
||||
}
|
||||
|
||||
public String getHttp_proxy_username() {
|
||||
return http_proxy_username;
|
||||
}
|
||||
|
||||
public void setHttp_proxy_username(String http_proxy_username) {
|
||||
this.http_proxy_username = http_proxy_username;
|
||||
}
|
||||
|
||||
public String getHttp_proxy_password() {
|
||||
return http_proxy_password;
|
||||
}
|
||||
|
||||
public void setHttp_proxy_password(String http_proxy_password) {
|
||||
this.http_proxy_password = http_proxy_password;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "WxMpInMemoryConfigStorage{" +
|
||||
"appId='" + appId + '\'' +
|
||||
", secret='" + secret + '\'' +
|
||||
", token='" + token + '\'' +
|
||||
", accessToken='" + accessToken + '\'' +
|
||||
", aesKey='" + aesKey + '\'' +
|
||||
", expiresIn=" + expiresIn +
|
||||
", http_proxy_host='" + http_proxy_host + '\'' +
|
||||
", http_proxy_port=" + http_proxy_port +
|
||||
", http_proxy_username='" + http_proxy_username + '\'' +
|
||||
", http_proxy_password='" + http_proxy_password + '\'' +
|
||||
'}';
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -19,9 +19,15 @@ import me.chanjar.weixin.mp.bean.result.*;
|
||||
import me.chanjar.weixin.mp.util.http.QrCodeRequestExecutor;
|
||||
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.auth.AuthScope;
|
||||
import org.apache.http.auth.UsernamePasswordCredentials;
|
||||
import org.apache.http.client.ClientProtocolException;
|
||||
import org.apache.http.client.CredentialsProvider;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.impl.client.BasicCredentialsProvider;
|
||||
import org.apache.http.impl.client.BasicResponseHandler;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
@@ -44,12 +50,14 @@ public class WxMpServiceImpl implements WxMpService {
|
||||
*/
|
||||
protected static final AtomicBoolean GLOBAL_ACCESS_TOKEN_REFRESH_FLAG = new AtomicBoolean(false);
|
||||
|
||||
protected static final CloseableHttpClient httpclient = HttpClients.createDefault();
|
||||
|
||||
protected WxMpConfigStorage wxMpConfigStorage;
|
||||
|
||||
protected final ThreadLocal<Integer> retryTimes = new ThreadLocal<Integer>();
|
||||
|
||||
protected CloseableHttpClient httpClient;
|
||||
|
||||
protected HttpHost httpProxy;
|
||||
|
||||
public boolean checkSignature(String timestamp, String nonce, String signature) {
|
||||
try {
|
||||
return SHA1.gen(wxMpConfigStorage.getToken(), timestamp, nonce).equals(signature);
|
||||
@@ -67,6 +75,7 @@ public class WxMpServiceImpl implements WxMpService {
|
||||
;
|
||||
try {
|
||||
HttpGet httpGet = new HttpGet(url);
|
||||
CloseableHttpClient httpclient = getHttpclient();
|
||||
CloseableHttpResponse response = httpclient.execute(httpGet);
|
||||
String resultContent = new BasicResponseHandler().handleResponse(response);
|
||||
WxError error = WxError.fromJson(resultContent);
|
||||
@@ -305,7 +314,7 @@ public class WxMpServiceImpl implements WxMpService {
|
||||
uriWithAccessToken += uri.indexOf('?') == -1 ? "?access_token=" + accessToken : "&access_token=" + accessToken;
|
||||
|
||||
try {
|
||||
return executor.execute(uriWithAccessToken, data);
|
||||
return executor.execute(getHttpclient(), httpProxy, uriWithAccessToken, data);
|
||||
} catch (WxErrorException e) {
|
||||
WxError error = e.getError();
|
||||
/*
|
||||
@@ -348,9 +357,39 @@ public class WxMpServiceImpl implements WxMpService {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected CloseableHttpClient getHttpclient() {
|
||||
return httpClient;
|
||||
}
|
||||
|
||||
public void setWxMpConfigStorage(WxMpConfigStorage wxConfigProvider) {
|
||||
this.wxMpConfigStorage = wxConfigProvider;
|
||||
|
||||
String http_proxy_host = wxMpConfigStorage.getHttp_proxy_host();
|
||||
int http_proxy_port = wxMpConfigStorage.getHttp_proxy_port();
|
||||
String http_proxy_username = wxMpConfigStorage.getHttp_proxy_username();
|
||||
String http_proxy_password = wxMpConfigStorage.getHttp_proxy_password();
|
||||
|
||||
if(StringUtils.isNotBlank(http_proxy_host)) {
|
||||
// 使用代理服务器
|
||||
if(StringUtils.isNotBlank(http_proxy_username)) {
|
||||
// 需要用户认证的代理服务器
|
||||
CredentialsProvider credsProvider = new BasicCredentialsProvider();
|
||||
credsProvider.setCredentials(
|
||||
new AuthScope(http_proxy_host, http_proxy_port),
|
||||
new UsernamePasswordCredentials(http_proxy_username, http_proxy_password));
|
||||
httpClient = HttpClients
|
||||
.custom()
|
||||
.setDefaultCredentialsProvider(credsProvider)
|
||||
.build();
|
||||
} else {
|
||||
// 无需用户认证的代理服务器
|
||||
httpClient = HttpClients.createDefault();
|
||||
}
|
||||
httpProxy = new HttpHost(http_proxy_host, http_proxy_port);
|
||||
} else {
|
||||
httpClient = HttpClients.createDefault();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -8,10 +8,13 @@ import me.chanjar.weixin.common.util.http.Utf8ResponseHandler;
|
||||
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import org.apache.http.Header;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.client.ClientProtocolException;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
@@ -27,7 +30,7 @@ import java.util.UUID;
|
||||
public class QrCodeRequestExecutor implements RequestExecutor<File, WxMpQrCodeTicket> {
|
||||
|
||||
@Override
|
||||
public File execute(String uri, WxMpQrCodeTicket ticket) throws WxErrorException, ClientProtocolException, IOException {
|
||||
public File execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, WxMpQrCodeTicket ticket) throws WxErrorException, ClientProtocolException, IOException {
|
||||
if (ticket != null) {
|
||||
if (uri.indexOf('?') == -1) {
|
||||
uri += '?';
|
||||
@@ -39,6 +42,11 @@ public class QrCodeRequestExecutor implements RequestExecutor<File, WxMpQrCodeTi
|
||||
}
|
||||
|
||||
HttpGet httpGet = new HttpGet(uri);
|
||||
if (httpProxy != null) {
|
||||
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
|
||||
httpGet.setConfig(config);
|
||||
}
|
||||
|
||||
CloseableHttpResponse response = httpclient.execute(httpGet);
|
||||
|
||||
Header[] contentTypeHeader = response.getHeaders("Content-Type");
|
||||
|
Reference in New Issue
Block a user