O'Reilly logo

Java 9 Cookbook by Nick Samoylov, Mohamed Sanaulla

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

There is more...

We can provide a custom response handler while invoking the HttpClient.execute method, as follows:

String responseBody = client.execute(request, response -> {  int status = response.getStatusLine().getStatusCode();  HttpEntity entity = response.getEntity();  return entity != null ? EntityUtils.toString(entity) : null;});

In this case, the response is processed by the response handler and returns us the response body string. The complete code for this can be found at the location, chp11\5_1_apache_http_demo_response_handler.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required