簡體   English   中英

如何以流形式獲取httpclient響應

[英]How to get httpclient response as stream

我正在使用httpclient 4.5.5,我想獲取高達1 GB的大文件作為響應。 但是好像

CloseableHttpResponse response = httpClient.execute(httpGet); HttpEntity entity = response.getEntity();

這將返回完整響應,因此在內存中具有完整響應不是很好。 有什么辦法可以讓響應作為流?

自版本4.0起的Apache HttpClient(以及Apache HttpAsyncClient)支持傳入和傳出HTTP消息的完整內容流。 使用HttpEntity來訪問基礎內容輸入流

CloseableHttpClient client = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("http://myhost/tons-of-stuff");
try (CloseableHttpResponse response1 = client.execute(httpGet)) {
    final HttpEntity entity = response1.getEntity();
    if (entity != null) {
        try (InputStream inputStream = entity.getContent()) {
            // do something useful with the stream
        }
    }
}

使用HttpURLConnection代替httpClient

final HttpURLConnection conn = (HttpURLConnection)url.openConnection();
final int bufferSize = 1024 * 1024;
conn.setChunkedStreamingMode(bufferSize);
final OutputStream out = conn.getOutputStream();

您需要Apache Async Client。

HttpAsyncClient是Apache HttpClient的ASYNC版本。 Apache HttpClient在內存中構造整個響應,而使用HttpAsyncClient時,您可以定義一個處理程序(消費者)以在接收數據時處理響應。

https://hc.apache.org/httpcomponents-asyncclient-4.1.x/index.html

這是他們官方示例代碼中的一個示例

package org.apache.http.examples.nio.client;

import java.io.IOException;
import java.nio.CharBuffer;
import java.util.concurrent.Future;

import org.apache.http.HttpResponse;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClients;
import org.apache.http.nio.IOControl;
import org.apache.http.nio.client.methods.AsyncCharConsumer;
import org.apache.http.nio.client.methods.HttpAsyncMethods;
import org.apache.http.protocol.HttpContext;

/**
 * This example demonstrates an asynchronous HTTP request / response exchange with
 * a full content streaming.
 */
public class AsyncClientHttpExchangeStreaming {

    public static void main(final String[] args) throws Exception {
        CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
        try {
            httpclient.start();
            Future<Boolean> future = httpclient.execute(
                    HttpAsyncMethods.createGet("http://httpbin.org/"),
                    new MyResponseConsumer(), null);
            Boolean result = future.get();
            if (result != null && result.booleanValue()) {
                System.out.println("Request successfully executed");
            } else {
                System.out.println("Request failed");
            }
            System.out.println("Shutting down");
        } finally {
            httpclient.close();
        }
        System.out.println("Done");
    }

    static class MyResponseConsumer extends AsyncCharConsumer<Boolean> {

        @Override
        protected void onResponseReceived(final HttpResponse response) {
        }

        @Override
        protected void onCharReceived(final CharBuffer buf, final IOControl ioctrl) throws IOException {
            while (buf.hasRemaining()) {
                System.out.print(buf.get());
            }
        }

        @Override
        protected void releaseResources() {
        }

        @Override
        protected Boolean buildResult(final HttpContext context) {
            return Boolean.TRUE;
        }

    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM