Skip to content

Commit 20ffefa

Browse files
authored
Merge pull request eugenp#3019 from parthkaria/master
BAEL-21 Exploring the new HTTP Client in Java 9
2 parents 4672451 + a793b67 commit 20ffefa

4 files changed

Lines changed: 90 additions & 1 deletion

File tree

‎core-java-9/compile-httpclient.bat‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
javac --module-path mods -d mods/com.baeldung.httpclient^
2+
src/modules/com.baeldung.httpclient/module-info.java^
3+
src/modules/com.baeldung.httpclient/com/baeldung/httpclient/HttpClientExample.java
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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.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 static void main(String[] args) throws Exception {
30+
httpGetRequest();
31+
httpPosttRequest();
32+
asynchronousRequest();
33+
asynchronousMultipleRequests();
34+
}
35+
36+
public static void httpGetRequest() throws URISyntaxException, IOException, InterruptedException {
37+
HttpClient client = HttpClient.newHttpClient();
38+
URI httpURI = new URI("http://jsonplaceholder.typicode.com/posts/1");
39+
HttpRequest request = HttpRequest.newBuilder(httpURI).GET().build();
40+
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandler.asString());
41+
String responseBody = response.body();
42+
int responseStatusCode = response.statusCode();
43+
System.out.println(responseBody);
44+
}
45+
46+
public static void httpPosttRequest() throws URISyntaxException, IOException, InterruptedException {
47+
HttpClient client = HttpClient
48+
.newBuilder()
49+
.build();
50+
HttpRequest request = HttpRequest
51+
.newBuilder(new URI("http://jsonplaceholder.typicode.com/posts"))
52+
.POST(BodyProcessor.fromString("Sample Post Request"))
53+
.build();
54+
HttpResponse<String> response
55+
= client.send(request, HttpResponse.BodyHandler.asString());
56+
String responseBody = response.body();
57+
System.out.println(responseBody);
58+
}
59+
60+
public static void asynchronousRequest() throws URISyntaxException {
61+
HttpClient client = HttpClient.newHttpClient();
62+
URI httpURI = new URI("http://jsonplaceholder.typicode.com/posts/1");
63+
HttpRequest request = HttpRequest.newBuilder(httpURI).GET().build();
64+
CompletableFuture<HttpResponse<String>> futureResponse = client.sendAsync(request,
65+
HttpResponse.BodyHandler.asString());
66+
}
67+
68+
public static void asynchronousMultipleRequests() throws URISyntaxException {
69+
List<URI> targets = Arrays.asList(new URI("http://jsonplaceholder.typicode.com/posts/1"), new URI("http://jsonplaceholder.typicode.com/posts/2"));
70+
HttpClient client = HttpClient.newHttpClient();
71+
List<CompletableFuture<File>> futures = targets
72+
.stream()
73+
.map(target -> client
74+
.sendAsync(
75+
HttpRequest.newBuilder(target)
76+
.GET()
77+
.build(),
78+
BodyHandler.asFile(Paths.get("base", target.getPath())))
79+
.thenApply(response -> response.body())
80+
.thenApply(path -> path.toFile()))
81+
.collect(Collectors.toList());
82+
}
83+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module com.baeldung.httpclient {
2+
requires jdk.incubator.httpclient;
3+
}

‎libraries/pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -711,4 +711,4 @@
711711
<hazelcast.version>3.8.4</hazelcast.version>
712712
<caffeine.version>2.5.5</caffeine.version>
713713
</properties>
714-
</project>
714+
</project>

0 commit comments

Comments
 (0)