Apache HttpClient 没有收到完整的响应

Apache HttpClient not receiving entire response

更新: 如果我使用 System.out.println(EntityUtils.toString(response.getEntity())); 输出似乎是 HTML 的缺失行(包括结束 bodyhtml 标签)。但是,打印到一个文件仍然只给我前 2000 行缺少最后 1000 行。


我正在使用以下代码执行 http post 请求:

public static String Post(CloseableHttpClient httpClient, String url, Header[] headers,
            List<NameValuePair> data, HttpClientContext context) throws IOException
{
    HttpPost httpPost = new HttpPost(url);
    httpPost.setEntity(new UrlEncodedFormEntity(data));
    httpPost.setHeaders(headers);
    CloseableHttpResponse response = httpClient.execute(httpPost, context);

    if (response.getEntity() == null)
        throw new NullPointerException("Unable to get html for: " + url);

    // Get the data then close the response object
    String responseData = EntityUtils.toString(response.getEntity());
    EntityUtils.consume(response.getEntity());
    response.close();

    return responseData;
}

但是我没有收到完整的响应实体。我遗漏了大约 1000 行 html(包括结束的 bodyhtml 标记。我认为这是因为数据是分块发送的,尽管我不是完全确定。

这是回复headers:

Cache-Control:max-age=0, no-cache, no-store
Connection:Transfer-Encoding
Connection:keep-alive
Content-Encoding:gzip
Content-Type:text/html; charset=utf-8
Date:Sat, 04 Jul 2015 15:14:58 GMT
Expires:Sat, 04 Jul 2015 15:14:58 GMT
Pragma:no-cache
Server:Microsoft-IIS/7.5
Transfer-Encoding:chunked
Vary:User-Agent
Vary:Accept-Encoding
X-Content-Type-Options:nosniff
X-Frame-Options:SAMEORIGIN

如何确保收到完整的响应实体?

汇集大家的评论精华。您的代码在这里没有任何问题 - 使用 EntityUtils 是处理各种响应的推荐方法。存储您对文件的响应的代码有误。

我遇到了类似的问题,并通过确保 lcosing 这样的连接解决了它:

} finally {
        try {
            EntityUtils.consume(entity);

            try {
                response.getOutputStream().flush();
            } catch (IOException e) {
                logger.warn("Error while flushing the response output connection. It will ensure to close the connection.", e);
            }

            if (null != httpResponse) {
                httpResponse.close();
            }
        } catch (IOException ignore) {
        }
    }

或者使用 try-resources 更好的事件:

try(CloseableHttpResponse response = httpClient.execute(httpPost, context)){ 
  if (response.getEntity() == null){
    throw new NullPointerException("Unable to get html for: " + url);
  }
  String responseData = EntityUtils.toString(response.getEntity());
  EntityUtils.consume(response.getEntity());
}