Apache HttpClient没有收到整个响应

时间:2015-07-04 15:39:14

标签: java http apache-httpclient-4.x chunked-encoding

更新:如果我使用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标记。我认为这是因为数据是以块的形式发送的,尽管我并不完全确定。

以下是回复标题:

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

如何确保收到完整的回复实体?

2 个答案:

答案 0 :(得分:1)

收集评论的所有要点。您的代码在这里没有任何问题 - 使用EntityUtils是处理各种响应的推荐方法。您在代码中存在错误,该代码存储您对文件的响应。

答案 1 :(得分:0)

我遇到了类似的问题,并通过确保连接如下来解决了这个问题:

} 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());
}