目录
jdk9开始引入HTTP Client标准化,jdk10开始更新。CompletableFutures提供了非阻塞请求和响应式,java.util.concurrent.Flow API提供流量控制支持,从用户请求发布者和响应订阅者,一直到底层套接字,更容易的跟踪数据流。
public void get(String uri) throws Exception {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(uri))
.build();
HttpResponse<String> response =
client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
}
使用HttpResponse.BodyHandlers.ofStrin