public static void main(final String[] args) throws Exception {
        try (final CloseableHttpClient httpclient = HttpClients.createDefault()) {
            // Create local HTTP context
            final HttpClientContext localContext = HttpClientContext.create();

            final HttpGet httpget = new HttpGet("http://httpbin.org/get");
            System.out.println("Executing request " + httpget.getMethod() + " " + httpget.getUri());

            // Pass local context as a parameter
            httpclient.execute(httpget, localContext, response -> {
                System.out.println("----------------------------------------");
                System.out.println(httpget + "->" + new StatusLine(response));
                EntityUtils.consume(response.getEntity());

                final EndpointDetails endpointDetails = localContext.getEndpointDetails();
                System.out.println("Remote address: " + endpointDetails.getRemoteAddress());
                System.out.println("Request counts: " + endpointDetails.getRequestCount());
                System.out.println("Response counts: " + endpointDetails.getResponseCount());
                System.out.println("Bytes sent: " + endpointDetails.getSentBytesCount());
                System.out.println("Bytes received: " + endpointDetails.getReceivedBytesCount());

                return null;
            });

            // EndpointDetails is present in the context even if the underlying connection
            // gets immediately released back to the connection pool

            final HttpHead httphead = new HttpHead("http://httpbin.org/get");
            System.out.println("Executing request " + httphead.getMethod() + " " + httphead.getUri());

            // Pass local context as a parameter
            httpclient.execute(httphead, localContext, response -> {
                System.out.println("----------------------------------------");
                System.out.println(httphead + "->" + new StatusLine(response));
                EntityUtils.consume(response.getEntity());

                final EndpointDetails endpointDetails = localContext.getEndpointDetails();
                System.out.println("Remote address: " + endpointDetails.getRemoteAddress());
                System.out.println("Request counts: " + endpointDetails.getRequestCount());
                System.out.println("Response counts: " + endpointDetails.getResponseCount());
                System.out.println("Bytes sent: " + endpointDetails.getSentBytesCount());
                System.out.println("Bytes received: " + endpointDetails.getReceivedBytesCount());

                return null;
            });
        }
    }
This example demonstrates how to get details of the underlying connection endpoint.
httpclient5/src/test/java/org/apache/hc/client5/http/examples/ClientRemoteEndpointDetails.java