public static void main(final String[] args) throws Exception {
        final BasicCookieStore cookieStore = new BasicCookieStore();
        try (final CloseableHttpClient httpclient = HttpClients.custom()
                .setDefaultCookieStore(cookieStore)
                .build()) {
            final HttpGet httpget = new HttpGet("https://someportal/");
            httpclient.execute(httpget, response -> {
                System.out.println("----------------------------------------");
                System.out.println("Login form get: " + response.getCode() + " " + response.getReasonPhrase());
                EntityUtils.consume(response.getEntity());

                System.out.println("Initial set of cookies:");
                final List<Cookie> cookies = cookieStore.getCookies();
                if (cookies.isEmpty()) {
                    System.out.println("None");
                } else {
                    for (int i = 0; i < cookies.size(); i++) {
                        System.out.println("- " + cookies.get(i));
                    }
                }
                return null;
            });

            final ClassicHttpRequest login = ClassicRequestBuilder.post()
                    .setUri(new URI("https://someportal/"))
                    .addParameter("IDToken1", "username")
                    .addParameter("IDToken2", "password")
                    .build();
            httpclient.execute(login, response -> {
                System.out.println("----------------------------------------");
                System.out.println("Login form get: " + response.getCode() + " " + response.getReasonPhrase());
                EntityUtils.consume(response.getEntity());

                System.out.println("Post logon cookies:");
                final List<Cookie> cookies = cookieStore.getCookies();
                if (cookies.isEmpty()) {
                    System.out.println("None");
                } else {
                    for (int i = 0; i < cookies.size(); i++) {
                        System.out.println("- " + cookies.get(i));
                    }
                }
                return null;
            });
        }
    }
A example that demonstrates how HttpClient APIs can be used to perform
form-based logon.
httpclient5/src/test/java/org/apache/hc/client5/http/examples/ClientFormLogin.java