issue #29 http代理支持

This commit is contained in:
Daniel Qian
2014-11-03 15:56:31 +08:00
parent d659e5b621
commit 3067468f71
16 changed files with 254 additions and 17 deletions

View File

@@ -8,10 +8,13 @@ import me.chanjar.weixin.common.util.http.RequestExecutor;
import me.chanjar.weixin.common.util.http.Utf8ResponseHandler; import me.chanjar.weixin.common.util.http.Utf8ResponseHandler;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.http.Header; import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.client.ClientProtocolException; 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.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.ContentType; import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.CloseableHttpClient;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@@ -27,7 +30,7 @@ import java.util.regex.Pattern;
public class MediaDownloadRequestExecutor implements RequestExecutor<File, String> { public class MediaDownloadRequestExecutor implements RequestExecutor<File, String> {
@Override @Override
public File execute(String uri, String queryParam) throws WxErrorException, ClientProtocolException, IOException { public File execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String queryParam) throws WxErrorException, ClientProtocolException, IOException {
if (queryParam != null) { if (queryParam != null) {
if (uri.indexOf('?') == -1) { if (uri.indexOf('?') == -1) {
uri += '?'; uri += '?';
@@ -36,6 +39,11 @@ public class MediaDownloadRequestExecutor implements RequestExecutor<File, Strin
} }
HttpGet httpGet = new HttpGet(uri); HttpGet httpGet = new HttpGet(uri);
if (httpProxy != null) {
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
httpGet.setConfig(config);
}
CloseableHttpResponse response = httpclient.execute(httpGet); CloseableHttpResponse response = httpclient.execute(httpGet);
Header[] contentTypeHeader = response.getHeaders("Content-Type"); Header[] contentTypeHeader = response.getHeaders("Content-Type");

View File

@@ -5,7 +5,9 @@ import java.io.IOException;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.client.ClientProtocolException; 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.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType; import org.apache.http.entity.ContentType;
@@ -13,6 +15,7 @@ import org.apache.http.entity.mime.MultipartEntityBuilder;
import me.chanjar.weixin.common.bean.result.WxError; import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException; import me.chanjar.weixin.common.exception.WxErrorException;
import org.apache.http.impl.client.CloseableHttpClient;
/** /**
* 上传媒体文件请求执行器请求的参数是File, 返回的结果是String * 上传媒体文件请求执行器请求的参数是File, 返回的结果是String
@@ -22,8 +25,12 @@ import me.chanjar.weixin.common.exception.WxErrorException;
public class MediaUploadRequestExecutor implements RequestExecutor<WxMediaUploadResult, File> { public class MediaUploadRequestExecutor implements RequestExecutor<WxMediaUploadResult, File> {
@Override @Override
public WxMediaUploadResult execute(String uri, File file) throws WxErrorException, ClientProtocolException, IOException { public WxMediaUploadResult execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, File file) throws WxErrorException, ClientProtocolException, IOException {
HttpPost httpPost = new HttpPost(uri); HttpPost httpPost = new HttpPost(uri);
if (httpProxy != null) {
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
httpPost.setConfig(config);
}
if (file != null) { if (file != null) {
HttpEntity entity = MultipartEntityBuilder HttpEntity entity = MultipartEntityBuilder
.create() .create()

View File

@@ -2,6 +2,7 @@ package me.chanjar.weixin.common.util.http;
import java.io.IOException; import java.io.IOException;
import org.apache.http.HttpHost;
import org.apache.http.client.ClientProtocolException; import org.apache.http.client.ClientProtocolException;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.client.HttpClients;
@@ -17,8 +18,6 @@ import me.chanjar.weixin.common.exception.WxErrorException;
*/ */
public interface RequestExecutor<T, E> { public interface RequestExecutor<T, E> {
public static final CloseableHttpClient httpclient = HttpClients.createDefault(); public T execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, E data) throws WxErrorException, ClientProtocolException, IOException;
public T execute(String uri, E data) throws WxErrorException, ClientProtocolException, IOException;
} }

View File

@@ -2,12 +2,15 @@ package me.chanjar.weixin.common.util.http;
import java.io.IOException; import java.io.IOException;
import org.apache.http.HttpHost;
import org.apache.http.client.ClientProtocolException; 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.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
import me.chanjar.weixin.common.bean.result.WxError; import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException; import me.chanjar.weixin.common.exception.WxErrorException;
import org.apache.http.impl.client.CloseableHttpClient;
/** /**
* 简单的GET请求执行器请求的参数是String, 返回的结果也是String * 简单的GET请求执行器请求的参数是String, 返回的结果也是String
@@ -17,7 +20,7 @@ import me.chanjar.weixin.common.exception.WxErrorException;
public class SimpleGetRequestExecutor implements RequestExecutor<String, String> { public class SimpleGetRequestExecutor implements RequestExecutor<String, String> {
@Override @Override
public String execute(String uri, String queryParam) throws WxErrorException, ClientProtocolException, IOException { public String execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String queryParam) throws WxErrorException, ClientProtocolException, IOException {
if (queryParam != null) { if (queryParam != null) {
if (uri.indexOf('?') == -1) { if (uri.indexOf('?') == -1) {
uri += '?'; uri += '?';
@@ -25,6 +28,11 @@ public class SimpleGetRequestExecutor implements RequestExecutor<String, String>
uri += uri.endsWith("?") ? queryParam : '&' + queryParam; uri += uri.endsWith("?") ? queryParam : '&' + queryParam;
} }
HttpGet httpGet = new HttpGet(uri); HttpGet httpGet = new HttpGet(uri);
if (httpProxy != null) {
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
httpGet.setConfig(config);
}
CloseableHttpResponse response = httpclient.execute(httpGet); CloseableHttpResponse response = httpclient.execute(httpGet);
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
WxError error = WxError.fromJson(responseContent); WxError error = WxError.fromJson(responseContent);

View File

@@ -5,10 +5,20 @@ import java.io.IOException;
import me.chanjar.weixin.common.bean.result.WxError; import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException; import me.chanjar.weixin.common.exception.WxErrorException;
import org.apache.http.Consts; import org.apache.http.Consts;
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.ClientProtocolException;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse; 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.client.methods.HttpPost;
import org.apache.http.entity.StringEntity; import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
/** /**
* 简单的POST请求执行器请求的参数是String, 返回的结果也是String * 简单的POST请求执行器请求的参数是String, 返回的结果也是String
@@ -18,12 +28,18 @@ import org.apache.http.entity.StringEntity;
public class SimplePostRequestExecutor implements RequestExecutor<String, String> { public class SimplePostRequestExecutor implements RequestExecutor<String, String> {
@Override @Override
public String execute(String uri, String postEntity) throws WxErrorException, ClientProtocolException, IOException { public String execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String postEntity) throws WxErrorException, ClientProtocolException, IOException {
HttpPost httpPost = new HttpPost(uri); HttpPost httpPost = new HttpPost(uri);
if (httpProxy != null) {
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
httpPost.setConfig(config);
}
if (postEntity != null) { if (postEntity != null) {
StringEntity entity = new StringEntity(postEntity, Consts.UTF_8); StringEntity entity = new StringEntity(postEntity, Consts.UTF_8);
httpPost.setEntity(entity); httpPost.setEntity(entity);
} }
CloseableHttpResponse response = httpclient.execute(httpPost); CloseableHttpResponse response = httpclient.execute(httpPost);
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
WxError error = WxError.fromJson(responseContent); WxError error = WxError.fromJson(responseContent);

View File

@@ -27,4 +27,12 @@ public interface WxCpConfigStorage {
public int getExpiresIn(); public int getExpiresIn();
public String getHttp_proxy_host();
public int getHttp_proxy_port();
public String getHttp_proxy_username();
public String getHttp_proxy_password();
} }

View File

@@ -18,6 +18,11 @@ public class WxCpInMemoryConfigStorage implements WxCpConfigStorage {
protected String agentId; protected String agentId;
protected int expiresIn; 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) { public void updateAccessToken(WxAccessToken accessToken) {
updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn()); updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
} }
@@ -83,16 +88,53 @@ public class WxCpInMemoryConfigStorage implements WxCpConfigStorage {
this.agentId = agentId; 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 @Override
public String toString() { public String toString() {
return "WxInMemoryCpConfigStorage{" + return "WxCpInMemoryConfigStorage{" +
"appidOrCorpid='" + corpId + '\'' + "corpId='" + corpId + '\'' +
", corpSecret='" + corpSecret + '\'' + ", corpSecret='" + corpSecret + '\'' +
", token='" + token + '\'' + ", token='" + token + '\'' +
", accessToken='" + accessToken + '\'' + ", accessToken='" + accessToken + '\'' +
", aesKey='" + aesKey + '\'' + ", aesKey='" + aesKey + '\'' +
", agentId='" + agentId + '\'' + ", agentId='" + agentId + '\'' +
", expiresIn=" + expiresIn + ", 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 + '\'' +
'}'; '}';
} }
} }

View File

@@ -20,9 +20,14 @@ import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
import me.chanjar.weixin.common.util.crypto.SHA1; import me.chanjar.weixin.common.util.crypto.SHA1;
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder; import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
import org.apache.commons.lang3.StringUtils; 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.ClientProtocolException;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet; 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.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients; 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 AtomicBoolean GLOBAL_ACCESS_TOKEN_REFRESH_FLAG = new AtomicBoolean(false);
protected static final CloseableHttpClient httpclient = HttpClients.createDefault();
protected WxCpConfigStorage wxCpConfigStorage; protected WxCpConfigStorage wxCpConfigStorage;
protected final ThreadLocal<Integer> retryTimes = new ThreadLocal<Integer>(); 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) { public boolean checkSignature(String msgSignature, String timestamp, String nonce, String data) {
try { try {
return SHA1.gen(wxCpConfigStorage.getToken(), timestamp, nonce, data).equals(msgSignature); return SHA1.gen(wxCpConfigStorage.getToken(), timestamp, nonce, data).equals(msgSignature);
@@ -78,6 +85,7 @@ public class WxCpServiceImpl implements WxCpService {
+ "&corpsecret=" + wxCpConfigStorage.getCorpSecret(); + "&corpsecret=" + wxCpConfigStorage.getCorpSecret();
try { try {
HttpGet httpGet = new HttpGet(url); HttpGet httpGet = new HttpGet(url);
CloseableHttpClient httpclient = getHttpclient();
CloseableHttpResponse response = httpclient.execute(httpGet); CloseableHttpResponse response = httpclient.execute(httpGet);
String resultContent = new BasicResponseHandler().handleResponse(response); String resultContent = new BasicResponseHandler().handleResponse(response);
WxError error = WxError.fromJson(resultContent); WxError error = WxError.fromJson(resultContent);
@@ -335,7 +343,7 @@ public class WxCpServiceImpl implements WxCpService {
uriWithAccessToken += uri.indexOf('?') == -1 ? "?access_token=" + accessToken : "&access_token=" + accessToken; uriWithAccessToken += uri.indexOf('?') == -1 ? "?access_token=" + accessToken : "&access_token=" + accessToken;
try { try {
return executor.execute(uriWithAccessToken, data); return executor.execute(getHttpclient(), httpProxy, uriWithAccessToken, data);
} catch (WxErrorException e) { } catch (WxErrorException e) {
WxError error = e.getError(); WxError error = e.getError();
/* /*
@@ -379,8 +387,38 @@ public class WxCpServiceImpl implements WxCpService {
} }
} }
protected CloseableHttpClient getHttpclient() {
return httpClient;
}
public void setWxCpConfigStorage(WxCpConfigStorage wxConfigProvider) { public void setWxCpConfigStorage(WxCpConfigStorage wxConfigProvider) {
this.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();
}
} }
} }

View File

@@ -5,6 +5,7 @@ import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import org.testng.Assert; import org.testng.Assert;
import org.testng.annotations.DataProvider; import org.testng.annotations.DataProvider;

View File

@@ -1,5 +1,6 @@
package me.chanjar.weixin.cp.api; package me.chanjar.weixin.cp.api;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.cp.bean.WxCpMessage; import me.chanjar.weixin.cp.bean.WxCpMessage;
import org.testng.annotations.Guice; import org.testng.annotations.Guice;
import org.testng.annotations.Test; import org.testng.annotations.Test;

View File

@@ -2,6 +2,7 @@ package me.chanjar.weixin.cp.api;
import java.util.Map; import java.util.Map;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.cp.bean.WxCpXmlMessage; import me.chanjar.weixin.cp.bean.WxCpXmlMessage;
import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage; import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage;
import org.testng.Assert; import org.testng.Assert;

View File

@@ -2,6 +2,7 @@ package me.chanjar.weixin.cp.api;
import javax.xml.bind.JAXBException; import javax.xml.bind.JAXBException;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.WxMenu; import me.chanjar.weixin.common.bean.WxMenu;
import org.testng.Assert; import org.testng.Assert;
import org.testng.annotations.DataProvider; import org.testng.annotations.DataProvider;

View File

@@ -25,4 +25,12 @@ public interface WxMpConfigStorage {
public int getExpiresIn(); public int getExpiresIn();
public String getHttp_proxy_host();
public int getHttp_proxy_port();
public String getHttp_proxy_username();
public String getHttp_proxy_password();
} }

View File

@@ -16,6 +16,11 @@ public class WxMpInMemoryConfigStorage implements WxMpConfigStorage {
protected String aesKey; protected String aesKey;
protected int expiresIn; 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) { public void updateAccessToken(WxAccessToken accessToken) {
updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn()); updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
} }
@@ -73,5 +78,52 @@ public class WxMpInMemoryConfigStorage implements WxMpConfigStorage {
this.expiresIn = expiresIn; 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 + '\'' +
'}';
}
} }

View File

@@ -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.http.QrCodeRequestExecutor;
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
import org.apache.commons.lang3.StringUtils; 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.ClientProtocolException;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet; 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.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients; 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 AtomicBoolean GLOBAL_ACCESS_TOKEN_REFRESH_FLAG = new AtomicBoolean(false);
protected static final CloseableHttpClient httpclient = HttpClients.createDefault();
protected WxMpConfigStorage wxMpConfigStorage; protected WxMpConfigStorage wxMpConfigStorage;
protected final ThreadLocal<Integer> retryTimes = new ThreadLocal<Integer>(); protected final ThreadLocal<Integer> retryTimes = new ThreadLocal<Integer>();
protected CloseableHttpClient httpClient;
protected HttpHost httpProxy;
public boolean checkSignature(String timestamp, String nonce, String signature) { public boolean checkSignature(String timestamp, String nonce, String signature) {
try { try {
return SHA1.gen(wxMpConfigStorage.getToken(), timestamp, nonce).equals(signature); return SHA1.gen(wxMpConfigStorage.getToken(), timestamp, nonce).equals(signature);
@@ -67,6 +75,7 @@ public class WxMpServiceImpl implements WxMpService {
; ;
try { try {
HttpGet httpGet = new HttpGet(url); HttpGet httpGet = new HttpGet(url);
CloseableHttpClient httpclient = getHttpclient();
CloseableHttpResponse response = httpclient.execute(httpGet); CloseableHttpResponse response = httpclient.execute(httpGet);
String resultContent = new BasicResponseHandler().handleResponse(response); String resultContent = new BasicResponseHandler().handleResponse(response);
WxError error = WxError.fromJson(resultContent); WxError error = WxError.fromJson(resultContent);
@@ -305,7 +314,7 @@ public class WxMpServiceImpl implements WxMpService {
uriWithAccessToken += uri.indexOf('?') == -1 ? "?access_token=" + accessToken : "&access_token=" + accessToken; uriWithAccessToken += uri.indexOf('?') == -1 ? "?access_token=" + accessToken : "&access_token=" + accessToken;
try { try {
return executor.execute(uriWithAccessToken, data); return executor.execute(getHttpclient(), httpProxy, uriWithAccessToken, data);
} catch (WxErrorException e) { } catch (WxErrorException e) {
WxError error = e.getError(); WxError error = e.getError();
/* /*
@@ -349,8 +358,38 @@ public class WxMpServiceImpl implements WxMpService {
} }
} }
protected CloseableHttpClient getHttpclient() {
return httpClient;
}
public void setWxMpConfigStorage(WxMpConfigStorage wxConfigProvider) { public void setWxMpConfigStorage(WxMpConfigStorage wxConfigProvider) {
this.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();
}
} }
} }

View File

@@ -8,10 +8,13 @@ import me.chanjar.weixin.common.util.http.Utf8ResponseHandler;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket; import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
import me.chanjar.weixin.common.exception.WxErrorException; import me.chanjar.weixin.common.exception.WxErrorException;
import org.apache.http.Header; import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.client.ClientProtocolException; 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.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.ContentType; import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.CloseableHttpClient;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@@ -27,7 +30,7 @@ import java.util.UUID;
public class QrCodeRequestExecutor implements RequestExecutor<File, WxMpQrCodeTicket> { public class QrCodeRequestExecutor implements RequestExecutor<File, WxMpQrCodeTicket> {
@Override @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 (ticket != null) {
if (uri.indexOf('?') == -1) { if (uri.indexOf('?') == -1) {
uri += '?'; uri += '?';
@@ -39,6 +42,11 @@ public class QrCodeRequestExecutor implements RequestExecutor<File, WxMpQrCodeTi
} }
HttpGet httpGet = new HttpGet(uri); HttpGet httpGet = new HttpGet(uri);
if (httpProxy != null) {
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
httpGet.setConfig(config);
}
CloseableHttpResponse response = httpclient.execute(httpGet); CloseableHttpResponse response = httpclient.execute(httpGet);
Header[] contentTypeHeader = response.getHeaders("Content-Type"); Header[] contentTypeHeader = response.getHeaders("Content-Type");