From e134c020ceb050ecacfb23509f1a8418fe481e41 Mon Sep 17 00:00:00 2001 From: Oussama BEN MAHMOUD Date: Mon, 28 Sep 2020 10:43:07 +0200 Subject: [PATCH] BAEL-4443 Reading HTTP ResponseBody as a String --- httpclient-2/pom.xml | 29 ++++++++++++++++ .../ApacheHttpClientUnitTest.java | 26 ++++++++++++++ .../HttpClientUnitTest.java | 29 ++++++++++++++++ .../HttpUrlConnectionUnitTest.java | 34 +++++++++++++++++++ .../SpringRestTemplateUnitTest.java | 17 ++++++++++ pom.xml | 4 +-- 6 files changed, 137 insertions(+), 2 deletions(-) create mode 100644 httpclient-2/src/test/java/com/baeldung/httpclient/readresponsebodystring/ApacheHttpClientUnitTest.java create mode 100644 httpclient-2/src/test/java/com/baeldung/httpclient/readresponsebodystring/HttpClientUnitTest.java create mode 100644 httpclient-2/src/test/java/com/baeldung/httpclient/readresponsebodystring/HttpUrlConnectionUnitTest.java create mode 100644 httpclient-2/src/test/java/com/baeldung/httpclient/readresponsebodystring/SpringRestTemplateUnitTest.java diff --git a/httpclient-2/pom.xml b/httpclient-2/pom.xml index 1a27d9b5fe..7638c692dc 100644 --- a/httpclient-2/pom.xml +++ b/httpclient-2/pom.xml @@ -4,6 +4,7 @@ 4.0.0 httpclient-2 0.1-SNAPSHOT + httpclient-2 com.baeldung @@ -13,6 +14,7 @@ + org.apache.httpcomponents httpclient @@ -24,6 +26,19 @@ + + + + org.springframework.boot + spring-boot-starter-web + ${spring-boot.version} + + + org.springframework.boot + spring-boot-starter-test + ${spring-boot.version} + test + @@ -34,10 +49,24 @@ true + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + ${maven.compiler.source.version} + ${maven.compiler.target.version} + + + 4.5.8 + 11 + 11 + 2.1.7.RELEASE \ No newline at end of file diff --git a/httpclient-2/src/test/java/com/baeldung/httpclient/readresponsebodystring/ApacheHttpClientUnitTest.java b/httpclient-2/src/test/java/com/baeldung/httpclient/readresponsebodystring/ApacheHttpClientUnitTest.java new file mode 100644 index 0000000000..5a638b2bd5 --- /dev/null +++ b/httpclient-2/src/test/java/com/baeldung/httpclient/readresponsebodystring/ApacheHttpClientUnitTest.java @@ -0,0 +1,26 @@ +package com.baeldung.httpclient.readresponsebodystring; + +import org.apache.http.HttpEntity; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; +import org.junit.Test; + +import java.io.IOException; + +public class ApacheHttpClientUnitTest { + public static final String DUMMY_URL = "https://postman-echo.com/get"; + + @Test + public void whenUseApacheHttpClient_thenCorrect() throws IOException { + HttpGet request = new HttpGet(DUMMY_URL); + + try (CloseableHttpClient client = HttpClients.createDefault(); CloseableHttpResponse response = client.execute(request)) { + HttpEntity entity = response.getEntity(); + String result = EntityUtils.toString(entity); + System.out.println("Response -> " + result); + } + } +} diff --git a/httpclient-2/src/test/java/com/baeldung/httpclient/readresponsebodystring/HttpClientUnitTest.java b/httpclient-2/src/test/java/com/baeldung/httpclient/readresponsebodystring/HttpClientUnitTest.java new file mode 100644 index 0000000000..1dca1bf7c6 --- /dev/null +++ b/httpclient-2/src/test/java/com/baeldung/httpclient/readresponsebodystring/HttpClientUnitTest.java @@ -0,0 +1,29 @@ +package com.baeldung.httpclient.readresponsebodystring; + +import org.junit.Test; + +import java.io.IOException; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; + +public class HttpClientUnitTest { + public static final String DUMMY_URL = "https://postman-echo.com/get"; + + @Test + public void whenUseHttpClient_thenCorrect() throws IOException, InterruptedException { + HttpClient client = HttpClient.newHttpClient(); + HttpRequest request = HttpRequest.newBuilder().uri(URI.create(DUMMY_URL)).build(); + + // synchronous response + HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); + System.out.println(response.body()); + + // asynchronous response + client.sendAsync(request, HttpResponse.BodyHandlers.ofString()) + .thenApply(HttpResponse::body) + .thenAccept(System.out::println) + .join(); + } +} diff --git a/httpclient-2/src/test/java/com/baeldung/httpclient/readresponsebodystring/HttpUrlConnectionUnitTest.java b/httpclient-2/src/test/java/com/baeldung/httpclient/readresponsebodystring/HttpUrlConnectionUnitTest.java new file mode 100644 index 0000000000..54ae887eb4 --- /dev/null +++ b/httpclient-2/src/test/java/com/baeldung/httpclient/readresponsebodystring/HttpUrlConnectionUnitTest.java @@ -0,0 +1,34 @@ +package com.baeldung.httpclient.readresponsebodystring; + +import org.junit.Assert; +import org.junit.Test; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; + +public class HttpUrlConnectionUnitTest { + + public static final String DUMMY_URL = "https://postman-echo.com/get"; + + @Test + public void whenUseHttpUrlConnection_thenCorrect() throws IOException { + HttpURLConnection connection = (HttpURLConnection) new URL(DUMMY_URL).openConnection(); + + InputStream inputStream = connection.getInputStream(); + + BufferedReader in = new BufferedReader(new InputStreamReader(inputStream)); + StringBuilder response = new StringBuilder(); + String currentLine; + + while ((currentLine = in.readLine()) != null) + response.append(currentLine); + + in.close(); + Assert.assertNotNull(response.toString()); + System.out.println("Response -> " + response.toString()); + } +} diff --git a/httpclient-2/src/test/java/com/baeldung/httpclient/readresponsebodystring/SpringRestTemplateUnitTest.java b/httpclient-2/src/test/java/com/baeldung/httpclient/readresponsebodystring/SpringRestTemplateUnitTest.java new file mode 100644 index 0000000000..c59d7662f1 --- /dev/null +++ b/httpclient-2/src/test/java/com/baeldung/httpclient/readresponsebodystring/SpringRestTemplateUnitTest.java @@ -0,0 +1,17 @@ +package com.baeldung.httpclient.readresponsebodystring; + +import org.junit.Test; +import org.springframework.web.client.RestTemplate; + +public class SpringRestTemplateUnitTest { + + public static final String DUMMY_URL = "https://postman-echo.com/get"; + + @Test + public void whenUseRestTemplate_thenCorrect() { + RestTemplate restTemplate = new RestTemplate(); + String response = restTemplate.getForObject(DUMMY_URL, String.class); + System.out.println(response); + } + +} diff --git a/pom.xml b/pom.xml index 065d6abbdd..c93aeffcbd 100644 --- a/pom.xml +++ b/pom.xml @@ -424,7 +424,7 @@ hazelcast helidon httpclient - httpclient-2 + httpclient-simple hystrix @@ -936,7 +936,7 @@ hazelcast helidon httpclient - httpclient-2 + httpclient-simple hystrix