Search code examples
apache-httpclient-4.x

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


Solution

  • 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);