/
NikolayIvkin
/
tutorials1
Обзор
Документация
Войти
/
NikolayIvkin
/
tutorials1
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
apache-httpclient4/src/test/java/com/baeldung/httpclient/HttpClientCancelRequestV4LiveTest.java
59 строк
2 KB
panos-kakos
JAVA-17488 (#14694)
13 сен 2023, 13:16
13 сен 2023, 13:16
ac590b3
Код
Авторство
О чём код?
package com.baeldung.httpclient; import java.io.IOException; 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.HttpClientBuilder; import org.apache.http.impl.client.HttpClients; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; class HttpClientCancelRequestV4LiveTest { private static final String SAMPLE_URL = "http://www.github.com"; private CloseableHttpClient instance; private CloseableHttpResponse response; @BeforeEach public final void before() { instance = HttpClientBuilder.create().build(); } @AfterEach public final void after() throws IllegalStateException, IOException { ResponseUtil.closeResponse(response); } @Test final void whenRequestIsCanceled_thenCorrect() throws IOException { instance = HttpClients.custom().build(); final HttpGet request = new HttpGet(SAMPLE_URL); response = instance.execute(request); try { final HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); entity.getContent().close(); } System.out.println("----------------------------------------"); // Do not feel like reading the response body // Call abort on the request object request.abort(); } finally { response.close(); } } }