Skip to content

Commit 4b92b43

Browse files
committed
Java 11 HttpClient examples
1 parent 81a0fa3 commit 4b92b43

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.winterbe.java11;
2+
3+
import java.io.IOException;
4+
import java.net.Authenticator;
5+
import java.net.PasswordAuthentication;
6+
import java.net.URI;
7+
import java.net.http.HttpClient;
8+
import java.net.http.HttpRequest;
9+
import java.net.http.HttpResponse;
10+
import java.time.Duration;
11+
12+
public class HttpClientExamples {
13+
14+
public static void main(String[] args) throws IOException, InterruptedException {
15+
// syncRequest();
16+
// asyncRequest();
17+
// postData();
18+
basicAuth();
19+
}
20+
21+
private static void syncRequest() throws IOException, InterruptedException {
22+
var client = HttpClient.newHttpClient();
23+
var request = HttpRequest.newBuilder()
24+
.uri(URI.create("https://winterbe.com"))
25+
.build();
26+
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
27+
System.out.println(response.body());
28+
}
29+
30+
private static void asyncRequest() {
31+
var client = HttpClient.newHttpClient();
32+
var request = HttpRequest.newBuilder()
33+
.uri(URI.create("https://winterbe.com"))
34+
.build();
35+
client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
36+
.thenApply(HttpResponse::body)
37+
.thenAccept(System.out::println);
38+
}
39+
40+
private static void postData() throws IOException, InterruptedException {
41+
var client = HttpClient.newHttpClient();
42+
var request = HttpRequest.newBuilder()
43+
.uri(URI.create("https://postman-echo.com/post"))
44+
.timeout(Duration.ofSeconds(30))
45+
.version(HttpClient.Version.HTTP_2)
46+
.header("Content-Type", "text/plain")
47+
.POST(HttpRequest.BodyPublishers.ofString("Hi there!"))
48+
.build();
49+
var response = client.send(request, HttpResponse.BodyHandlers.ofString());
50+
System.out.println(response.statusCode()); // 200
51+
}
52+
53+
private static void basicAuth() throws IOException, InterruptedException {
54+
var client = HttpClient.newHttpClient();
55+
var request1 = HttpRequest.newBuilder()
56+
.uri(URI.create("https://postman-echo.com/basic-auth"))
57+
.build();
58+
var response1 = client.send(request1, HttpResponse.BodyHandlers.ofString());
59+
System.out.println(response1.statusCode()); // 401
60+
61+
var authClient = HttpClient
62+
.newBuilder()
63+
.authenticator(new Authenticator() {
64+
@Override
65+
protected PasswordAuthentication getPasswordAuthentication() {
66+
return new PasswordAuthentication("postman", "password".toCharArray());
67+
}
68+
})
69+
.build();
70+
var request2 = HttpRequest.newBuilder()
71+
.uri(URI.create("https://postman-echo.com/basic-auth"))
72+
.build();
73+
var response2 = authClient.send(request2, HttpResponse.BodyHandlers.ofString());
74+
System.out.println(response2.statusCode()); // 200
75+
}
76+
77+
}

0 commit comments

Comments
 (0)