Skip to content

Commit 3daa1b0

Browse files
committed
BAEL-21 Exploring the new HTTP Client in Java 9
1 parent b1782b3 commit 3daa1b0

2 files changed

Lines changed: 79 additions & 0 deletions

File tree

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package com.baeldung.java9.httpclient;
7+
8+
import java.io.File;
9+
import java.io.IOException;
10+
import java.net.URI;
11+
import java.net.URISyntaxException;
12+
import java.nio.file.Paths;
13+
import java.util.Arrays;
14+
import java.util.List;
15+
import java.util.concurrent.CompletableFuture;
16+
import java.util.stream.Collectors;
17+
import jdk.incubator.http.HttpClient;
18+
import jdk.incubator.http.HttpRequest;
19+
import jdk.incubator.http.HttpRequest.BodyProcessor;
20+
import jdk.incubator.http.HttpResponse;
21+
import jdk.incubator.http.HttpResponse.BodyHandler;
22+
23+
/**
24+
*
25+
* @author pkaria
26+
*/
27+
public class HttpClientExample {
28+
29+
public void httpGetRequest() throws URISyntaxException, IOException, InterruptedException {
30+
HttpClient client = HttpClient.newHttpClient();
31+
URI httpURI = new URI("http://jsonplaceholder.typicode.com/posts/1");
32+
HttpRequest request = HttpRequest.newBuilder(httpURI).GET().build();
33+
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandler.asString());
34+
String responseBody = response.body();
35+
int responseStatusCode = response.statusCode();
36+
System.out.println(responseBody);
37+
}
38+
39+
public void httpPosttRequest() throws URISyntaxException, IOException, InterruptedException {
40+
HttpClient client = HttpClient
41+
.newBuilder()
42+
.build();
43+
HttpRequest request = HttpRequest
44+
.newBuilder(new URI("http://jsonplaceholder.typicode.com/posts"))
45+
.POST(BodyProcessor.fromString("Sample Post Request"))
46+
.build();
47+
HttpResponse<String> response
48+
= client.send(request, HttpResponse.BodyHandler.asString());
49+
String responseBody = response.body();
50+
System.out.println(responseBody);
51+
}
52+
53+
public void asynchronousRequest() throws URISyntaxException {
54+
HttpClient client = HttpClient.newHttpClient();
55+
URI httpURI = new URI("http://jsonplaceholder.typicode.com/posts/1");
56+
HttpRequest request = HttpRequest.newBuilder(httpURI).GET().build();
57+
CompletableFuture<HttpResponse<String>> futureResponse = client.sendAsync(request,
58+
HttpResponse.BodyHandler.asString());
59+
}
60+
61+
public void asynchronousMultipleRequests() throws URISyntaxException {
62+
List<URI> targets = Arrays.asList(new URI("http://jsonplaceholder.typicode.com/posts/1"), new URI("http://jsonplaceholder.typicode.com/posts/2"));
63+
HttpClient client = HttpClient.newHttpClient();
64+
List<CompletableFuture<File>> futures = targets
65+
.stream()
66+
.map(target -> client
67+
.sendAsync(
68+
HttpRequest.newBuilder(target)
69+
.GET()
70+
.build(),
71+
BodyHandler.asFile(Paths.get("base", target.getPath())))
72+
.thenApply(response -> response.body())
73+
.thenApply(path -> path.toFile()))
74+
.collect(Collectors.toList());
75+
}
76+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module com.baeldung.java9.httpclient {
2+
requires jdk.incubator.httpclient;
3+
}

0 commit comments

Comments
 (0)