Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

apache httpclient- most efficient way to read response

I'm using apache httpcompnonents library for httpclient. I want to use it in a multithreaded application where number of threads are going to be really high and there would be frequent http calls. This is the code I'm using to read the response after execute call.

HttpEntity entity = httpResponse.getEntity();
String response = EntityUtils.toString(entity);

I just want to confirm that is it the most efficient way of reading the response?

Thanks, Hemant

like image 958
Hemant Avatar asked Jul 04 '13 07:07

Hemant


People also ask

Is Apache HttpClient deprecated?

From Apache HTTP Client API version 4.3 on wards, DefaultHttpClient is deprecated.

Is HttpClient multithreaded?

HttpClient is fully thread-safe when used with a thread-safe connection manager such as MultiThreadedHttpConnectionManager.

Is HttpClient RESTful?

Apache HttpClient is a robust and complete solution Java library to perform HTTP operations, including RESTful service.


1 Answers

This in fact represents the most inefficient way of processing an HTTP response.

You most likely want to digest the content of the response into a domain object of a sort. So, what is the point of buffering it in-memory in a form of a string?

The recommended way to deal with response processing is by using a custom ResponseHandler that can process the content by streaming it directly from the underlying connection. The added benefit of using a ResponseHandler is that it completely relieves from dealing with connection release and resource deallocation.

EDIT: modified the sample code to use JSON

Here's an example of it using HttpClient 4.2 and Jackson JSON processor. Stuff is assumed to be your domain object with JSON bindings.

ResponseHandler<Stuff> rh = new ResponseHandler<Stuff>() {

    @Override
    public Stuff handleResponse(
            final HttpResponse response) throws IOException {
        StatusLine statusLine = response.getStatusLine();
        if (statusLine.getStatusCode() >= 300) {
            throw new HttpResponseException(
                    statusLine.getStatusCode(),
                    statusLine.getReasonPhrase());
        }
        HttpEntity entity = response.getEntity();
        if (entity == null) {
            throw new ClientProtocolException("Response contains no content");
        }
        JsonFactory jsonf = new JsonFactory();
        InputStream instream = entity.getContent();
        // try - finally is not strictly necessary here 
        // but is a good practice
        try {
            JsonParser jsonParser = jsonf.createParser(instream);
            // Use the parser to deserialize the object from the content stream
            return stuff;
        }  finally {
            instream.close();
        }
    }
};
DefaultHttpClient client = new DefaultHttpClient();
Stuff mystuff = client.execute(new HttpGet("http://somehost/stuff"), rh);
like image 80
ok2c Avatar answered Nov 09 '22 02:11

ok2c