Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(String[] args) {
- var client = HttpClient.newBuilder()
- .authenticator(new Authenticator() {
- @Override
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication("username", "password".toCharArray());
- }
- })
- .version(HttpClient.Version.HTTP_1_1)
- .build();
- var request = HttpRequest.newBuilder()
- .uri("https://service-that-needs-auth.example/")
- .build();
- client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
- .thenApply(HttpResponse::body)
- .thenAccept(System.out::println)
- .join();
- }
- public static void main(String[] args) {
- var client = HttpClient.newBuilder()
- .version(HttpClient.Version.HTTP_1_1)
- .build();
- var request = HttpRequest.newBuilder()
- .uri("https://service-that-needs-auth.example/")
- .header("Authorization", basicAuth("username", "password"))
- .build();
- client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
- .thenApply(HttpResponse::body)
- .thenAccept(System.out::println)
- .join();
- }
- private static String basicAuth(String username, String password) {
- return "Basic " + Base64.getEncoder().encodeToString((username + ":" + password).getBytes());
- }
Add Comment
Please, Sign In to add comment