【问题标题】:Apache HttpClient not receiving entire responseApache HttpClient 没有收到整个响应
【发布时间】:2015-07-04 15:39:13
【问题描述】:

更新:如果我使用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

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

【问题讨论】:

  • 共享代码保存到文件 - 必须有一个错误
  • @WandMaker 绝对没有
  • @WandMaker 我不这么认为,请参阅stackoverflow.com/questions/15969037/…
  • @Antoniossss 你是对的。最初我使用单行创建打印器,然后调用 print.但是,正确使用它(调用 flush() 和 close())可确保将所有 html 写入文件。不幸的是,我仍然缺少 html,所以这意味着我的请求是错误的:/
  • 但这是不同的问题 ;)

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


【解决方案1】:

收集所有cmets的精华。您的代码在这里没有任何问题 - 使用 EntityUtils 是处理各种响应的推荐方式。存储您对文件的响应的代码有错误。

【讨论】:

    【解决方案2】:

    我有一个类似的问题,并通过确保 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());
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-23
      • 1970-01-01
      • 1970-01-01
      • 2014-05-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多