File tree Expand file tree Collapse file tree
java/com/baeldung/httpclient/readresponsebodystring Expand file tree Collapse file tree Original file line number Diff line number Diff line change 2626 </exclusion >
2727 </exclusions >
2828 </dependency >
29+ <dependency >
30+ <groupId >org.apache.httpcomponents.client5</groupId >
31+ <artifactId >httpclient5</artifactId >
32+ <version >${httpclient5.version} </version >
33+ </dependency >
2934 <!-- rest template -->
3035 <dependency >
3136 <groupId >org.springframework.boot</groupId >
6368
6469 <properties >
6570 <httpclient .version>4.5.8</httpclient .version>
71+ <httpclient5 .version>5.1</httpclient5 .version>
6672 <maven .compiler.source.version>11</maven .compiler.source.version>
6773 <maven .compiler.target.version>11</maven .compiler.target.version>
6874 <spring-boot .version>2.1.7.RELEASE</spring-boot .version>
Original file line number Diff line number Diff line change 1+ package com .baeldung .httpclient .readresponsebodystring ;
2+
3+ import org .apache .hc .client5 .http .classic .methods .HttpGet ;
4+ import org .apache .hc .client5 .http .impl .classic .CloseableHttpClient ;
5+ import org .apache .hc .client5 .http .impl .classic .CloseableHttpResponse ;
6+ import org .apache .hc .client5 .http .impl .classic .HttpClients ;
7+ import org .apache .hc .core5 .http .HttpEntity ;
8+ import org .apache .hc .core5 .http .ParseException ;
9+ import org .apache .hc .core5 .http .io .entity .EntityUtils ;
10+ import org .junit .Test ;
11+ import org .slf4j .Logger ;
12+ import org .slf4j .LoggerFactory ;
13+
14+ import java .io .IOException ;
15+
16+ public class ApacheHttpClient5UnitTest {
17+ private final Logger logger = LoggerFactory .getLogger (this .getClass ());
18+ public static final String DUMMY_URL = "https://postman-echo.com/get" ;
19+
20+ @ Test
21+ public void whenUseApacheHttpClient_thenCorrect () throws IOException , ParseException {
22+ HttpGet request = new HttpGet (DUMMY_URL );
23+
24+ try (CloseableHttpClient client = HttpClients .createDefault (); CloseableHttpResponse response = client .execute (request )) {
25+ HttpEntity entity = response .getEntity ();
26+ logger .debug ("Response -> {}" , EntityUtils .toString (entity ));
27+ }
28+ }
29+ }
Original file line number Diff line number Diff line change 77import org .apache .http .impl .client .HttpClients ;
88import org .apache .http .util .EntityUtils ;
99import org .junit .Test ;
10+ import org .slf4j .Logger ;
11+ import org .slf4j .LoggerFactory ;
1012
1113import java .io .IOException ;
1214
1315public class ApacheHttpClientUnitTest {
16+ private final Logger logger = LoggerFactory .getLogger (this .getClass ());
1417 public static final String DUMMY_URL = "https://postman-echo.com/get" ;
1518
1619 @ Test
@@ -19,8 +22,7 @@ public void whenUseApacheHttpClient_thenCorrect() throws IOException {
1922
2023 try (CloseableHttpClient client = HttpClients .createDefault (); CloseableHttpResponse response = client .execute (request )) {
2124 HttpEntity entity = response .getEntity ();
22- String result = EntityUtils .toString (entity );
23- System .out .println ("Response -> " + result );
25+ logger .debug ("Response -> {}" , EntityUtils .toString (entity ));
2426 }
2527 }
2628}
Original file line number Diff line number Diff line change 1+ <configuration debug =" false" >
2+ <appender name =" stdout" class =" ch.qos.logback.core.ConsoleAppender" >
3+ <encoder >
4+ <pattern >%date [%level] %logger - %msg %n</pattern >
5+ </encoder >
6+ </appender >
7+
8+ <logger name =" com.baeldung.httpclient.readresponsebodystring" level =" debug" />
9+ <logger name =" org.apache.http" level =" debug" />
10+ <logger name =" org.apache.hc.client5.http" level =" debug" />
11+
12+ <root level =" WARN" >
13+ <appender-ref ref =" stdout" />
14+ </root >
15+ </configuration >
You can’t perform that action at this time.
0 commit comments