-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHTTPClientDemo.java
35 lines (30 loc) · 1.02 KB
/
HTTPClientDemo.java
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
package com.di1shuai.java11.http;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
/**
* @author Shea
* @date 2021-01-22
* @description 支持HTTP/1.1和HTTP/2 ,也支持 websockets
* Java 9 引入
* Java 10 更新
* Java 11 标准化
*/
public class HTTPClientDemo {
public static void main(String[] args) throws IOException, InterruptedException {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://di1shuai.com"))
.GET()
.build();
// 同步
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
// 异步
client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.thenAccept(System.out::println);
}
}