Files
spring-boot-rest/apache-httpclient-2/src/test/java/com/baeldung/httpclient/readresponsebodystring/HttpUrlConnectionUnitTest.java
freelansam 2cc0b34f4f JAVA-10903: Rename HttpClient Modules to Apache HttpClient (#12070)
* JAVA-10903: rename httpclient to apache-httpclient

* JAVA-10903: rename httpclient-2 to apache-httpclient

* JAVA-10903: update main pom
2022-04-20 17:53:00 +05:30

35 lines
1.0 KiB
Java

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());
}
}