Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4b92b43

Browse files
committed
Java 11 HttpClient examples
1 parent81a0fa3 commit4b92b43

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
packagecom.winterbe.java11;
2+
3+
importjava.io.IOException;
4+
importjava.net.Authenticator;
5+
importjava.net.PasswordAuthentication;
6+
importjava.net.URI;
7+
importjava.net.http.HttpClient;
8+
importjava.net.http.HttpRequest;
9+
importjava.net.http.HttpResponse;
10+
importjava.time.Duration;
11+
12+
publicclassHttpClientExamples {
13+
14+
publicstaticvoidmain(String[]args)throwsIOException,InterruptedException {
15+
// syncRequest();
16+
// asyncRequest();
17+
// postData();
18+
basicAuth();
19+
}
20+
21+
privatestaticvoidsyncRequest()throwsIOException,InterruptedException {
22+
varclient =HttpClient.newHttpClient();
23+
varrequest =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+
privatestaticvoidasyncRequest() {
31+
varclient =HttpClient.newHttpClient();
32+
varrequest =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+
privatestaticvoidpostData()throwsIOException,InterruptedException {
41+
varclient =HttpClient.newHttpClient();
42+
varrequest =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+
varresponse =client.send(request,HttpResponse.BodyHandlers.ofString());
50+
System.out.println(response.statusCode());// 200
51+
}
52+
53+
privatestaticvoidbasicAuth()throwsIOException,InterruptedException {
54+
varclient =HttpClient.newHttpClient();
55+
varrequest1 =HttpRequest.newBuilder()
56+
.uri(URI.create("https://postman-echo.com/basic-auth"))
57+
.build();
58+
varresponse1 =client.send(request1,HttpResponse.BodyHandlers.ofString());
59+
System.out.println(response1.statusCode());// 401
60+
61+
varauthClient =HttpClient
62+
.newBuilder()
63+
.authenticator(newAuthenticator() {
64+
@Override
65+
protectedPasswordAuthenticationgetPasswordAuthentication() {
66+
returnnewPasswordAuthentication("postman","password".toCharArray());
67+
}
68+
})
69+
.build();
70+
varrequest2 =HttpRequest.newBuilder()
71+
.uri(URI.create("https://postman-echo.com/basic-auth"))
72+
.build();
73+
varresponse2 =authClient.send(request2,HttpResponse.BodyHandlers.ofString());
74+
System.out.println(response2.statusCode());// 200
75+
}
76+
77+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp