public static void main(final String[] args) throws Exception {
        final CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
        httpclient.start();

        final HttpClientContext localContext = ContextBuilder.create()
                .preemptiveBasicAuth(new HttpHost("http", "httpbin.org"),
                        new UsernamePasswordCredentials("user", "passwd".toCharArray()))
                .build();

        final SimpleHttpRequest request = SimpleRequestBuilder.get("http://httpbin.org:80/basic-auth/user/passwd")
                .build();

        System.out.println("Executing request " + request);
        for (int i = 0; i < 3; i++) {
            final Future<SimpleHttpResponse> future = httpclient.execute(
                    SimpleRequestProducer.create(request),
                    SimpleResponseConsumer.create(),
                    localContext,
                    new FutureCallback<SimpleHttpResponse>() {

                        @Override
                        public void completed(final SimpleHttpResponse response) {
                            System.out.println(request + "->" + new StatusLine(response));
                            System.out.println(response.getBody());
                        }

                        @Override
                        public void failed(final Exception ex) {
                            System.out.println(request + "->" + ex);
                        }

                        @Override
                        public void cancelled() {
                            System.out.println(request + " cancelled");
                        }

                    });
            future.get();
        }

        System.out.println("Shutting down");
        httpclient.close(CloseMode.GRACEFUL);
    }
A simple example that uses HttpClient to execute an HTTP request against
a target site that requires user authentication.
httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncPreemptiveBasicClientAuthentication.java