0

一度にボディを生成するリクエストボディを送信したいのですが、事前に全体の長さを知りません。つまり、チャンクされたリクエストを送信する必要があります。

これを行う方法が見つかりません。

2 つのタイプの接続の主な違いは、通常の、ただし本質的にブロッキング クラスを使用して、インバウンドおよびアウトバウンド コンテンツのストリームを表すことができないことjava.io.InputStreamですjava.io.OutputStream。HttpCore NIO は、非同期コンテンツ転送のプロセスを処理するためのインターフェースを提供ContentEncoderします。ContentDecoder

...

非ブロッキング HTTP 接続は、コンテンツ エンティティが完全に転送されたとマークされるまで、出力イベントを発生させます。

ContentEncoder encoder = <...>
// Prepare output data
ByteBuffer src = ByteBuffer.allocate(2048);
// Write data out
encoder.write(src);
// Mark content entity as fully transferred when done
encoder.complete();

を見ましたorg.apache.http.nio.conn.ClientAsyncConnectionが、出力イベントが発生している場所がわかりません。

ファイル送信の例は見つかりますが、やりたいコンテンツ生成の例はありません。

を使用してチャンク化されたストリーミング リクエストを送信するにはどうすればよいAsyncHttpClientですか?

4

1 に答える 1

0

カスタムを実装し、提供されたいずれかを使用するか、独自のコンテンツ生成ロジックを実装してHttpAsyncRequestProducer、コンテンツをストリーミングすることができますHttpAsyncContentProducer

public class MyAsyncRequestProducer implements HttpAsyncRequestProducer {

    private final HttpHost target;
    private final HttpAsyncContentProducer producer;

    public MyAsyncRequestProducer(
            final HttpHost target,
            final HttpAsyncContentProducer producer) {
        super();
        Args.notNull(target, "HTTP host");
        Args.notNull(producer, "HTTP content producer");
        this.target = target;
        this.producer = producer;
    }

    public synchronized HttpRequest generateRequest() {
        BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("POST", "/");
        BasicHttpEntity entity = new BasicHttpEntity();
        entity.setContentType(ContentType.TEXT_PLAIN.toString());
        entity.setContentLength(-1);
        entity.setChunked(true);
        request.setEntity(entity);
        return request;
    }

    public HttpHost getTarget() {
        return this.target;
    }

    public synchronized void produceContent(
            final ContentEncoder encoder, final IOControl ioctrl) throws IOException {
        this.producer.produceContent(encoder, ioctrl);
        if (encoder.isCompleted()) {
            this.producer.close();
        }
    }

    public void requestCompleted(final HttpContext context) {
    }

    public void failed(final Exception ex) {
    }

    public synchronized boolean isRepeatable() {
        return this.producer.isRepeatable();
    }

    public synchronized void resetRequest() throws IOException {
        this.producer.close();
    }

    public synchronized void close() throws IOException {
        this.producer.close();
    }

}
于 2014-07-13T14:09:55.657 に答える