I'm trying to get a series of Web pages with the help of Apache HttpClient 4.2 (Java). The problem is: some of HttpEntities in the series has empty content, i.e.:
is = new ByteArrayInputStream(EntityUtils.toByteArray(entity))
System.out.println(response.getStatusLine());
System.out.println(is.available());
shows HTTP/1.1 200 OK an 0. For others it shows, e.g. HTTP/1.1 200 OK and 64344. If I restart code, another HttpEntities in the series may be empty. I made a recursion of getting Web page in the same program run until getting non zero content - and after some calls I have got it... I run program under Win'XP.
The code itself (without recursion):
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 is closed in external code.