我正在尝试通过 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 客户端用例提供统一的外观。我可以在外观级别上修复它,但更愿意将其逻辑保持在尽可能小。