From 3daa1b01ce4794f2df03ad0bf44c87b5378f9438 Mon Sep 17 00:00:00 2001 From: "parth.karia" Date: Mon, 27 Nov 2017 12:10:19 +0530 Subject: [PATCH] BAEL-21 Exploring the new HTTP Client in Java 9 --- .../java9/httpclient/HttpClientExample.java | 76 +++++++++++++++++++ core-java-9/src/main/java/module-info.java | 3 + 2 files changed, 79 insertions(+) create mode 100644 core-java-9/src/main/java/com/baeldung/java9/httpclient/HttpClientExample.java create mode 100644 core-java-9/src/main/java/module-info.java diff --git a/core-java-9/src/main/java/com/baeldung/java9/httpclient/HttpClientExample.java b/core-java-9/src/main/java/com/baeldung/java9/httpclient/HttpClientExample.java new file mode 100644 index 0000000000..6ccfd030a5 --- /dev/null +++ b/core-java-9/src/main/java/com/baeldung/java9/httpclient/HttpClientExample.java @@ -0,0 +1,76 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.baeldung.java9.httpclient; + +import java.io.File; +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.stream.Collectors; +import jdk.incubator.http.HttpClient; +import jdk.incubator.http.HttpRequest; +import jdk.incubator.http.HttpRequest.BodyProcessor; +import jdk.incubator.http.HttpResponse; +import jdk.incubator.http.HttpResponse.BodyHandler; + +/** + * + * @author pkaria + */ +public class HttpClientExample { + + public void httpGetRequest() throws URISyntaxException, IOException, InterruptedException { + HttpClient client = HttpClient.newHttpClient(); + URI httpURI = new URI("http://jsonplaceholder.typicode.com/posts/1"); + HttpRequest request = HttpRequest.newBuilder(httpURI).GET().build(); + HttpResponse response = client.send(request, HttpResponse.BodyHandler.asString()); + String responseBody = response.body(); + int responseStatusCode = response.statusCode(); + System.out.println(responseBody); + } + + public void httpPosttRequest() throws URISyntaxException, IOException, InterruptedException { + HttpClient client = HttpClient + .newBuilder() + .build(); + HttpRequest request = HttpRequest + .newBuilder(new URI("http://jsonplaceholder.typicode.com/posts")) + .POST(BodyProcessor.fromString("Sample Post Request")) + .build(); + HttpResponse response + = client.send(request, HttpResponse.BodyHandler.asString()); + String responseBody = response.body(); + System.out.println(responseBody); + } + + public void asynchronousRequest() throws URISyntaxException { + HttpClient client = HttpClient.newHttpClient(); + URI httpURI = new URI("http://jsonplaceholder.typicode.com/posts/1"); + HttpRequest request = HttpRequest.newBuilder(httpURI).GET().build(); + CompletableFuture> futureResponse = client.sendAsync(request, + HttpResponse.BodyHandler.asString()); + } + + public void asynchronousMultipleRequests() throws URISyntaxException { + List targets = Arrays.asList(new URI("http://jsonplaceholder.typicode.com/posts/1"), new URI("http://jsonplaceholder.typicode.com/posts/2")); + HttpClient client = HttpClient.newHttpClient(); + List> futures = targets + .stream() + .map(target -> client + .sendAsync( + HttpRequest.newBuilder(target) + .GET() + .build(), + BodyHandler.asFile(Paths.get("base", target.getPath()))) + .thenApply(response -> response.body()) + .thenApply(path -> path.toFile())) + .collect(Collectors.toList()); + } +} diff --git a/core-java-9/src/main/java/module-info.java b/core-java-9/src/main/java/module-info.java new file mode 100644 index 0000000000..163dd4f5be --- /dev/null +++ b/core-java-9/src/main/java/module-info.java @@ -0,0 +1,3 @@ +module com.baeldung.java9.httpclient { + requires jdk.incubator.httpclient; +} \ No newline at end of file