|
| 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 | +} |
0 commit comments