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