【发布时间】:2015-07-04 15:39:13
【问题描述】:
更新:如果我使用System.out.println(EntityUtils.toString(response.getEntity()));,则输出似乎是缺少的HTML 行(包括结束body 和html 标记)。但是,打印到文件仍然只给我前 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(包括结束的 body 和 html 标签。我认为这是因为数据是分块发送的,尽管我不完全确定。
这里是响应头:
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