public static void main(final String... args)throws Exception {
        final Document result = Request.get("http://somehost/content")
                .execute().handleResponse(response -> {
                    final int status = response.getCode();
                    final HttpEntity entity = response.getEntity();
                    if (status >= HttpStatus.SC_REDIRECTION) {
                        throw new HttpResponseException(status, response.getReasonPhrase());
                    }
                    if (entity == null) {
                        throw new ClientProtocolException("Response contains no content");
                    }
                    final DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
                    try {
                        final DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
                        final ContentType contentType = ContentType.parseLenient(entity.getContentType());
                        if (!contentType.equals(ContentType.APPLICATION_XML)) {
                            throw new ClientProtocolException("Unexpected content type:" + contentType);
                        }
                        Charset charset = contentType.getCharset();
                        if (charset == null) {
                            charset = StandardCharsets.ISO_8859_1;
                        }
                        return docBuilder.parse(entity.getContent(), charset.name());
                    } catch (final ParserConfigurationException ex) {
                        throw new IllegalStateException(ex);
                    } catch (final SAXException ex) {
                        throw new ClientProtocolException("Malformed XML document", ex);
                    }
                });
        // Do something useful with the result
        System.out.println(result);
    }
This example demonstrates how the HttpClient fluent API can be used to handle HTTP responses
without buffering content body in memory.
httpclient5-fluent/src/test/java/org/apache/hc/client5/http/examples/fluent/FluentResponseHandling.java