forked from PacktPublishing/Java-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
37 lines (27 loc) · 1.42 KB
/
Copy pathMain.java
File metadata and controls
37 lines (27 loc) · 1.42 KB
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
31
32
33
34
35
36
37
package modern.challenge;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpHeaders;
import java.util.List;
import java.util.Optional;
public class Main {
public static void main(String[] args) throws IOException, InterruptedException {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.headers("Content-Type", "application/json")
.header("Referer", "https://reqres.in/")
.setHeader("X-Auth", "authtoken")
.uri(URI.create("https://reqres.in/api/users/2"))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
HttpHeaders allHeaders = response.headers();
System.out.println("All headers: " + allHeaders);
List<String> allValuesOfCacheControl = response.headers().allValues("Cache-Control");
System.out.println("All values of Cache-Control: " + allValuesOfCacheControl);
Optional<String> firstValueOfCacheControl = response.headers().firstValue("Cache-Control");
System.out.println("First value of Cache-Control: " + firstValueOfCacheControl);
}
}