mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-09-22 12:09:33 +08:00
issue #29 http代理支持
This commit is contained in:
@@ -27,4 +27,12 @@ public interface WxCpConfigStorage {
|
||||
|
||||
public int getExpiresIn();
|
||||
|
||||
public String getHttp_proxy_host();
|
||||
|
||||
public int getHttp_proxy_port();
|
||||
|
||||
public String getHttp_proxy_username();
|
||||
|
||||
public String getHttp_proxy_password();
|
||||
|
||||
}
|
||||
|
@@ -18,6 +18,11 @@ public class WxCpInMemoryConfigStorage implements WxCpConfigStorage {
|
||||
protected String agentId;
|
||||
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());
|
||||
}
|
||||
@@ -83,16 +88,53 @@ public class WxCpInMemoryConfigStorage implements WxCpConfigStorage {
|
||||
this.agentId = agentId;
|
||||
}
|
||||
|
||||
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 "WxInMemoryCpConfigStorage{" +
|
||||
"appidOrCorpid='" + corpId + '\'' +
|
||||
return "WxCpInMemoryConfigStorage{" +
|
||||
"corpId='" + corpId + '\'' +
|
||||
", corpSecret='" + corpSecret + '\'' +
|
||||
", token='" + token + '\'' +
|
||||
", accessToken='" + accessToken + '\'' +
|
||||
", aesKey='" + aesKey + '\'' +
|
||||
", agentId='" + agentId + '\'' +
|
||||
", 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 + '\'' +
|
||||
'}';
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -20,9 +20,14 @@ import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.crypto.SHA1;
|
||||
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
|
||||
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.impl.client.BasicCredentialsProvider;
|
||||
import org.apache.http.impl.client.BasicResponseHandler;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
@@ -51,12 +56,14 @@ public class WxCpServiceImpl implements WxCpService {
|
||||
*/
|
||||
protected static final AtomicBoolean GLOBAL_ACCESS_TOKEN_REFRESH_FLAG = new AtomicBoolean(false);
|
||||
|
||||
protected static final CloseableHttpClient httpclient = HttpClients.createDefault();
|
||||
|
||||
protected WxCpConfigStorage wxCpConfigStorage;
|
||||
|
||||
protected final ThreadLocal<Integer> retryTimes = new ThreadLocal<Integer>();
|
||||
|
||||
protected CloseableHttpClient httpClient;
|
||||
|
||||
protected HttpHost httpProxy;
|
||||
|
||||
public boolean checkSignature(String msgSignature, String timestamp, String nonce, String data) {
|
||||
try {
|
||||
return SHA1.gen(wxCpConfigStorage.getToken(), timestamp, nonce, data).equals(msgSignature);
|
||||
@@ -78,6 +85,7 @@ public class WxCpServiceImpl implements WxCpService {
|
||||
+ "&corpsecret=" + wxCpConfigStorage.getCorpSecret();
|
||||
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);
|
||||
@@ -335,7 +343,7 @@ public class WxCpServiceImpl implements WxCpService {
|
||||
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();
|
||||
/*
|
||||
@@ -379,8 +387,38 @@ public class WxCpServiceImpl implements WxCpService {
|
||||
}
|
||||
}
|
||||
|
||||
protected CloseableHttpClient getHttpclient() {
|
||||
return httpClient;
|
||||
}
|
||||
|
||||
public void setWxCpConfigStorage(WxCpConfigStorage wxConfigProvider) {
|
||||
this.wxCpConfigStorage = wxConfigProvider;
|
||||
|
||||
String http_proxy_host = wxCpConfigStorage.getHttp_proxy_host();
|
||||
int http_proxy_port = wxCpConfigStorage.getHttp_proxy_port();
|
||||
String http_proxy_username = wxCpConfigStorage.getHttp_proxy_username();
|
||||
String http_proxy_password = wxCpConfigStorage.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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -5,6 +5,7 @@ import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import me.chanjar.weixin.common.api.WxConsts;
|
||||
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.DataProvider;
|
||||
|
@@ -1,5 +1,6 @@
|
||||
package me.chanjar.weixin.cp.api;
|
||||
|
||||
import me.chanjar.weixin.common.api.WxConsts;
|
||||
import me.chanjar.weixin.cp.bean.WxCpMessage;
|
||||
import org.testng.annotations.Guice;
|
||||
import org.testng.annotations.Test;
|
||||
|
@@ -2,6 +2,7 @@ package me.chanjar.weixin.cp.api;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import me.chanjar.weixin.common.api.WxConsts;
|
||||
import me.chanjar.weixin.cp.bean.WxCpXmlMessage;
|
||||
import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage;
|
||||
import org.testng.Assert;
|
||||
|
@@ -2,6 +2,7 @@ package me.chanjar.weixin.cp.api;
|
||||
|
||||
import javax.xml.bind.JAXBException;
|
||||
|
||||
import me.chanjar.weixin.common.api.WxConsts;
|
||||
import me.chanjar.weixin.common.bean.WxMenu;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.DataProvider;
|
||||
|
Reference in New Issue
Block a user