简体   繁体   English

Apache Http EntityUtils.consume() 与 EntityUtils.toString()?

[英]Apache Http EntityUtils.consume() vs EntityUtils.toString()?

I have written a HTTP client, where I am reading the data response from a REST web service.我写了一个 HTTP 客户端,我正在读取来自 REST web 服务的数据响应。 My confusion arises after reading multiple blogs on EntityUtils.consume() and EntiryUtils.toString().在阅读了有关 EntityUtils.consume() 和 EntiryUtils.toString() 的多个博客后,我产生了困惑。 I wanted to know the following:我想知道以下内容:

  1. If EntityUtils.toString(..) ONLY is sufficient as it also closes the stream after reading char bytes.如果 EntityUtils.toString(..) ONLY 就足够了,因为它还会在读取 char 字节后关闭 stream。 Or I should also do EntityUtils.consume(..) as a good practice.或者我也应该做 EntityUtils.consume(..) 作为一个好习惯。

  2. If both toString() and consume() operation can be used.如果toString() 和consume() 操作都可以使用。 If yes, then what should be there order.如果是,那么应该有什么命令。

  3. If I EntityUtils.toString() closes the stream;如果我 EntityUtils.toString() 关闭 stream; then why the next call in EntityUtils.consume(..) operations which is entity.isStreaming() still returns true?那么为什么 EntityUtils.consume(..) 操作中的下一个调用是 entity.isStreaming() 仍然返回 true?

Could anyone guide me here to use these operations in a standard way.任何人都可以在这里指导我以标准方式使用这些操作。 I am using HTTP version 4+.我正在使用 HTTP 版本 4+。

I have to use these configurations in multithreaded(web-app) environment.我必须在多线程(网络应用程序)环境中使用这些配置。

Thanks谢谢

I looked at the recommended example from the apache httpclient commons website.我查看了来自 apache httpclient commons 网站的推荐示例。

In the example, they used EntityUtils.toString(..) without needing to use EntityUtils.consume(..) before or after.在示例中,他们使用了 EntityUtils.toString(..) 而无需在前后使用 EntityUtils.consume(..)。

They mention that calling httpclient.close() ensures all resources are closed.他们提到调用 httpclient.close() 可确保关闭所有资源。

source: https://hc.apache.org/httpcomponents-client-ga/httpclient/examples/org/apache/http/examples/client/ClientWithResponseHandler.java来源: https://hc.apache.org/httpcomponents-client-ga/httpclient/examples/org/apache/http/examples/client/ClientWithResponseHandler.Z93F725A07423FE1C889FZ448B3

CloseableHttpClient httpclient = HttpClients.createDefault();
    try {
        HttpGet httpget = new HttpGet("http://httpbin.org/");

        System.out.println("Executing request " + httpget.getRequestLine());

        // Create a custom response handler
        ResponseHandler<String> responseHandler = new ResponseHandler<String>() {

            @Override
            public String handleResponse(
                    final HttpResponse response) throws ClientProtocolException, IOException {
                int status = response.getStatusLine().getStatusCode();
                if (status >= 200 && status < 300) {
                    HttpEntity entity = response.getEntity();
                    return entity != null ? EntityUtils.toString(entity) : null;
                } else {
                    throw new ClientProtocolException("Unexpected response status: " + status);
                }
            }

        };
        String responseBody = httpclient.execute(httpget, responseHandler);
        System.out.println("----------------------------------------");
        System.out.println(responseBody);
    } finally {
        httpclient.close();
    }

This is what is quoted for the above example:这是上面示例中引用的内容:

This example demonstrates how to process HTTP responses using a response handler.此示例演示如何使用响应处理程序处理 HTTP 响应。 This is the recommended way of executing HTTP requests and processing HTTP responses.这是执行 HTTP 请求和处理 HTTP 响应的推荐方式。 This approach enables the caller to concentrate on the process of digesting HTTP responses and to delegate the task of system resource deallocation to HttpClient.这种方法使调用者能够专注于消化 HTTP 响应的过程,并将系统资源释放的任务委托给 HttpClient。 The use of an HTTP response handler guarantees that the underlying HTTP connection will be released back to the connection manager automatically in all cases.使用 HTTP 响应处理程序可确保在所有情况下,底层 HTTP 连接将自动释放回连接管理器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM