From bdf881701c8676714301b5865fdc1b3bddc8b32a Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Fri, 1 Dec 2017 16:11:07 +0200 Subject: [PATCH] add headers ex --- core-java-9/run-httpclient.bat | 1 + .../com/baeldung/httpclient/HttpClientExample.java | 7 ++++--- 2 files changed, 5 insertions(+), 3 deletions(-) create mode 100644 core-java-9/run-httpclient.bat diff --git a/core-java-9/run-httpclient.bat b/core-java-9/run-httpclient.bat new file mode 100644 index 0000000000..60b1eb7f68 --- /dev/null +++ b/core-java-9/run-httpclient.bat @@ -0,0 +1 @@ +java --module-path mods -m com.baeldung.httpclient/com.baeldung.httpclient.HttpClientExample \ No newline at end of file diff --git a/core-java-9/src/modules/com.baeldung.httpclient/com/baeldung/httpclient/HttpClientExample.java b/core-java-9/src/modules/com.baeldung.httpclient/com/baeldung/httpclient/HttpClientExample.java index 44c55715c7..de08c2164e 100644 --- a/core-java-9/src/modules/com.baeldung.httpclient/com/baeldung/httpclient/HttpClientExample.java +++ b/core-java-9/src/modules/com.baeldung.httpclient/com/baeldung/httpclient/HttpClientExample.java @@ -28,7 +28,7 @@ public class HttpClientExample { public static void main(String[] args) throws Exception { httpGetRequest(); - httpPosttRequest(); + httpPostRequest(); asynchronousRequest(); asynchronousMultipleRequests(); } @@ -36,14 +36,15 @@ public class HttpClientExample { public static 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(); + HttpRequest request = HttpRequest.newBuilder(httpURI).GET() + .headers("Accept-Enconding", "gzip, deflate").build(); HttpResponse response = client.send(request, HttpResponse.BodyHandler.asString()); String responseBody = response.body(); int responseStatusCode = response.statusCode(); System.out.println(responseBody); } - public static void httpPosttRequest() throws URISyntaxException, IOException, InterruptedException { + public static void httpPostRequest() throws URISyntaxException, IOException, InterruptedException { HttpClient client = HttpClient .newBuilder() .build();