Skip to content

Commit d3c691a

Browse files
committed
微信支付接口请求增加超时时间的设置参数
1 parent c5c204b commit d3c691a

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/config/WxPayConfig.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@
1818
* @author Binary Wang (https://github.com/binarywang)
1919
*/
2020
public class WxPayConfig {
21+
22+
/**
23+
* http请求连接超时时间
24+
*/
25+
private int httpConnectionTimeout = 5000;
26+
27+
/**
28+
* http请求数据读取等待时间
29+
*/
30+
private int httpTimeout = 10000;
31+
2132
private String appId;
2233
private String subAppId;
2334
private String mchId;
@@ -194,4 +205,26 @@ public SSLContext initSSLContext() throws WxPayException {
194205
IOUtils.closeQuietly(inputStream);
195206
}
196207
}
208+
209+
/**
210+
* http请求连接超时时间
211+
*/
212+
public int getHttpConnectionTimeout() {
213+
return this.httpConnectionTimeout;
214+
}
215+
216+
public void setHttpConnectionTimeout(int httpConnectionTimeout) {
217+
this.httpConnectionTimeout = httpConnectionTimeout;
218+
}
219+
220+
/**
221+
* http请求数据读取等待时间
222+
*/
223+
public int getHttpTimeout() {
224+
return this.httpTimeout;
225+
}
226+
227+
public void setHttpTimeout(int httpTimeout) {
228+
this.httpTimeout = httpTimeout;
229+
}
197230
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -465,9 +465,11 @@ public String getSandboxSignKey() throws WxPayException {
465465
* @return 返回请求结果
466466
*/
467467
private String post(String url, String xmlParam) throws WxPayException {
468-
String requestString = new String(xmlParam.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
469-
470-
HttpRequest request = HttpRequest.post(url).body(requestString);
468+
HttpRequest request = HttpRequest
469+
.post(url)
470+
.timeout(this.config.getHttpTimeout())
471+
.connectionTimeout(this.config.getHttpConnectionTimeout())
472+
.body(new String(xmlParam.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
471473
String responseString = this.getResponseString(request.send());
472474

473475
this.log.info("\n【请求地址】: {}\n【请求参数】:{}\n【响应数据】:{}", url, xmlParam, responseString);
@@ -489,6 +491,8 @@ private String postWithKey(String url, String requestStr) throws WxPayException
489491

490492
HttpRequest request = HttpRequest
491493
.post(url)
494+
.timeout(this.config.getHttpTimeout())
495+
.connectionTimeout(this.config.getHttpConnectionTimeout())
492496
.withConnectionProvider(new SSLSocketHttpConnectionProvider(sslContext))
493497
.bodyText(requestStr);
494498

0 commit comments

Comments
 (0)