/
NikolayIvkin
/
tutorials1
Обзор
Документация
Войти
/
NikolayIvkin
/
tutorials1
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
apache-httpclient/src/test/java/com/baeldung/httpclient/readresponsebodystring/HttpUrlConnectionUnitTest.java
37 строк
1 KB
Gaetano Piazzolla
JAVA-38565 | moving articles (#17409)
23 авг 2024, 00:30
Не верифицирован
23 авг 2024, 00:30
8a96812
Код
Авторство
О чём код?
package com.baeldung.httpclient.readresponsebodystring; import org.junit.jupiter.api.Test; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URI; import java.net.URISyntaxException; import static org.junit.jupiter.api.Assertions.assertNotNull; public class HttpUrlConnectionUnitTest { public static final String DUMMY_URL = "https://postman-echo.com/get"; @Test void whenUseHttpUrlConnection_thenCorrect() throws IOException, URISyntaxException { HttpURLConnection connection = (HttpURLConnection) new URI(DUMMY_URL).toURL().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(); assertNotNull(response.toString()); System.out.println("Response -> " + response.toString()); } }