我正在尝试通过apache异步http客户端发送多部分请求,但遇到org.apache.http.ContentTooLongException: Content length is unknown
错误。
我确实理解了异步模型中为什么需要内容长度,但是想了解这是设计导致的还是错误的?
我希望它将流加载到内存缓冲区中并使用它(这是AsyncHttpClient所做的)。
我的测试用例是:
public class InputStreamAsMultipartPayloadTest {
@Rule
public WireMockClassRule instanceRule = new WireMockClassRule(9090);
@Test
public void inputStreamPartTest() throws Exception {
String partBody = "hello";
instanceRule.stubFor(any(urlPathEqualTo("/hello"))
.withMultipartRequestBody(
aMultipart()
.withName("part-name")
.withHeader("Content-Type", equalTo("application/octet-stream"))
.withHeader("Content-Transfer-Encoding", equalTo("binary"))
.withBody(equalTo(partBody)))
.willReturn(aResponse()));
byte[] body = partBody.getBytes(UTF_8);
try (CloseableHttpAsyncClient httpAsyncClient = HttpAsyncClients.createDefault()) {
httpAsyncClient.start();
HttpPost postRequest = new HttpPost(URI.create("/hello"));
MultipartEntityBuilder multipartBuilder = MultipartEntityBuilder.create();
multipartBuilder.addBinaryBody("part-name", new ByteArrayInputStream(body));
postRequest.setEntity(multipartBuilder.build());
Future<HttpResponse> fHttpResponse = httpAsyncClient.execute(HttpHost.create("http://localhost:9090"), postRequest, null);
assertThat(fHttpResponse.get(3, TimeUnit.SECONDS).getStatusLine().getStatusCode()).isEqualTo(HttpURLConnection.HTTP_OK);
}
}
}
异常堆栈跟踪为:
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.nio.entity.EntityAsyncContentProducer.produceContent(EntityAsyncContentProducer.java:65)
at org.apache.http.nio.protocol.BasicAsyncRequestProducer.produceContent(BasicAsyncRequestProducer.java:125)
at org.apache.http.impl.nio.client.MainClientExec.produceContent(MainClientExec.java:262)
at org.apache.http.impl.nio.client.DefaultClientExchangeHandlerImpl.produceContent(DefaultClientExchangeHandlerImpl.java:140)
at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.outputReady(HttpAsyncRequestExecutor.java:241)
at org.apache.http.impl.nio.DefaultNHttpClientConnection.produceOutput(DefaultNHttpClientConnection.java:290)
at org.apache.http.impl.nio.client.InternalIODispatch.onOutputReady(InternalIODispatch.java:86)
at org.apache.http.impl.nio.client.InternalIODispatch.onOutputReady(InternalIODispatch.java:39)
at org.apache.http.impl.nio.reactor.AbstractIODispatch.outputReady(AbstractIODispatch.java:145)
at org.apache.http.impl.nio.reactor.BaseIOReactor.writable(BaseIOReactor.java:188)
at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvent(AbstractIOReactor.java:341)
at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvents(AbstractIOReactor.java:315)
at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:276)
at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:104)
at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:588)
完整的源代码可以在以下网址找到:https://github.com/maximkir/apache-async-http
通常,我是SHCF4J(Java的简单HTTP客户端)的作者,该网站致力于为常见的HTTP客户端用例提供统一的外观。我可以在立面级别上对其进行修复,但更希望将其逻辑保持在最低限度。