From d88a8d3d8392dcab1937a0372b0c4070f8e2e41c Mon Sep 17 00:00:00 2001 From: Binary Wang Date: Sat, 29 Oct 2016 17:14:05 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=BD=E5=8F=96=E8=8E=B7=E5=8F=96=E4=BB=A3?= =?UTF-8?q?=E7=90=86=E5=AF=B9=E8=B1=A1=E7=9A=84=E6=96=B9=E6=B3=95=E5=88=B0?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E4=B8=AD=EF=BC=8C=E6=96=B9=E4=BE=BF=E5=BE=AE?= =?UTF-8?q?=E4=BF=A1=E6=94=AF=E4=BB=98=E8=B0=83=E7=94=A8=EF=BC=8C=E5=B9=B6?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=83=A8=E5=88=86=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../me/chanjar/weixin/mp/api/WxMpService.java | 6 +++ .../mp/api/impl/WxMpPayServiceImpl.java | 40 ++++++++++++++++--- .../weixin/mp/api/impl/WxMpServiceImpl.java | 1 + 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java index 01ec5b13..a74e718d 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java @@ -5,6 +5,7 @@ import me.chanjar.weixin.common.exception.WxErrorException; import me.chanjar.weixin.common.util.http.RequestExecutor; import me.chanjar.weixin.mp.bean.*; import me.chanjar.weixin.mp.bean.result.*; +import org.apache.http.HttpHost; /** * 微信API的Service @@ -228,6 +229,11 @@ public interface WxMpService { */ T execute(RequestExecutor executor, String uri, E data) throws WxErrorException; + /** + * 获取代理对象 + */ + HttpHost getHttpProxy(); + /** * 注入 {@link WxMpConfigStorage} 的实现 */ diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpPayServiceImpl.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpPayServiceImpl.java index 79acad73..9064515d 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpPayServiceImpl.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpPayServiceImpl.java @@ -14,6 +14,8 @@ import me.chanjar.weixin.mp.bean.pay.result.*; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.StringUtils; +import org.apache.http.Consts; +import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.conn.ssl.DefaultHostnameVerifier; @@ -27,6 +29,7 @@ import org.apache.http.util.EntityUtils; import javax.net.ssl.SSLContext; import java.io.File; import java.io.FileInputStream; +import java.io.IOException; import java.security.KeyStore; import java.util.*; @@ -199,7 +202,7 @@ public class WxMpPayServiceImpl implements WxMpPayService { String url = PAY_BASE_URL + "/pay/orderquery"; - String responseContent = this.wxMpService.post(url, xstream.toXML(request)); + String responseContent = this.executeRequest(url, xstream.toXML(request)); WxPayOrderQueryResult result = (WxPayOrderQueryResult) xstream.fromXML(responseContent); result.composeCoupons(responseContent); if ("FAIL".equals(result.getResultCode())) { @@ -233,7 +236,7 @@ public class WxMpPayServiceImpl implements WxMpPayService { String url = PAY_BASE_URL + "/pay/closeorder"; - String responseContent = this.wxMpService.post(url, xstream.toXML(request)); + String responseContent = this.executeRequest(url, xstream.toXML(request)); WxPayOrderCloseResult result = (WxPayOrderCloseResult) xstream.fromXML(responseContent); if ("FAIL".equals(result.getResultCode())) { throw new WxErrorException(WxError.newBuilder() @@ -263,7 +266,7 @@ public class WxMpPayServiceImpl implements WxMpPayService { String url = PAY_BASE_URL + "/pay/unifiedorder"; - String responseContent = this.wxMpService.post(url, xstream.toXML(request)); + String responseContent = this.executeRequest(url, xstream.toXML(request)); WxPayUnifiedOrderResult result = (WxPayUnifiedOrderResult) xstream .fromXML(responseContent); if ("FAIL".equals(result.getResultCode())) { @@ -377,7 +380,27 @@ public class WxMpPayServiceImpl implements WxMpPayService { return result; } + private String executeRequest( String url, String requestStr) throws WxErrorException { + HttpPost httpPost = new HttpPost(url); + if (this.wxMpService.getHttpProxy() != null) { + httpPost.setConfig(RequestConfig.custom().setProxy(this.wxMpService.getHttpProxy()).build()); + } + + try (CloseableHttpClient httpclient = HttpClients.custom().build()) { + httpPost.setEntity(new StringEntity(new String(requestStr.getBytes("UTF-8"), "ISO-8859-1"))); + + try (CloseableHttpResponse response = httpclient.execute(httpPost)) { + return EntityUtils.toString(response.getEntity(), Consts.UTF_8); + } + } catch (IOException e) { + throw new WxErrorException(WxError.newBuilder().setErrorMsg(e.getMessage()).build(), e); + }finally { + httpPost.releaseConnection(); + } + } + private String executeRequestWithKeyFile( String url, File keyFile, String requestStr, String mchId) throws WxErrorException { + try (FileInputStream inputStream = new FileInputStream(keyFile)) { KeyStore keyStore = KeyStore.getInstance("PKCS12"); keyStore.load(inputStream, mchId.toCharArray()); @@ -386,13 +409,18 @@ public class WxMpPayServiceImpl implements WxMpPayService { SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" }, null, new DefaultHostnameVerifier()); + HttpPost httpPost = new HttpPost(url); + if (this.wxMpService.getHttpProxy() != null) { + httpPost.setConfig(RequestConfig.custom().setProxy(this.wxMpService.getHttpProxy()).build()); + } + try (CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build()) { - HttpPost httpPost = new HttpPost(url); httpPost.setEntity(new StringEntity(new String(requestStr.getBytes("UTF-8"), "ISO-8859-1"))); - try (CloseableHttpResponse response = httpclient.execute(httpPost)) { - return EntityUtils.toString(response.getEntity()); + return EntityUtils.toString(response.getEntity(), Consts.UTF_8); } + }finally { + httpPost.releaseConnection(); } } catch (Exception e) { throw new WxErrorException(WxError.newBuilder().setErrorMsg(e.getMessage()).build(), e); diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpServiceImpl.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpServiceImpl.java index 7b5171f5..84ba693c 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpServiceImpl.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpServiceImpl.java @@ -425,6 +425,7 @@ public class WxMpServiceImpl implements WxMpService { } } + @Override public HttpHost getHttpProxy() { return this.httpProxy; }