1
我想借助Apache HttpClient 4.2(Java)獲得一系列Web頁面。問題是:一些在串聯HttpEntities具有空的內容,即:Apache HttpClient返回空內容的實體
is = new ByteArrayInputStream(EntityUtils.toByteArray(entity))
System.out.println(response.getStatusLine());
System.out.println(is.available());
示出了HTTP/1.1 200 OK的0。對於他人它顯示,例如HTTP/1.1 200 OK和64344. 如果我重新啓動代碼,系列中的另一個HttpEntities可能爲空。我做了一個遞歸獲取網頁在同一個程序運行,直到獲得非零內容 - 和一些調用後,我已經得到它...我運行Win'XP下的程序。
代碼本身(沒有遞歸):
public InputStream loadURL(String url) throws IOException {
PoolingClientConnectionManager connManager = new PoolingClientConnectionManager();
DefaultHttpClient httpclient = new DefaultHttpClient(connManager);
InputStream is = null;
try {
HttpGet httpget = new HttpGet(url);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) {
try {
System.out.println("========================================");
is = new ByteArrayInputStream(EntityUtils.toByteArray(entity));
System.out.println(is.available());
System.out.println(response.getStatusLine());
System.out.println("========================================");
} catch (IOException ex) {
throw ex;
} catch (RuntimeException ex) {
httpget.abort();
throw ex;
}
}
} catch (ClientProtocolException ex) {
throw ex;
} finally {
httpclient.getConnectionManager().shutdown();
}
return is;
}
的InputStream在外部代碼關閉。
感謝您的回覆。也許這是令人困惑的例子,因爲'available()'僅用於演示文稿。當我嘗試解析接收到的內容並且根本沒有任何東西可以解析時,這個問題得到了確認......當我嘗試了'EntityUtils.toString(entity)'時,返回了一個空字符串... – DrDecay 2012-08-05 15:14:43