public static void main(final String[] args) throws Exception {
        try (final CloseableHttpClient httpclient = HttpClients.createDefault()) {

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

            final HttpGet httpget = new HttpGet("http://httpbin.org/hidden-basic-auth/user/passwd");

            System.out.println("Executing request " + httpget.getMethod() + " " + httpget.getUri());
            for (int i = 0; i < 3; i++) {
                httpclient.execute(httpget, localContext, response -> {
                    System.out.println("----------------------------------------");
                    System.out.println(httpget + "->" + new StatusLine(response));
                    EntityUtils.consume(response.getEntity());
                    return null;
                });
            }
        }
    }
An example of how HttpClient can be customized to authenticate
preemptively using Basic scheme.
<b/>
Generally, preemptive authentication can be considered less
secure than a response to an authentication challenge
and therefore discouraged.
httpclient5/src/test/java/org/apache/hc/client5/http/examples/ClientPreemptiveBasicAuthentication.java