0
更新:如果我使用System.out.println(EntityUtils.toString(response.getEntity()));
輸出似乎是HTML失蹤線(包括關閉body
和html
標籤)。然而,打印到文件仍然只給我的第一個2000奇數行丟掉了最後1000的Apache的HttpClient沒有收到整個響應
我使用下面的代碼來執行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
我怎麼能保證我收到完整的響應實體
保存到文件的共享代碼 - 必須有錯誤 – Antoniossss
@WandMaker definetly否 – Antoniossss
@WandMaker我不這麼認爲,請參閱http://stackoverflow.com/questions/15969037/why-did-the-author -use-entityutils-consumehttpentity – james