public static void main(final String[] args) throws Exception {
        try (final CloseableHttpClient httpclient = HttpClients.createDefault()) {
            // Create a local instance of cookie store
            final CookieStore cookieStore = new BasicCookieStore();

            // Create local HTTP context
            final HttpClientContext localContext = ContextBuilder.create()
                    // Bind custom cookie store to the local context
                    .useCookieStore(cookieStore)
                    .build();

            final HttpGet httpget = new HttpGet("http://httpbin.org/cookies");
            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));
                final List<Cookie> cookies = cookieStore.getCookies();
                for (int i = 0; i < cookies.size(); i++) {
                    System.out.println("Local cookie: " + cookies.get(i));
                }
                EntityUtils.consume(response.getEntity());
                return null;
            });
        }
    }
This example demonstrates the use of a local HTTP context populated with
custom attributes.
httpclient5/src/test/java/org/apache/hc/client5/http/examples/ClientCustomContext.java