2020import me .chanjar .weixin .common .util .crypto .SHA1 ;
2121import me .chanjar .weixin .cp .util .json .WxCpGsonBuilder ;
2222import org .apache .commons .lang3 .StringUtils ;
23+ import org .apache .http .HttpHost ;
24+ import org .apache .http .auth .AuthScope ;
25+ import org .apache .http .auth .UsernamePasswordCredentials ;
2326import org .apache .http .client .ClientProtocolException ;
27+ import org .apache .http .client .CredentialsProvider ;
2428import org .apache .http .client .methods .CloseableHttpResponse ;
2529import org .apache .http .client .methods .HttpGet ;
30+ import org .apache .http .impl .client .BasicCredentialsProvider ;
2631import org .apache .http .impl .client .BasicResponseHandler ;
2732import org .apache .http .impl .client .CloseableHttpClient ;
2833import org .apache .http .impl .client .HttpClients ;
@@ -51,12 +56,14 @@ public class WxCpServiceImpl implements WxCpService {
5156 */
5257 protected static final AtomicBoolean GLOBAL_ACCESS_TOKEN_REFRESH_FLAG = new AtomicBoolean (false );
5358
54- protected static final CloseableHttpClient httpclient = HttpClients .createDefault ();
55-
5659 protected WxCpConfigStorage wxCpConfigStorage ;
5760
5861 protected final ThreadLocal <Integer > retryTimes = new ThreadLocal <Integer >();
5962
63+ protected CloseableHttpClient httpClient ;
64+
65+ protected HttpHost httpProxy ;
66+
6067 public boolean checkSignature (String msgSignature , String timestamp , String nonce , String data ) {
6168 try {
6269 return SHA1 .gen (wxCpConfigStorage .getToken (), timestamp , nonce , data ).equals (msgSignature );
@@ -78,6 +85,7 @@ public void accessTokenRefresh() throws WxErrorException {
7885 + "&corpsecret=" + wxCpConfigStorage .getCorpSecret ();
7986 try {
8087 HttpGet httpGet = new HttpGet (url );
88+ CloseableHttpClient httpclient = getHttpclient ();
8189 CloseableHttpResponse response = httpclient .execute (httpGet );
8290 String resultContent = new BasicResponseHandler ().handleResponse (response );
8391 WxError error = WxError .fromJson (resultContent );
@@ -335,7 +343,7 @@ public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) thro
335343 uriWithAccessToken += uri .indexOf ('?' ) == -1 ? "?access_token=" + accessToken : "&access_token=" + accessToken ;
336344
337345 try {
338- return executor .execute (uriWithAccessToken , data );
346+ return executor .execute (getHttpclient (), httpProxy , uriWithAccessToken , data );
339347 } catch (WxErrorException e ) {
340348 WxError error = e .getError ();
341349 /*
@@ -379,8 +387,38 @@ public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) thro
379387 }
380388 }
381389
390+ protected CloseableHttpClient getHttpclient () {
391+ return httpClient ;
392+ }
393+
382394 public void setWxCpConfigStorage (WxCpConfigStorage wxConfigProvider ) {
383395 this .wxCpConfigStorage = wxConfigProvider ;
396+
397+ String http_proxy_host = wxCpConfigStorage .getHttp_proxy_host ();
398+ int http_proxy_port = wxCpConfigStorage .getHttp_proxy_port ();
399+ String http_proxy_username = wxCpConfigStorage .getHttp_proxy_username ();
400+ String http_proxy_password = wxCpConfigStorage .getHttp_proxy_password ();
401+
402+ if (StringUtils .isNotBlank (http_proxy_host )) {
403+ // 使用代理服务器
404+ if (StringUtils .isNotBlank (http_proxy_username )) {
405+ // 需要用户认证的代理服务器
406+ CredentialsProvider credsProvider = new BasicCredentialsProvider ();
407+ credsProvider .setCredentials (
408+ new AuthScope (http_proxy_host , http_proxy_port ),
409+ new UsernamePasswordCredentials (http_proxy_username , http_proxy_password ));
410+ httpClient = HttpClients
411+ .custom ()
412+ .setDefaultCredentialsProvider (credsProvider )
413+ .build ();
414+ } else {
415+ // 无需用户认证的代理服务器
416+ httpClient = HttpClients .createDefault ();
417+ }
418+ httpProxy = new HttpHost (http_proxy_host , http_proxy_port );
419+ } else {
420+ httpClient = HttpClients .createDefault ();
421+ }
384422 }
385423
386424}
0 commit comments