使用的官方示例中的写法报错,debug看request中content-length是-1。是这个的问题还是什么?
<dependency>
<groupId>com.github.wechatpay-apiv3</groupId>
<artifactId>wechatpay-apache-httpclient</artifactId>
<version>0.4.9</version>
</dependency>
报错信息:
org.apache.http.client.ClientProtocolException
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:186)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107)
at com.xg.boss.wx.spec.WxPayApiV3SpecServiceDefaultImpl.upload(WxPayApiV3SpecServiceDefaultImpl.java:171)
at com.xg.boss.merchant.MerchantController.getFileString(MerchantController.java:319)
at com.xg.boss.merchant.MerchantController.merchantJoinPay(MerchantController.java:216)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:175)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:446)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:434)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at com.xg.boss.filter.SessionFilter.doFilter(SessionFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:522)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1095)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.apache.http.client.NonRepeatableRequestException: Cannot retry request with a non-repeatable request entity
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:107)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
... 47 more
Caused by: org.apache.http.ContentTooLongException: Content length is unknown
at org.apache.http.entity.mime.MultipartFormEntity.getContent(MultipartFormEntity.java:101)
at org.apache.http.util.EntityUtils.toByteArray(EntityUtils.java:122)
at com.xg.boss.wx.httpclient.SignatureExec.newRepeatableEntity(SignatureExec.java:27)
at com.xg.boss.wx.httpclient.SignatureExec.convertToRepeatableRequestEntity(SignatureExec.java:46)
at com.xg.boss.wx.httpclient.SignatureExec.executeWithSignature(SignatureExec.java:64)
at com.xg.boss.wx.httpclient.SignatureExec.execute(SignatureExec.java:55)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
... 49 more
调用代码:
this.specConfig = specConfig;
this.certificate = PemUtil.loadCertificate(specConfig.getX509certificateInputStream());
this.privateKey = PemUtil.loadPrivateKey(specConfig.getPrivateKeyInputStream());
objectMapper.setPropertyNamingStrategy(com.fasterxml.jackson.databind.PropertyNamingStrategy.SNAKE_CASE);
try {
autoUpdateCertificatesVerifier = new AutoUpdateCertificatesVerifier(
new WechatPay2Credentials(specConfig.getMerchantId(), new PrivateKeySigner(specConfig.getMerchantSerialNumber(), this.privateKey), "boundary"),
specConfig.getApiV3Key().getBytes("utf-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
WechatPayHttpClientBuilder builder = WechatPayHttpClientBuilder.create()
.withMerchant(specConfig.getMerchantId(), specConfig.getMerchantSerialNumber(), this.privateKey)
.withValidator(new WechatPay2Validator(autoUpdateCertificatesVerifier));
this.httpClient = builder.build();
URI uri = new URI("https://api.mch.weixin.qq.com/v3/merchant/media/upload");
try (FileInputStream fileIs = new FileInputStream(file)) {
String sha256 = DigestUtils.sha256Hex(fileIs);
try (InputStream is = new FileInputStream(file)) {
WechatPayUploadHttpPost request = new WechatPayUploadHttpPost.Builder(uri)
.withImage(file.getName(), sha256, is)
.build();
try (CloseableHttpResponse response = httpClient.execute(request)) {
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 s;
} catch (Exception e) {
e.printStackTrace();
}
}