<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx</center>
</body>
----------------------------------------------------各种帖子,各种解决方案 排除八百遍了,各位大佬帮帮忙吧--------------------------------
官方提供的工具类
public String getToken(String method, HttpUrl url, String body,String nonceStr,long timestamp) {
String message = buildMessage(method, url, timestamp, nonceStr, body);
String signature = null;
try {
signature = sign(message.getBytes("utf-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return "mchid=\"" + mchId + "\","
+ "nonce_str=\"" + nonceStr + "\","
+ "timestamp=\"" + timestamp + "\","
+ "serial_no=\"" + mchSerNo + "\","
+ "signature=\"" + signature + "\"";
}
public String sign(byte[] message) {
Signature sign = null;
try {
sign = Signature.getInstance("SHA256withRSA");
sign.initSign(getPrivateKey());
sign.update(message);
return Base64.getEncoder().encodeToString(sign.sign());
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
public String buildMessage(String method, HttpUrl url, long timestamp, String nonceStr, String body) {
String canonicalUrl = url.encodedPath();
if (url.encodedQuery() != null) {
canonicalUrl += "?" + url.encodedQuery();
}
return method + "\n"
+ canonicalUrl + "\n"
+ timestamp + "\n"
+ nonceStr + "\n"
+ body + "\n";
}
请求代码
//请求URL
String url = "https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi";
//先签名
long timestamp = Sha1Util.getTimeStamp();
// 请求body参数
String reqdata = "{"
+ "\"amount\": {"
+ "\"total\": "+totalFee+","
+ "\"currency\": \"CNY\""
+ "},"
+ "\"mchid\": \""+mch_id+"\","
+ "\"description\": \"商家\","
+ "\"notify_url\": \""+notify_url+"\","
+ "\"payer\": {"
+ "\"openid\": \""+openid+"\"},"
+ "\"out_trade_no\": \""+orderNo+"\","
+ "\"appid\": \"wx80c666666775022\"" + "}";
HttpUrl httpurl = HttpUrl.parse(url);
String signStr = wxPayUtil.buildMessage("POST",httpurl,timestamp,nonce_str,reqdata);
System.out.println(signStr);
//签名
String finalsign=wxPayUtil.sign(signStr.getBytes(StandardCharsets.UTF_8));
//得到token
String token = wxPayUtil.getToken("POST",httpurl,reqdata,nonce_str,timestamp);
HttpPost httpPost = new HttpPost(url);
//加上这个请求头就报错400 不加请求头可以得到prepay_id 但是签名验证错误,头大啊
httpPost.setHeader("Authorization","WECHATPAY2-SHA256-RSA2048" + " " + token);
System.out.println("WECHATPAY2-SHA256-RSA2048 "+token);
//打印的Authorization WECHATPAY2-SHA256-RSA2048 mchid="1608126666",nonce_str="0035415424",timestamp="1621442141",serial_no="6207B9E171818239AC04AD7F35BB3DFFFFFA03A0",signature="AIAOuX63FYHDHKohR2FFOAuvySJUH05dBEZzXWVsSj3l8BpqvjLIARwDOAbAdLH1GM1+VWCqTroRGDLTHy8vlx/oiDoRHv+Be8xPWWkoJmE5DYnuOYK9u0mhzuiOeJyYIpFELxh1cRaEZGtZN75qb3dZ7vpvwIrrbz3RJnvvErzeUVGtAwuEh1vu+4dgccfgKIO+NNFgY1QIpQ8EJAgK4B+1IdjawTctwmzWSZDrZUA2H0r9tFLyYr5G0PgaYKHZxeNq8YTk87fc1iRPQ43occ5z928iX27F3Cx8UrrsrsbwwcmJyl3iHHHcf4OSd83o1rT+C+uMheg4yha0m47Gzg=="
StringEntity entity = new StringEntity(reqdata,"UTF-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);
httpPost.setHeader("Accept", "text/html, application/xhtml+xml, image/jxr, */*");
String prepay_id = "";
CloseableHttpResponse resultPay = httpClient.execute(httpPost);
400一般是参数或者使用证书错误