Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,19 @@ public interface WxPayService {
*/
String post(String url, String requestStr, boolean useKey) throws WxPayException;


/**
* 发送post请求,得到响应字符串.
*
* @param url 请求地址
* @param requestStr 请求信息
* @param useKey 是否使用证书
* @param mimeType Content-Type请求头
* @return 返回请求结果字符串 string
* @throws WxPayException the wx pay exception
*/
String post(String url, String requestStr, boolean useKey, String mimeType) throws WxPayException;

/**
* 发送post请求,得到响应字符串.
*
Expand Down Expand Up @@ -1457,6 +1470,7 @@ WxPayRefundQueryResult refundQuery(String transactionId, String outTradeNo, Stri
* 是否需要证书: 否
* 请求方式: POST
* 文档地址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=23_1
* 注意: 微信暂不支持api v3
* </pre>
*
* @return the sandbox sign key
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import me.chanjar.weixin.common.error.WxRuntimeException;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.reflect.ConstructorUtils;
import org.apache.http.entity.ContentType;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -1262,7 +1263,7 @@ public String getSandboxSignKey() throws WxPayException {
request.checkAndSign(this.getConfig());

String url = "https://api.mch.weixin.qq.com/xdc/apiv2getsignkey/sign/getsignkey";
String responseContent = this.post(url, request.toXML(), false);
String responseContent = this.post(url, request.toXML(), false, ContentType.APPLICATION_XML.getMimeType());
WxPaySandboxSignKeyResult result = BaseWxPayResult.fromXML(responseContent, WxPaySandboxSignKeyResult.class);
result.checkResult(this, request.getSignType(), true);
return result.getSandboxSignKey();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public byte[] postForBytes(String url, String requestStr, boolean useKey) throws
try {
HttpPost httpPost = this.createHttpPost(url, requestStr);
CloseableHttpClient httpClient = this.createHttpClient(useKey);

// 使用连接池的客户端,不需要手动关闭
final byte[] bytes = httpClient.execute(httpPost, ByteArrayResponseHandler.INSTANCE);
final String responseData = Base64.getEncoder().encodeToString(bytes);
Expand All @@ -73,7 +73,33 @@ public String post(String url, String requestStr, boolean useKey) throws WxPayEx
try {
HttpPost httpPost = this.createHttpPost(url, requestStr);
CloseableHttpClient httpClient = this.createHttpClient(useKey);


// 使用连接池的客户端,不需要手动关闭
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
String responseString = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
this.logRequestAndResponse(url, requestStr, responseString);
if (this.getConfig().isIfSaveApiData()) {
wxApiData.set(new WxPayApiData(url, requestStr, responseString, null));
}
return responseString;
} finally {
httpPost.releaseConnection();
}
} catch (Exception e) {
this.logError(url, requestStr, e);
if (this.getConfig().isIfSaveApiData()) {
wxApiData.set(new WxPayApiData(url, requestStr, null, e.getMessage()));
}
throw new WxPayException(e.getMessage(), e);
}
}

@Override
public String post(String url, String requestStr, boolean useKey, String mimeType) throws WxPayException {
try {
HttpPost httpPost = this.createHttpPost(url, requestStr, mimeType);
CloseableHttpClient httpClient = this.createHttpClient(useKey);

// 使用连接池的客户端,不需要手动关闭
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
String responseString = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
Expand Down Expand Up @@ -306,6 +332,10 @@ private static StringEntity createEntry(String requestStr) {
//return new StringEntity(new String(requestStr.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
}

private static StringEntity createEntry(String requestStr, String mimeType) {
return new StringEntity(requestStr, ContentType.create(mimeType, StandardCharsets.UTF_8));
//return new StringEntity(new String(requestStr.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
}
private HttpClientBuilder createHttpClientBuilder(boolean useKey) throws WxPayException {
HttpClientBuilder httpClientBuilder = HttpClients.custom();
if (useKey) {
Expand Down Expand Up @@ -348,6 +378,19 @@ private HttpPost createHttpPost(String url, String requestStr) {
return httpPost;
}

private HttpPost createHttpPost(String url, String requestStr, String mimeType) throws WxPayException {
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(createEntry(requestStr, mimeType));

httpPost.setConfig(RequestConfig.custom()
.setConnectionRequestTimeout(this.getConfig().getHttpConnectionTimeout())
.setConnectTimeout(this.getConfig().getHttpConnectionTimeout())
.setSocketTimeout(this.getConfig().getHttpTimeout())
.build());

return httpPost;
}

private void initSSLContext(HttpClientBuilder httpClientBuilder) throws WxPayException {
SSLContext sslContext = this.getConfig().getSslContext();
if (null == sslContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,32 @@ public String post(String url, String requestStr, boolean useKey) throws WxPayEx
}
}

@Override
public String post(String url, String requestStr, boolean useKey, String mimeType) throws WxPayException {
try {
HttpClientBuilder httpClientBuilder = this.createHttpClientBuilder(useKey);
HttpPost httpPost = this.createHttpPost(url, requestStr, mimeType);
try (CloseableHttpClient httpClient = httpClientBuilder.build()) {
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
String responseString = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
this.logRequestAndResponse(url, requestStr, responseString);
if (this.getConfig().isIfSaveApiData()) {
wxApiData.set(new WxPayApiData(url, requestStr, responseString, null));
}
return responseString;
}
} finally {
httpPost.releaseConnection();
}
} catch (Exception e) {
this.logError(url, requestStr, e);
if (this.getConfig().isIfSaveApiData()) {
wxApiData.set(new WxPayApiData(url, requestStr, null, e.getMessage()));
}
throw new WxPayException(e.getMessage(), e);
}
}

@Override
public String postV3(String url, String requestStr) throws WxPayException {
HttpPost httpPost = this.createHttpPost(url, requestStr);
Expand Down Expand Up @@ -283,6 +309,11 @@ private static StringEntity createEntry(String requestStr) {
//return new StringEntity(new String(requestStr.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
}

private static StringEntity createEntry(String requestStr, String mimeType) {
return new StringEntity(requestStr, ContentType.create(mimeType, StandardCharsets.UTF_8));
//return new StringEntity(new String(requestStr.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
}

private HttpClientBuilder createHttpClientBuilder(boolean useKey) throws WxPayException {
HttpClientBuilder httpClientBuilder = HttpClients.custom();
if (useKey) {
Expand Down Expand Up @@ -325,6 +356,19 @@ private HttpPost createHttpPost(String url, String requestStr) {
return httpPost;
}

private HttpPost createHttpPost(String url, String requestStr, String mimeType) throws WxPayException {
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(createEntry(requestStr, mimeType));

httpPost.setConfig(RequestConfig.custom()
.setConnectionRequestTimeout(this.getConfig().getHttpConnectionTimeout())
.setConnectTimeout(this.getConfig().getHttpConnectionTimeout())
.setSocketTimeout(this.getConfig().getHttpTimeout())
.build());

return httpPost;
}

private void initSSLContext(HttpClientBuilder httpClientBuilder) throws WxPayException {
SSLContext sslContext = this.getConfig().getSslContext();
if (null == sslContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,24 @@ public String post(String url, String requestStr, boolean useKey) throws WxPayEx
}
}

@Override
public String post(String url, String requestStr, boolean useKey, String mimeType) throws WxPayException {
try {
HttpRequest request = this.buildHttpRequest(url, requestStr, useKey, mimeType);
String responseString = this.getResponseString(request.send());

log.info("\n【请求地址】:{}\n【请求数据】:{}\n【响应数据】:{}", url, requestStr, responseString);
if (this.getConfig().isIfSaveApiData()) {
wxApiData.set(new WxPayApiData(url, requestStr, responseString, null));
}
return responseString;
} catch (Exception e) {
log.error("\n【请求地址】:{}\n【请求数据】:{}\n【异常信息】:{}", url, requestStr, e.getMessage());
wxApiData.set(new WxPayApiData(url, requestStr, null, e.getMessage()));
throw new WxPayException(e.getMessage(), e);
}
}

@Override
public String postV3(String url, String requestStr) throws WxPayException {
return null;
Expand Down Expand Up @@ -146,6 +164,40 @@ private HttpRequest buildHttpRequest(String url, String requestStr, boolean useK
return request;
}

private HttpRequest buildHttpRequest(String url, String requestStr, boolean useKey, String mimeType) throws WxPayException {
HttpRequest request = HttpRequest
.post(url)
.contentType(mimeType)
.timeout(this.getConfig().getHttpTimeout())
.connectionTimeout(this.getConfig().getHttpConnectionTimeout())
.bodyText(requestStr);

if (useKey) {
SSLContext sslContext = this.getConfig().getSslContext();
if (null == sslContext) {
sslContext = this.getConfig().initSSLContext();
}
final SSLSocketHttpConnectionProvider provider = new SSLSocketHttpConnectionProvider(sslContext);
request.withConnectionProvider(provider);
}

if (StringUtils.isNotBlank(this.getConfig().getHttpProxyHost()) && this.getConfig().getHttpProxyPort() > 0) {
if (StringUtils.isEmpty(this.getConfig().getHttpProxyUsername())) {
this.getConfig().setHttpProxyUsername("whatever");
}

ProxyInfo httpProxy = new ProxyInfo(ProxyType.HTTP, this.getConfig().getHttpProxyHost(), this.getConfig().getHttpProxyPort(),
this.getConfig().getHttpProxyUsername(), this.getConfig().getHttpProxyPassword());
HttpConnectionProvider provider = request.connectionProvider();
if (null == provider) {
provider = new SocketHttpConnectionProvider();
}
provider.useProxy(httpProxy);
request.withConnectionProvider(provider);
}
return request;
}

private String getResponseString(HttpResponse response) throws WxPayException {
try {
log.debug("【微信服务器响应头信息】:\n{}", response.toString(false));
Expand Down