//官方提供的初始化客户端方法 无法通过 nginx
//privateKeyPath 私钥路径
String privateKey = new String(Files.readAllBytes(Paths.get(privateKeyPath)), "utf-8");
PrivateKey merchantPrivateKey = PemUtil.loadPrivateKey(privateKey);
//mchId 商户号
//wechatPayserialNo 证书序列号
//apiV3Key 支付密码
CertificatesManager instance = CertificatesManager.getInstance();
instance.putMerchant(mchId, new WechatPay2Credentials(mchId,
new PrivateKeySigner(wechatPayserialNo, merchantPrivateKey)),
apiV3Key.getBytes(StandardCharsets.UTF_8));
Verifier verifier = instance.getVerifier(mchId);
httpclient = WechatPayHttpClientBuilder.create()
.withMerchant(mchId, wechatPayserialNo, merchantPrivateKey)
.withValidator(new WechatPay2Validator(verifier))
.build();
此方法返回
400 Bad Request 400 Bad Request
nginx
阿帕奇默认创建客户端 可以通过请求
CloseableHttpClient httpClient = HttpClients.createDefault();
请问各路大神,导致这种情况的原因可能是什么?
curl -v https://api.mch.weixin.qq.com/ * Rebuilt URL to: https://api.mch.weixin.qq.com/ * Trying 101.91.0.140... * TCP_NODELAY set * Connected to api.mch.weixin.qq.com (101.91.0.140) port 443 (#0) * ALPN, offering h2 * ALPN, offering http/1.1 * Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH * successfully set certificate verify locations: * CAfile: /etc/ssl/cert.pem CApath: none * TLSv1.2 (OUT), TLS handshake, Client hello (1): * TLSv1.2 (IN), TLS handshake, Server hello (2): * TLSv1.2 (IN), TLS handshake, Certificate (11): * TLSv1.2 (IN), TLS handshake, Server key exchange (12): * TLSv1.2 (IN), TLS handshake, Request CERT (13): * TLSv1.2 (IN), TLS handshake, Server finished (14): * TLSv1.2 (OUT), TLS handshake, Certificate (11): * TLSv1.2 (OUT), TLS handshake, Client key exchange (16): * TLSv1.2 (OUT), TLS change cipher, Client hello (1): * TLSv1.2 (OUT), TLS handshake, Finished (20): * TLSv1.2 (IN), TLS change cipher, Client hello (1): * TLSv1.2 (IN), TLS handshake, Finished (20): * SSL connection using TLSv1.2 / ECDHE-RSA-AES256-GCM-SHA384 * ALPN, server accepted to use http/1.1 * Server certificate: * subject: C=CN; ST=Guangdong Province; L=Shenzhen; O=Tenpay Technology Company Limited; CN=payapp.weixin.qq.com * start date: Sep 21 00:00:00 2022 GMT * expire date: Oct 21 23:59:59 2023 GMT * subjectAltName: host "api.mch.weixin.qq.com" matched cert's "*.mch.weixin.qq.com" * issuer: C=US; O=DigiCert Inc; CN=DigiCert Secure Site CN CA G3 * SSL certificate verify ok. > GET / HTTP/1.1 > Host: api.mch.weixin.qq.com > User-Agent: curl/7.54.0 > Accept: */* > < HTTP/1.1 404 Not Found < Server: nginx < Date: Tue, 08 Nov 2022 03:51:23 GMT < Content-Type: text/html < Content-Length: 162 < Connection: keep-alive < Keep-Alive: timeout=8 < <html> <head><title>404 Not Found</title></head> <body bgcolor="white"> <center><h1>404 Not Found</h1></center> <hr><center>nginx</center> </body> </html> * Connectio #0 to host api.mch.weixin.qq.com left intact
你请求的URL不存在的话,默认返回就是404,例如上述 curl 命令行回显一样。
请求地址 https://api.mch.weixin.qq.com/v3/transfer/batches
HttpPost httpPost = new HttpPost(requestUrl);
httpPost.addHeader(ACCEPT, APPLICATION_JSON.toString());
httpPost.addHeader(CONTENT_TYPE, APPLICATION_JSON.toString());
httpPost.addHeader(WECHAT_PAY_SERIAL, wechatPayserialNo);
•
//-------------------------核心认证 start-----------------------------------------------------------------
String strToken = null;
try {
log.info("requestJson:{}", requestJson);
strToken = VechatPayV3Util.getToken(requestType,
url,
requestJson, mchId, wechatPayserialNo, privatekeypath);
} catch (Exception e) {
log.error("createHttpPost error:", e);
e.printStackTrace();
}
StringEntity reqEntity = new StringEntity(requestJson, APPLICATION_JSON);
log.info("token " + strToken);
// 添加认证信息
httpPost.addHeader("Authorization",
"WECHATPAY2-SHA256-RSA2048" + " "
+ strToken);
//---------------------------核心认证 end---------------------------------------------------------------
httpPost.setEntity(reqEntity);
//创建 httpost
HttpPost httpPost = createHttpPost(requestUrl, requestJson, wechatPayserialNo, mchId, privatekeypath, requestType, url);
//第一个初始化客户端方法 无法正常请求
//httpClient = createHttpClient(wechatPayserialNo, mchId,apiV3key);
//可以正常请求
httpClient = HttpClients.createDefault();
//发起请求 --- 成功转账的
httpClient.execute(httpPost);