java—使用apache异步http客户端从inputstream构造多部分请求

goucqfw6  于 2021-07-09  发布在  Java
关注(0)|答案(0)|浏览(286)

我试图通过apache异步http客户机发送一个多部分请求,但是得到一个 org.apache.http.ContentTooLongException: Content length is unknown 错误。
我确实理解异步模型中为什么需要内容长度,但我想知道这是设计完成的还是一个bug?
我希望它将流加载到内存缓冲区并使用它(这就是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(simplehttpclientforjava)的作者,它致力于为常见的http客户机用例提供统一的facade。我可以在facade级别上修复它,但更喜欢尽可能地保持它的逻辑性。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题