Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【Help】使用demo中的例子上传图片返回400 badrequest #188

Closed
zzmloveddm opened this issue Feb 15, 2023 · 4 comments
Closed

【Help】使用demo中的例子上传图片返回400 badrequest #188

zzmloveddm opened this issue Feb 15, 2023 · 4 comments

Comments

@zzmloveddm
Copy link

可否帮忙看下这个是啥情况
代码如下:
PrivateKey merchantPrivateKey = PemUtil.loadPrivateKey(Files.newInputStream(Paths.get(PrivateKeyPath)));
//使用自动更新的签名验证器,不需要传入证书
verifier = new AutoUpdateCertificatesVerifier(
new WechatPay2Credentials(tMerchantId, new PrivateKeySigner(MerchantSerialNumber, merchantPrivateKey)),
ApiV3key.getBytes(StandardCharsets.UTF_8));
httpClient = WechatPayHttpClientBuilder.create()
.withMerchant(MerchantId, MerchantSerialNumber, merchantPrivateKey)
.withValidator(new WechatPay2Validator(verifier))
.build();
URI uri = new URI("https://api.mch.weixin.qq.com/v3/merchant/media/upload");
byte[] bytes = file.getBytes();
try (InputStream byteArrayInputStream = new ByteArrayInputStream(bytes)) {
String sha256 = DigestUtils.sha256Hex(byteArrayInputStream);
WechatPayUploadHttpPost request = new WechatPayUploadHttpPost.Builder(uri)
.withImage(file.getOriginalFilename(), sha256, byteArrayInputStream)
.build();
Header[] allHeaders = request.getAllHeaders();
for (Header allHeader : allHeaders) {
System.out.println(allHeader);
}
try (CloseableHttpResponse response = httpClient.execute(request)) {
if (SC_OK == response.getStatusLine().getStatusCode()){
HttpEntity entity = response.getEntity();
// do something useful with the response body
// and ensure it is fully consumed
String s = EntityUtils.toString(entity);
System.out.println(s);
return success;
}else {
return error;
}
}
}
返回400 badrequest

@xy-peng
Copy link
Contributor

xy-peng commented Feb 15, 2023

你看下服务器返回的错误码和错误信息?

@zzmloveddm
Copy link
Author

你看下服务器返回的错误码和错误信息?

应答的状态码不为200-299。status code[400] request headers[[Accept: application/json; charset=UTF-8, Authorization: WECHATPAY2-SHA256-RSA2048 mchid="我的appId",nonce_str="xOWyRfSwKYc63dPWQjxntu9ASfdEjJDy",timestamp="1676433245",serial_no="我的证书序列号",signature="nz0UdFsD0pRw5g4/kYnB0sCvdky8mmG+ZvrerZ68Wj8NTmFNZ2bDebmGneUQtu6Mvdeif1dOpBkzM+DpGra6cbkhQnEO1zSZRtIuo1wbLp9lo/oSIwDQXf5h81YDyvmSTDJ7x5qwI1YbTVVALLu4vDjILkEoNjNTHQfGEYbBeJey39DBuS/5hgIHDCDvESa6tofaX+Ox1onqmQgcqrG6+nvm6B1t1VGH5zA62Q4JiAbQ4gU6z7X108WEoETbuOkjIWDYUZ78bXkxvEEgqCWCV51DrybZ9QLxRRlWokK9dk8pNnnI8vA4Xa9CwtLoGyu07Co8p8NElN1P1uQshniUdg==", Transfer-Encoding: chunked, Content-Type: multipart/form-data; boundary=tZYqbmWlPGw2UR6hFQMjgSvraLAaDBKhEy, Host: api.mch.weixin.qq.com, Connection: Keep-Alive, User-Agent: WechatPay-Apache-HttpClient/0.4.9 (Windows 10/10.0) Java/1.8.0_152, Accept-Encoding: gzip,deflate]]

@xy-peng
Copy link
Contributor

xy-peng commented Feb 15, 2023

400只是状态码。你需要把应答流收回来,输出 body,看到错误码和错误信息。它们可以用来定位问题。

@zzmloveddm
Copy link
Author

400只是状态码。你需要把应答流收回来,输出 body,看到错误码和错误信息。它们可以用来定位问题。

好的谢谢

@zzmloveddm zzmloveddm changed the title 使用demo中的例子上传图片返回400 badrequest 【Help】使用demo中的例子上传图片返回400 badrequest Feb 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants