September 2019
Intermediate to advanced
816 pages
18h 47m
English
Typically, authentication to a server is accomplished using a username and password. In code form, this can be done by using the Authenticator class (this negotiates the credentials for HTTP authentication) and the PasswordAuthentication class (the holder for the username and password) together, as follows:
HttpClient client = HttpClient.newBuilder() .authenticator(new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication( "username", "password".toCharArray()); } }) .build();
Furthermore, the client can be used to send requests:
HttpRequest request = HttpRequest.newBuilder() ... .build();HttpResponse<String> response = client.send(request, ...