File tree Expand file tree Collapse file tree
src/test/java/com/baeldung/httpclient/readresponsebodystring Expand file tree Collapse file tree Original file line number Diff line number Diff line change 44 <modelVersion >4.0.0</modelVersion >
55 <artifactId >httpclient-2</artifactId >
66 <version >0.1-SNAPSHOT</version >
7+ <name >httpclient-2</name >
78
89 <parent >
910 <groupId >com.baeldung</groupId >
1314 </parent >
1415
1516 <dependencies >
17+ <!-- http client -->
1618 <dependency >
1719 <groupId >org.apache.httpcomponents</groupId >
1820 <artifactId >httpclient</artifactId >
2426 </exclusion >
2527 </exclusions >
2628 </dependency >
29+
30+ <!-- rest template -->
31+ <dependency >
32+ <groupId >org.springframework.boot</groupId >
33+ <artifactId >spring-boot-starter-web</artifactId >
34+ <version >${spring-boot.version} </version >
35+ </dependency >
36+ <dependency >
37+ <groupId >org.springframework.boot</groupId >
38+ <artifactId >spring-boot-starter-test</artifactId >
39+ <version >${spring-boot.version} </version >
40+ <scope >test</scope >
41+ </dependency >
2742 </dependencies >
2843
2944 <build >
3449 <filtering >true</filtering >
3550 </resource >
3651 </resources >
52+ <plugins >
53+ <plugin >
54+ <groupId >org.apache.maven.plugins</groupId >
55+ <artifactId >maven-compiler-plugin</artifactId >
56+ <version >${maven-compiler-plugin.version} </version >
57+ <configuration >
58+ <source >${maven.compiler.source.version} </source >
59+ <target >${maven.compiler.target.version} </target >
60+ </configuration >
61+ </plugin >
62+ </plugins >
3763 </build >
3864
3965 <properties >
4066 <httpclient .version>4.5.8</httpclient .version>
67+ <maven .compiler.source.version>11</maven .compiler.source.version>
68+ <maven .compiler.target.version>11</maven .compiler.target.version>
69+ <spring-boot .version>2.1.7.RELEASE</spring-boot .version>
4170 </properties >
4271
4372</project >
Original file line number Diff line number Diff line change 1+ package com .baeldung .httpclient .readresponsebodystring ;
2+
3+ import org .apache .http .HttpEntity ;
4+ import org .apache .http .client .methods .CloseableHttpResponse ;
5+ import org .apache .http .client .methods .HttpGet ;
6+ import org .apache .http .impl .client .CloseableHttpClient ;
7+ import org .apache .http .impl .client .HttpClients ;
8+ import org .apache .http .util .EntityUtils ;
9+ import org .junit .Test ;
10+
11+ import java .io .IOException ;
12+
13+ public class ApacheHttpClientUnitTest {
14+ public static final String DUMMY_URL = "https://postman-echo.com/get" ;
15+
16+ @ Test
17+ public void whenUseApacheHttpClient_thenCorrect () throws IOException {
18+ HttpGet request = new HttpGet (DUMMY_URL );
19+
20+ try (CloseableHttpClient client = HttpClients .createDefault (); CloseableHttpResponse response = client .execute (request )) {
21+ HttpEntity entity = response .getEntity ();
22+ String result = EntityUtils .toString (entity );
23+ System .out .println ("Response -> " + result );
24+ }
25+ }
26+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .httpclient .readresponsebodystring ;
2+
3+ import org .junit .Test ;
4+
5+ import java .io .IOException ;
6+ import java .net .URI ;
7+ import java .net .http .HttpClient ;
8+ import java .net .http .HttpRequest ;
9+ import java .net .http .HttpResponse ;
10+
11+ public class HttpClientUnitTest {
12+ public static final String DUMMY_URL = "https://postman-echo.com/get" ;
13+
14+ @ Test
15+ public void whenUseHttpClient_thenCorrect () throws IOException , InterruptedException {
16+ HttpClient client = HttpClient .newHttpClient ();
17+ HttpRequest request = HttpRequest .newBuilder ().uri (URI .create (DUMMY_URL )).build ();
18+
19+ // synchronous response
20+ HttpResponse <String > response = client .send (request , HttpResponse .BodyHandlers .ofString ());
21+ System .out .println (response .body ());
22+
23+ // asynchronous response
24+ client .sendAsync (request , HttpResponse .BodyHandlers .ofString ())
25+ .thenApply (HttpResponse ::body )
26+ .thenAccept (System .out ::println )
27+ .join ();
28+ }
29+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .httpclient .readresponsebodystring ;
2+
3+ import org .junit .Assert ;
4+ import org .junit .Test ;
5+
6+ import java .io .BufferedReader ;
7+ import java .io .IOException ;
8+ import java .io .InputStream ;
9+ import java .io .InputStreamReader ;
10+ import java .net .HttpURLConnection ;
11+ import java .net .URL ;
12+
13+ public class HttpUrlConnectionUnitTest {
14+
15+ public static final String DUMMY_URL = "https://postman-echo.com/get" ;
16+
17+ @ Test
18+ public void whenUseHttpUrlConnection_thenCorrect () throws IOException {
19+ HttpURLConnection connection = (HttpURLConnection ) new URL (DUMMY_URL ).openConnection ();
20+
21+ InputStream inputStream = connection .getInputStream ();
22+
23+ BufferedReader in = new BufferedReader (new InputStreamReader (inputStream ));
24+ StringBuilder response = new StringBuilder ();
25+ String currentLine ;
26+
27+ while ((currentLine = in .readLine ()) != null )
28+ response .append (currentLine );
29+
30+ in .close ();
31+ Assert .assertNotNull (response .toString ());
32+ System .out .println ("Response -> " + response .toString ());
33+ }
34+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .httpclient .readresponsebodystring ;
2+
3+ import org .junit .Test ;
4+ import org .springframework .web .client .RestTemplate ;
5+
6+ public class SpringRestTemplateUnitTest {
7+
8+ public static final String DUMMY_URL = "https://postman-echo.com/get" ;
9+
10+ @ Test
11+ public void whenUseRestTemplate_thenCorrect () {
12+ RestTemplate restTemplate = new RestTemplate ();
13+ String response = restTemplate .getForObject (DUMMY_URL , String .class );
14+ System .out .println (response );
15+ }
16+
17+ }
Original file line number Diff line number Diff line change 424424 <module >hazelcast</module >
425425 <module >helidon</module >
426426 <module >httpclient</module >
427- <module >httpclient-2</module >
427+ <!-- < module>httpclient-2</module> --><!-- to be fixed in http://team.baeldung.com/browse/JAVA-2824 -- >
428428 <module >httpclient-simple</module >
429429 <module >hystrix</module >
430430
936936 <module >hazelcast</module >
937937 <module >helidon</module >
938938 <module >httpclient</module >
939- <module >httpclient-2</module >
939+ <!-- < module>httpclient-2</module> --><!-- to be fixed in http://team.baeldung.com/browse/JAVA-2824 -- >
940940 <module >httpclient-simple</module >
941941 <module >hystrix</module >
942942
You can’t perform that action at this time.
0 commit comments