1

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.

1 Answer 1

1

If you're relying on available() to tell you whether the entity is empty or not you are misusing it. It returns the number of bytes that can be read without blocking. Check the Javadoc, where you will find a specific warning against using it to predict the total length of the incoming data. That's not what it's for.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for reply. Perhaps it was confusing example, because available() was only for presentation use. The problem was recognized when I try to parse the received content and there was nothing to parse at all...When I have tried EntityUtils.toString(entity) an empty string was returned...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.