🎨 #3745 【微信支付】修复请求微信仿真测试系统时验签密钥接口的 Content-Type 问题
This commit is contained in:
committed by
GitHub
parent
b9b4f00438
commit
b26c786797
@@ -109,6 +109,19 @@ public interface WxPayService {
|
|||||||
*/
|
*/
|
||||||
String post(String url, String requestStr, boolean useKey) throws WxPayException;
|
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请求,得到响应字符串.
|
* 发送post请求,得到响应字符串.
|
||||||
*
|
*
|
||||||
@@ -1457,6 +1470,7 @@ public interface WxPayService {
|
|||||||
* 是否需要证书: 否
|
* 是否需要证书: 否
|
||||||
* 请求方式: POST
|
* 请求方式: POST
|
||||||
* 文档地址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=23_1
|
* 文档地址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=23_1
|
||||||
|
* 注意: 微信暂不支持api v3
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
* @return the sandbox sign key
|
* @return the sandbox sign key
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
import me.chanjar.weixin.common.error.WxRuntimeException;
|
import me.chanjar.weixin.common.error.WxRuntimeException;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.apache.commons.lang3.reflect.ConstructorUtils;
|
import org.apache.commons.lang3.reflect.ConstructorUtils;
|
||||||
|
import org.apache.http.entity.ContentType;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -1262,7 +1263,7 @@ public abstract class BaseWxPayServiceImpl implements WxPayService {
|
|||||||
request.checkAndSign(this.getConfig());
|
request.checkAndSign(this.getConfig());
|
||||||
|
|
||||||
String url = "https://api.mch.weixin.qq.com/xdc/apiv2getsignkey/sign/getsignkey";
|
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);
|
WxPaySandboxSignKeyResult result = BaseWxPayResult.fromXML(responseContent, WxPaySandboxSignKeyResult.class);
|
||||||
result.checkResult(this, request.getSignType(), true);
|
result.checkResult(this, request.getSignType(), true);
|
||||||
return result.getSandboxSignKey();
|
return result.getSandboxSignKey();
|
||||||
|
|||||||
@@ -94,6 +94,32 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@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);
|
||||||
|
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
|
@Override
|
||||||
public String postV3(String url, String requestStr) throws WxPayException {
|
public String postV3(String url, String requestStr) throws WxPayException {
|
||||||
HttpPost httpPost = this.createHttpPost(url, requestStr);
|
HttpPost httpPost = this.createHttpPost(url, requestStr);
|
||||||
@@ -306,6 +332,10 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
|
|||||||
//return new StringEntity(new String(requestStr.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
|
//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 {
|
private HttpClientBuilder createHttpClientBuilder(boolean useKey) throws WxPayException {
|
||||||
HttpClientBuilder httpClientBuilder = HttpClients.custom();
|
HttpClientBuilder httpClientBuilder = HttpClients.custom();
|
||||||
if (useKey) {
|
if (useKey) {
|
||||||
@@ -348,6 +378,19 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
|
|||||||
return httpPost;
|
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 {
|
private void initSSLContext(HttpClientBuilder httpClientBuilder) throws WxPayException {
|
||||||
SSLContext sslContext = this.getConfig().getSslContext();
|
SSLContext sslContext = this.getConfig().getSslContext();
|
||||||
if (null == sslContext) {
|
if (null == sslContext) {
|
||||||
|
|||||||
@@ -91,6 +91,32 @@ public class WxPayServiceHttpComponentsImpl extends BaseWxPayServiceImpl {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@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
|
@Override
|
||||||
public String postV3(String url, String requestStr) throws WxPayException {
|
public String postV3(String url, String requestStr) throws WxPayException {
|
||||||
HttpPost httpPost = this.createHttpPost(url, requestStr);
|
HttpPost httpPost = this.createHttpPost(url, requestStr);
|
||||||
@@ -283,6 +309,11 @@ public class WxPayServiceHttpComponentsImpl extends BaseWxPayServiceImpl {
|
|||||||
//return new StringEntity(new String(requestStr.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
|
//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 {
|
private HttpClientBuilder createHttpClientBuilder(boolean useKey) throws WxPayException {
|
||||||
HttpClientBuilder httpClientBuilder = HttpClients.custom();
|
HttpClientBuilder httpClientBuilder = HttpClients.custom();
|
||||||
if (useKey) {
|
if (useKey) {
|
||||||
@@ -325,6 +356,19 @@ public class WxPayServiceHttpComponentsImpl extends BaseWxPayServiceImpl {
|
|||||||
return httpPost;
|
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 {
|
private void initSSLContext(HttpClientBuilder httpClientBuilder) throws WxPayException {
|
||||||
SSLContext sslContext = this.getConfig().getSslContext();
|
SSLContext sslContext = this.getConfig().getSslContext();
|
||||||
if (null == sslContext) {
|
if (null == sslContext) {
|
||||||
|
|||||||
@@ -63,6 +63,24 @@ public class WxPayServiceJoddHttpImpl extends BaseWxPayServiceImpl {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@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
|
@Override
|
||||||
public String postV3(String url, String requestStr) throws WxPayException {
|
public String postV3(String url, String requestStr) throws WxPayException {
|
||||||
return null;
|
return null;
|
||||||
@@ -146,6 +164,40 @@ public class WxPayServiceJoddHttpImpl extends BaseWxPayServiceImpl {
|
|||||||
return request;
|
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 {
|
private String getResponseString(HttpResponse response) throws WxPayException {
|
||||||
try {
|
try {
|
||||||
log.debug("【微信服务器响应头信息】:\n{}", response.toString(false));
|
log.debug("【微信服务器响应头信息】:\n{}", response.toString(false));
|
||||||
|
|||||||
Reference in New Issue
Block a user