forked from PacktPublishing/Java-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViaAuthenticator.java
More file actions
30 lines (24 loc) · 1000 Bytes
/
ViaAuthenticator.java
File metadata and controls
30 lines (24 loc) · 1000 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package modern.challenge;
import java.io.IOException;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class ViaAuthenticator {
public void authenticatorExample() throws IOException, InterruptedException {
HttpClient client = HttpClient.newBuilder()
.authenticator(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
"username",
"password".toCharArray());
}
}).build();
HttpRequest request = HttpRequest.newBuilder()
// ...
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
}
}