Skip to content

Commit b26c786

Browse files
🎨 #3745 【微信支付】修复请求微信仿真测试系统时验签密钥接口的 Content-Type 问题
1 parent b9b4f00 commit b26c786

File tree

5 files changed

+157
-3
lines changed

5 files changed

+157
-3
lines changed

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/WxPayService.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,19 @@ public interface WxPayService {
109109
*/
110110
String post(String url, String requestStr, boolean useKey) throws WxPayException;
111111

112+
113+
/**
114+
* 发送post请求,得到响应字符串.
115+
*
116+
* @param url 请求地址
117+
* @param requestStr 请求信息
118+
* @param useKey 是否使用证书
119+
* @param mimeType Content-Type请求头
120+
* @return 返回请求结果字符串 string
121+
* @throws WxPayException the wx pay exception
122+
*/
123+
String post(String url, String requestStr, boolean useKey, String mimeType) throws WxPayException;
124+
112125
/**
113126
* 发送post请求,得到响应字符串.
114127
*
@@ -1457,6 +1470,7 @@ WxPayRefundQueryResult refundQuery(String transactionId, String outTradeNo, Stri
14571470
* 是否需要证书: 否
14581471
* 请求方式: POST
14591472
* 文档地址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=23_1
1473+
* 注意: 微信暂不支持api v3
14601474
* </pre>
14611475
*
14621476
* @return the sandbox sign key

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/BaseWxPayServiceImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import me.chanjar.weixin.common.error.WxRuntimeException;
3636
import org.apache.commons.lang3.StringUtils;
3737
import org.apache.commons.lang3.reflect.ConstructorUtils;
38+
import org.apache.http.entity.ContentType;
3839

3940
import java.io.File;
4041
import java.io.IOException;
@@ -1262,7 +1263,7 @@ public String getSandboxSignKey() throws WxPayException {
12621263
request.checkAndSign(this.getConfig());
12631264

12641265
String url = "https://api.mch.weixin.qq.com/xdc/apiv2getsignkey/sign/getsignkey";
1265-
String responseContent = this.post(url, request.toXML(), false);
1266+
String responseContent = this.post(url, request.toXML(), false, ContentType.APPLICATION_XML.getMimeType());
12661267
WxPaySandboxSignKeyResult result = BaseWxPayResult.fromXML(responseContent, WxPaySandboxSignKeyResult.class);
12671268
result.checkResult(this, request.getSignType(), true);
12681269
return result.getSandboxSignKey();

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceApacheHttpImpl.java

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public byte[] postForBytes(String url, String requestStr, boolean useKey) throws
5454
try {
5555
HttpPost httpPost = this.createHttpPost(url, requestStr);
5656
CloseableHttpClient httpClient = this.createHttpClient(useKey);
57-
57+
5858
// 使用连接池的客户端,不需要手动关闭
5959
final byte[] bytes = httpClient.execute(httpPost, ByteArrayResponseHandler.INSTANCE);
6060
final String responseData = Base64.getEncoder().encodeToString(bytes);
@@ -73,7 +73,33 @@ public String post(String url, String requestStr, boolean useKey) throws WxPayEx
7373
try {
7474
HttpPost httpPost = this.createHttpPost(url, requestStr);
7575
CloseableHttpClient httpClient = this.createHttpClient(useKey);
76-
76+
77+
// 使用连接池的客户端,不需要手动关闭
78+
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
79+
String responseString = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
80+
this.logRequestAndResponse(url, requestStr, responseString);
81+
if (this.getConfig().isIfSaveApiData()) {
82+
wxApiData.set(new WxPayApiData(url, requestStr, responseString, null));
83+
}
84+
return responseString;
85+
} finally {
86+
httpPost.releaseConnection();
87+
}
88+
} catch (Exception e) {
89+
this.logError(url, requestStr, e);
90+
if (this.getConfig().isIfSaveApiData()) {
91+
wxApiData.set(new WxPayApiData(url, requestStr, null, e.getMessage()));
92+
}
93+
throw new WxPayException(e.getMessage(), e);
94+
}
95+
}
96+
97+
@Override
98+
public String post(String url, String requestStr, boolean useKey, String mimeType) throws WxPayException {
99+
try {
100+
HttpPost httpPost = this.createHttpPost(url, requestStr, mimeType);
101+
CloseableHttpClient httpClient = this.createHttpClient(useKey);
102+
77103
// 使用连接池的客户端,不需要手动关闭
78104
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
79105
String responseString = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
@@ -306,6 +332,10 @@ private static StringEntity createEntry(String requestStr) {
306332
//return new StringEntity(new String(requestStr.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
307333
}
308334

335+
private static StringEntity createEntry(String requestStr, String mimeType) {
336+
return new StringEntity(requestStr, ContentType.create(mimeType, StandardCharsets.UTF_8));
337+
//return new StringEntity(new String(requestStr.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
338+
}
309339
private HttpClientBuilder createHttpClientBuilder(boolean useKey) throws WxPayException {
310340
HttpClientBuilder httpClientBuilder = HttpClients.custom();
311341
if (useKey) {
@@ -348,6 +378,19 @@ private HttpPost createHttpPost(String url, String requestStr) {
348378
return httpPost;
349379
}
350380

381+
private HttpPost createHttpPost(String url, String requestStr, String mimeType) throws WxPayException {
382+
HttpPost httpPost = new HttpPost(url);
383+
httpPost.setEntity(createEntry(requestStr, mimeType));
384+
385+
httpPost.setConfig(RequestConfig.custom()
386+
.setConnectionRequestTimeout(this.getConfig().getHttpConnectionTimeout())
387+
.setConnectTimeout(this.getConfig().getHttpConnectionTimeout())
388+
.setSocketTimeout(this.getConfig().getHttpTimeout())
389+
.build());
390+
391+
return httpPost;
392+
}
393+
351394
private void initSSLContext(HttpClientBuilder httpClientBuilder) throws WxPayException {
352395
SSLContext sslContext = this.getConfig().getSslContext();
353396
if (null == sslContext) {

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceHttpComponentsImpl.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,32 @@ public String post(String url, String requestStr, boolean useKey) throws WxPayEx
9191
}
9292
}
9393

94+
@Override
95+
public String post(String url, String requestStr, boolean useKey, String mimeType) throws WxPayException {
96+
try {
97+
HttpClientBuilder httpClientBuilder = this.createHttpClientBuilder(useKey);
98+
HttpPost httpPost = this.createHttpPost(url, requestStr, mimeType);
99+
try (CloseableHttpClient httpClient = httpClientBuilder.build()) {
100+
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
101+
String responseString = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
102+
this.logRequestAndResponse(url, requestStr, responseString);
103+
if (this.getConfig().isIfSaveApiData()) {
104+
wxApiData.set(new WxPayApiData(url, requestStr, responseString, null));
105+
}
106+
return responseString;
107+
}
108+
} finally {
109+
httpPost.releaseConnection();
110+
}
111+
} catch (Exception e) {
112+
this.logError(url, requestStr, e);
113+
if (this.getConfig().isIfSaveApiData()) {
114+
wxApiData.set(new WxPayApiData(url, requestStr, null, e.getMessage()));
115+
}
116+
throw new WxPayException(e.getMessage(), e);
117+
}
118+
}
119+
94120
@Override
95121
public String postV3(String url, String requestStr) throws WxPayException {
96122
HttpPost httpPost = this.createHttpPost(url, requestStr);
@@ -283,6 +309,11 @@ private static StringEntity createEntry(String requestStr) {
283309
//return new StringEntity(new String(requestStr.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
284310
}
285311

312+
private static StringEntity createEntry(String requestStr, String mimeType) {
313+
return new StringEntity(requestStr, ContentType.create(mimeType, StandardCharsets.UTF_8));
314+
//return new StringEntity(new String(requestStr.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
315+
}
316+
286317
private HttpClientBuilder createHttpClientBuilder(boolean useKey) throws WxPayException {
287318
HttpClientBuilder httpClientBuilder = HttpClients.custom();
288319
if (useKey) {
@@ -325,6 +356,19 @@ private HttpPost createHttpPost(String url, String requestStr) {
325356
return httpPost;
326357
}
327358

359+
private HttpPost createHttpPost(String url, String requestStr, String mimeType) throws WxPayException {
360+
HttpPost httpPost = new HttpPost(url);
361+
httpPost.setEntity(createEntry(requestStr, mimeType));
362+
363+
httpPost.setConfig(RequestConfig.custom()
364+
.setConnectionRequestTimeout(this.getConfig().getHttpConnectionTimeout())
365+
.setConnectTimeout(this.getConfig().getHttpConnectionTimeout())
366+
.setSocketTimeout(this.getConfig().getHttpTimeout())
367+
.build());
368+
369+
return httpPost;
370+
}
371+
328372
private void initSSLContext(HttpClientBuilder httpClientBuilder) throws WxPayException {
329373
SSLContext sslContext = this.getConfig().getSslContext();
330374
if (null == sslContext) {

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceJoddHttpImpl.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,24 @@ public String post(String url, String requestStr, boolean useKey) throws WxPayEx
6363
}
6464
}
6565

66+
@Override
67+
public String post(String url, String requestStr, boolean useKey, String mimeType) throws WxPayException {
68+
try {
69+
HttpRequest request = this.buildHttpRequest(url, requestStr, useKey, mimeType);
70+
String responseString = this.getResponseString(request.send());
71+
72+
log.info("\n【请求地址】:{}\n【请求数据】:{}\n【响应数据】:{}", url, requestStr, responseString);
73+
if (this.getConfig().isIfSaveApiData()) {
74+
wxApiData.set(new WxPayApiData(url, requestStr, responseString, null));
75+
}
76+
return responseString;
77+
} catch (Exception e) {
78+
log.error("\n【请求地址】:{}\n【请求数据】:{}\n【异常信息】:{}", url, requestStr, e.getMessage());
79+
wxApiData.set(new WxPayApiData(url, requestStr, null, e.getMessage()));
80+
throw new WxPayException(e.getMessage(), e);
81+
}
82+
}
83+
6684
@Override
6785
public String postV3(String url, String requestStr) throws WxPayException {
6886
return null;
@@ -146,6 +164,40 @@ private HttpRequest buildHttpRequest(String url, String requestStr, boolean useK
146164
return request;
147165
}
148166

167+
private HttpRequest buildHttpRequest(String url, String requestStr, boolean useKey, String mimeType) throws WxPayException {
168+
HttpRequest request = HttpRequest
169+
.post(url)
170+
.contentType(mimeType)
171+
.timeout(this.getConfig().getHttpTimeout())
172+
.connectionTimeout(this.getConfig().getHttpConnectionTimeout())
173+
.bodyText(requestStr);
174+
175+
if (useKey) {
176+
SSLContext sslContext = this.getConfig().getSslContext();
177+
if (null == sslContext) {
178+
sslContext = this.getConfig().initSSLContext();
179+
}
180+
final SSLSocketHttpConnectionProvider provider = new SSLSocketHttpConnectionProvider(sslContext);
181+
request.withConnectionProvider(provider);
182+
}
183+
184+
if (StringUtils.isNotBlank(this.getConfig().getHttpProxyHost()) && this.getConfig().getHttpProxyPort() > 0) {
185+
if (StringUtils.isEmpty(this.getConfig().getHttpProxyUsername())) {
186+
this.getConfig().setHttpProxyUsername("whatever");
187+
}
188+
189+
ProxyInfo httpProxy = new ProxyInfo(ProxyType.HTTP, this.getConfig().getHttpProxyHost(), this.getConfig().getHttpProxyPort(),
190+
this.getConfig().getHttpProxyUsername(), this.getConfig().getHttpProxyPassword());
191+
HttpConnectionProvider provider = request.connectionProvider();
192+
if (null == provider) {
193+
provider = new SocketHttpConnectionProvider();
194+
}
195+
provider.useProxy(httpProxy);
196+
request.withConnectionProvider(provider);
197+
}
198+
return request;
199+
}
200+
149201
private String getResponseString(HttpResponse response) throws WxPayException {
150202
try {
151203
log.debug("【微信服务器响应头信息】:\n{}", response.toString(false));

0 commit comments

Comments
 (0)