forked from winterbe/java8-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpClientExamples.java
More file actions
78 lines (68 loc) · 3.01 KB
/
HttpClientExamples.java
File metadata and controls
78 lines (68 loc) · 3.01 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package com.winterbe.java11;
import java.io.IOException;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
public class HttpClientExamples {
public static void main(String[] args) throws IOException, InterruptedException {
// syncRequest();
// asyncRequest();
// postData();
basicAuth();
}
private static void syncRequest() throws IOException, InterruptedException {
var request = HttpRequest.newBuilder()
.uri(URI.create("https://winterbe.com"))
.build();
var client = HttpClient.newHttpClient();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
}
private static void asyncRequest() {
var request = HttpRequest.newBuilder()
.uri(URI.create("https://winterbe.com"))
.build();
var client = HttpClient.newHttpClient();
client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.thenAccept(System.out::println);
}
private static void postData() throws IOException, InterruptedException {
var request = HttpRequest.newBuilder()
.uri(URI.create("https://postman-echo.com/post"))
.timeout(Duration.ofSeconds(30))
.version(HttpClient.Version.HTTP_2)
.header("Content-Type", "text/plain")
.POST(HttpRequest.BodyPublishers.ofString("Hi there!"))
.build();
var client = HttpClient.newHttpClient();
var response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.statusCode()); // 200
}
private static void basicAuth() throws IOException, InterruptedException {
var client = HttpClient.newHttpClient();
var request1 = HttpRequest.newBuilder()
.uri(URI.create("https://postman-echo.com/basic-auth"))
.build();
var response1 = client.send(request1, HttpResponse.BodyHandlers.ofString());
System.out.println(response1.statusCode()); // 401
var authClient = HttpClient
.newBuilder()
.authenticator(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("postman", "password".toCharArray());
}
})
.build();
var request2 = HttpRequest.newBuilder()
.uri(URI.create("https://postman-echo.com/basic-auth"))
.build();
var response2 = authClient.send(request2, HttpResponse.BodyHandlers.ofString());
System.out.println(response2.statusCode()); // 200
}
}