Search code examples
javaapache-httpclient-4.xapache-commons-httpclient

Migration from HTTPClient 3.1 to 4.3.3, Method.getResponseBody(int)


I'm updating an old software using HTTPClient 3.1 to use HTTPClient 4.3.3. I noticed that in the old code there is a specific requirement: when getting a remote page/resource the client is able to verify the dimension, generating an exception if the content is too big WITHOUT downloading the full resource. This was accomplished in the following manner:

int status = client.executeMethod(method);
...
byte[] responseBody= method.getResponseBody(maxAllowedSize+1);

Notice the "+1" after maxAllowedSize: it's requested to have a proof that the original page/resource was in fact too big. If the last byte was used, an exception was thrown; otherwise the page was processed.

I'm trying to implement the same thing in HTTPClient 4.3.3, but I can't find a way to download only a defined number of bytes from the server... this is critical in my application. Can you help me? Thank you in advance.

Javadoc of the old getResponseBody(int) method: https://hc.apache.org/httpclient-3.x/apidocs/org/apache/commons/httpclient/HttpMethodBase.html#getResponseBody(int)


Solution

  • One generally should be consuming content directly from the content stream instead of buffering it in an intermediate buffer, but this is roughly the same thing with 4.3 APIs:

    CloseableHttpClient client = HttpClients.custom()
            .build();
    try (CloseableHttpResponse response = client.execute(new HttpGet("/"))) {
        HttpEntity entity = response.getEntity();
        long expectedLen = entity.getContentLength();
        if (expectedLen != -1 && expectedLen > MAX_LIMIT) {
            throw new IOException("Size matters!!!!");
        }
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        InputStream inputStream = entity.getContent();
        byte[] tmp = new byte[1024];
        int chunk, total = 0;
        while ((chunk = inputStream.read(tmp)) != -1) {
            buffer.write(tmp, 0, chunk);
            total += chunk;
            if (total > MAX_LIMIT) {
                throw new IOException("Size matters!!!!");
            }
        }
        byte[] stuff = buffer.toByteArray();
    }