相关疑难解决方法(0)

为什么作者使用EntityUtils.consume(httpEntity);?

我遇到过EntityUtils.consume(httpEntity);,我不确定它到底是做什么的.

例如:

try {

    //... some code

    HttpEntity httpEntity = httpResponse.getEntity();
    BufferedReader br = new BufferedReader(new InputStreamReader(http.Entity.getContent()));
    String line;
    while ((line = br.readLine())!= null) {
        System.out.println(line);
    }
    EntityUtils.consume(httpEntity);
} catch (Exception e) {
    //code
} finally { 
    httpClient.getConnectionManager().shutdown();
}
Run Code Online (Sandbox Code Playgroud)

为什么作者在块关闭连接EntityUtils.consume(httpEntity);时放入finally并且垃圾收集器会处理httpEntity?

java apache-httpclient-4.x

33
推荐指数
1
解决办法
2万
查看次数

标签 统计

apache-httpclient-4.x ×1

java ×1