2015-07-04 41 views
0

更新:如果我使用System.out.println(EntityUtils.toString(response.getEntity()));輸出似乎是HTML失蹤線(包括關閉bodyhtml標籤)。然而,打印到文件仍然只給我的第一個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行(包括關閉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 

我怎麼能保證我收到完整的響應實體

+1

保存到文件的共享代碼 - 必須有錯誤 – Antoniossss

+0

@WandMaker definetly否 – Antoniossss

+0

@WandMaker我不這麼認爲,請參閱http://stackoverflow.com/questions/15969037/why-did-the-author -use-entityutils-consumehttpentity – james

回答

1

要收集的意見,所有的精華,沒有什麼不對您的代碼在這裏 - ?使用EntityUtils是應對各類recomended方式的答覆。您在存儲對文件的響應的代碼中存在錯誤。