在我的應用程序中,我需要解析一個網站並將一些數據從ir存儲到數據庫中。我正在使用HttpClient獲取頁面內容。我的代碼如下所示:不正確的Java HttpClient響應流
HttpClient client = new DefaultHttpClient();
System.out.println(doc.getUrl());
HttpGet contentGet= new HttpGet(siteUrl + personUrl);
HttpResponse response = client.execute(contentGet);
String html = convertStreamToString(response.getEntity().getContent());
/*
parse the page
*/
/***********************************************************************/
public static String convertStreamToString(InputStream is) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
return sb.toString();
}
我在一個循環中這樣做 - 我試圖得到一些網頁的內容(他們的結構是一樣的)。有時它工作正常,但不幸的是,我在很多情況下的反應是相似的垃圾liek這樣的順序:
�=�v7���9�Hdz$�d7/�$�st��؎I��X^�$A6t_D���!gr�����C^��[email protected]��MQ�2�d�8�]
我 我不知道問題出在哪裏,請幫助我。
我已經顯示了所有回覆的標題。爲了正確的,主要有:
Server : nginx/1.0.13
Date : Sat, 23 Mar 2013 21:50:31 GMT
Content-Type : text/html; charset=utf-8
Transfer-Encoding : chunked
Connection : close
Vary : Accept-Encoding
Expires : Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control : no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma : no-cache
Set-Cookie : pfSC=1; path=/; domain=.profeo.pl
Set-Cookie : pfSCvp=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.profeo.pl
對於那些不正確的:
Server : nginx/1.2.4
Date : Sat, 23 Mar 2013 21:50:33 GMT
Content-Type : text/html
Transfer-Encoding : chunked
Connection : close
Set-Cookie : pfSCvp=3cff2422fd8f9b6e57e858d3883f4eaf; path=/; domain=.profeo.pl
Content-Encoding : gzip
任何其他建議?我的猜測是,這個gzip編碼在這裏是一個問題,但我能做些什麼呢?
我使用EntityUtils按照你的建議,並運行它20個類似的頁面。我還顯示了isTextual變量的值。對於20頁中的2頁,回覆是正確的HTML,對於其他18頁,我再次收到垃圾。對於他們所有人來說,isTextual的價值是真實的。例如 - 頁面http://profeo.pl/piotr-grzes已成功收到,並且http://profeo.pl/annais不是。我不知道有什麼問題,這些頁面幾乎完全相同。 – user1315305 2013-03-23 21:00:00
我在第一篇文章中添加了一些新的信息。 – user1315305 2013-03-23 21:55:14
非常感謝!我花了很多時間試圖找出它,現在它終於起作用了! – user1315305 2013-03-23 22:49:05