[JAVA-15033] Update article Apache HttpClient 4 Cookbook (#14266)
This commit is contained in:
parent
84f60d9c86
commit
18da9d53f7
@ -0,0 +1,18 @@
|
|||||||
|
package com.baeldung.httpclient.httpclient;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
|
||||||
|
|
||||||
|
public final class ClientUtil {
|
||||||
|
|
||||||
|
private ClientUtil(){}
|
||||||
|
|
||||||
|
public static void closeClient(CloseableHttpClient client) throws IOException {
|
||||||
|
if (client == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
client.close();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,211 @@
|
|||||||
|
package com.baeldung.httpclient.httpclient;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.hamcrest.Matchers.notNullValue;
|
||||||
|
import static org.hamcrest.Matchers.emptyArray;
|
||||||
|
import static org.hamcrest.Matchers.not;
|
||||||
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.AfterEach;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import org.apache.hc.client5.http.classic.methods.HttpPost;
|
||||||
|
import org.apache.hc.client5.http.config.ConnectionConfig;
|
||||||
|
import org.apache.hc.client5.http.config.RequestConfig;
|
||||||
|
import org.apache.hc.client5.http.entity.UrlEncodedFormEntity;
|
||||||
|
import org.apache.hc.client5.http.impl.io.BasicHttpClientConnectionManager;
|
||||||
|
import org.apache.hc.client5.http.classic.methods.HttpGet;
|
||||||
|
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
|
||||||
|
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
|
||||||
|
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
|
||||||
|
import org.apache.hc.client5.http.impl.classic.HttpClients;
|
||||||
|
import org.apache.hc.core5.http.ContentType;
|
||||||
|
import org.apache.hc.core5.http.Header;
|
||||||
|
import org.apache.hc.core5.http.HttpHeaders;
|
||||||
|
import org.apache.hc.core5.http.NameValuePair;
|
||||||
|
import org.apache.hc.core5.http.io.entity.EntityUtils;
|
||||||
|
import org.apache.hc.core5.http.message.BasicNameValuePair;
|
||||||
|
import org.apache.hc.core5.util.Timeout;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.SocketTimeoutException;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
class HttpClientCookBookLiveTest {
|
||||||
|
|
||||||
|
private static final String SAMPLE_GET_URL = "http://www.google.com";
|
||||||
|
private static final String SAMPLE_POST_URL = "http://www.github.com";
|
||||||
|
|
||||||
|
private CloseableHttpClient httpClient;
|
||||||
|
|
||||||
|
private CloseableHttpResponse response;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public final void before() {
|
||||||
|
httpClient = HttpClientBuilder.create().build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterEach
|
||||||
|
public final void after() throws IOException {
|
||||||
|
ClientUtil.closeClient(httpClient);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenGetRequestExecuted_thenCorrectStatusCode() throws IOException {
|
||||||
|
HttpGet httpGet = new HttpGet(SAMPLE_GET_URL);
|
||||||
|
httpClient.execute(httpGet,
|
||||||
|
response -> {
|
||||||
|
assertThat(response.getCode()).isEqualTo(200);
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenGetRequestExecuted_thenCorrectContentMimeType() throws IOException {
|
||||||
|
HttpGet httpGet = new HttpGet(SAMPLE_GET_URL);
|
||||||
|
httpClient.execute(httpGet,
|
||||||
|
response -> {
|
||||||
|
final String contentMimeType = ContentType.parse(response.getEntity().getContentType()).getMimeType();
|
||||||
|
assertThat(contentMimeType).isEqualTo(ContentType.TEXT_HTML.getMimeType());
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenGetRequestExecuted_thenCorrectResponse() throws IOException {
|
||||||
|
HttpGet httpGet = new HttpGet(SAMPLE_GET_URL);
|
||||||
|
httpClient.execute(httpGet,
|
||||||
|
response -> {
|
||||||
|
String bodyAsString = EntityUtils.toString(response.getEntity());
|
||||||
|
assertThat(bodyAsString, notNullValue());
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenConfigureTimeoutOnRequest_thenCorrectResponse() throws IOException {
|
||||||
|
RequestConfig requestConfig = RequestConfig.custom()
|
||||||
|
.setConnectionRequestTimeout(Timeout.ofMilliseconds(2000L))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
CloseableHttpClient httpClient = HttpClientBuilder.create()
|
||||||
|
.setDefaultRequestConfig(requestConfig)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
HttpGet request = new HttpGet(SAMPLE_GET_URL);
|
||||||
|
request.setConfig(requestConfig);
|
||||||
|
|
||||||
|
httpClient.execute(request,
|
||||||
|
response -> {
|
||||||
|
assertThat(response.getCode()).isEqualTo(200);
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenLowSocketTimeOut_whenExecutingRequestWithTimeout_thenException() throws IOException {
|
||||||
|
ConnectionConfig connConfig = ConnectionConfig.custom()
|
||||||
|
.setConnectTimeout(1000, TimeUnit.MILLISECONDS)
|
||||||
|
.setSocketTimeout(20, TimeUnit.MILLISECONDS)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
RequestConfig requestConfig = RequestConfig.custom()
|
||||||
|
.setConnectionRequestTimeout(Timeout.ofMilliseconds(2000L))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
BasicHttpClientConnectionManager cm = new BasicHttpClientConnectionManager();
|
||||||
|
cm.setConnectionConfig(connConfig);
|
||||||
|
|
||||||
|
CloseableHttpClient httpClient = HttpClientBuilder.create()
|
||||||
|
.setDefaultRequestConfig(requestConfig)
|
||||||
|
.setConnectionManager(cm)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
HttpGet request = new HttpGet(SAMPLE_GET_URL);
|
||||||
|
|
||||||
|
assertThrows(SocketTimeoutException.class, () -> {
|
||||||
|
httpClient.execute(request, resp -> resp);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenExecutingPostRequest_thenNoExceptions() throws IOException {
|
||||||
|
final HttpPost httpPost = new HttpPost(SAMPLE_POST_URL);
|
||||||
|
httpClient.execute(httpPost,
|
||||||
|
response -> {
|
||||||
|
assertThat(response.getCode()).isEqualTo(200);
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenParametersAddedToRequest_thenCorrect() throws IOException {
|
||||||
|
final HttpPost httpPost = new HttpPost(SAMPLE_POST_URL);
|
||||||
|
final List<NameValuePair> params = new ArrayList<NameValuePair>();
|
||||||
|
params.add(new BasicNameValuePair("key1", "value1"));
|
||||||
|
params.add(new BasicNameValuePair("key2", "value2"));
|
||||||
|
httpPost.setEntity(new UrlEncodedFormEntity(params, Charset.defaultCharset()));
|
||||||
|
|
||||||
|
httpClient.execute(httpPost, response -> {
|
||||||
|
assertThat(response.getCode()).isEqualTo(200);
|
||||||
|
return response;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenRedirectsAreDisabled_whenConsumingUrlWhichRedirects_thenNotRedirected() throws IOException {
|
||||||
|
CloseableHttpClient client = HttpClientBuilder.create()
|
||||||
|
.disableRedirectHandling()
|
||||||
|
.build();
|
||||||
|
client.execute(new HttpGet("http://t.co/I5YYd9tddw"), response -> {
|
||||||
|
assertThat(response.getCode()).isEqualTo(301);
|
||||||
|
return response;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenHeadersAddedToRequest_thenCorrect() throws IOException {
|
||||||
|
HttpGet request = new HttpGet(SAMPLE_GET_URL);
|
||||||
|
request.addHeader(HttpHeaders.ACCEPT, "application/xml");
|
||||||
|
httpClient.execute(request,
|
||||||
|
response -> {
|
||||||
|
assertThat(response.getCode()).isEqualTo(200);
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenRequestWasSet_whenAnalyzingTheHeadersOfTheResponse_thenCorrect() throws IOException {
|
||||||
|
HttpGet httpGet = new HttpGet(SAMPLE_GET_URL);
|
||||||
|
httpClient.execute(httpGet,
|
||||||
|
response -> {
|
||||||
|
Header[] headers = response.getHeaders(HttpHeaders.CONTENT_TYPE);
|
||||||
|
assertThat(headers, not(emptyArray()));
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenAutoClosableClient_thenCorrect() throws IOException {
|
||||||
|
HttpGet httpGet = new HttpGet(SAMPLE_GET_URL);
|
||||||
|
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
||||||
|
httpClient.execute(httpGet, resp -> {
|
||||||
|
assertThat(resp.getCode()).isEqualTo(200);
|
||||||
|
return resp;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
package com.baeldung.httpclient;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.apache.http.HttpEntity;
|
||||||
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||||
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
|
|
||||||
|
public final class ClientUtil {
|
||||||
|
|
||||||
|
private ClientUtil(){}
|
||||||
|
|
||||||
|
public static void closeClient(CloseableHttpClient client) throws IOException {
|
||||||
|
if (client == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
client.close();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,166 @@
|
|||||||
|
package com.baeldung.httpclient;
|
||||||
|
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.AfterEach;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
|
import static org.hamcrest.Matchers.notNullValue;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
|
import static org.hamcrest.Matchers.emptyArray;
|
||||||
|
import static org.hamcrest.Matchers.not;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.net.SocketTimeoutException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.apache.http.Consts;
|
||||||
|
import org.apache.http.Header;
|
||||||
|
import org.apache.http.HttpEntity;
|
||||||
|
import org.apache.http.HttpHeaders;
|
||||||
|
import org.apache.http.NameValuePair;
|
||||||
|
|
||||||
|
import org.apache.http.client.config.RequestConfig;
|
||||||
|
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||||
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||||
|
import org.apache.http.client.methods.HttpGet;
|
||||||
|
import org.apache.http.client.methods.HttpPost;
|
||||||
|
import org.apache.http.entity.ContentType;
|
||||||
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
|
import org.apache.http.impl.client.HttpClientBuilder;
|
||||||
|
import org.apache.http.message.BasicNameValuePair;
|
||||||
|
import org.apache.http.util.EntityUtils;
|
||||||
|
|
||||||
|
class HttpClientCookBookV4LiveTest {
|
||||||
|
|
||||||
|
private static final String SAMPLE_GET_URL = "http://www.google.com";
|
||||||
|
private static final String SAMPLE_POST_URL = "http://www.github.com";
|
||||||
|
|
||||||
|
private CloseableHttpClient client;
|
||||||
|
|
||||||
|
private CloseableHttpResponse response;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public final void before() {
|
||||||
|
client = HttpClientBuilder.create().build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterEach
|
||||||
|
public final void after() throws IllegalStateException, IOException {
|
||||||
|
ResponseUtil.closeResponse(response);
|
||||||
|
ClientUtil.closeClient(client);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenGetRequestExecuted_thenCorrectStatusCode() throws IOException {
|
||||||
|
HttpGet httpGet = new HttpGet(SAMPLE_GET_URL);
|
||||||
|
response = client.execute(httpGet);
|
||||||
|
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenGetRequestExecuted_thenCorrectContentMimeType() throws IOException {
|
||||||
|
CloseableHttpResponse response = client.execute(new HttpGet(SAMPLE_GET_URL));
|
||||||
|
String contentMimeType = ContentType.getOrDefault(response.getEntity()).getMimeType();
|
||||||
|
assertThat(contentMimeType, equalTo(ContentType.TEXT_HTML.getMimeType()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenGetRequestExecuted_thenCorrectResponse() throws IOException {
|
||||||
|
CloseableHttpResponse response = client.execute(new HttpGet(SAMPLE_GET_URL));
|
||||||
|
String bodyAsString = EntityUtils.toString(response.getEntity());
|
||||||
|
assertThat(bodyAsString, notNullValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenLowSocketTimeOut_whenExecutingRequestWithTimeout_thenException() throws IOException {
|
||||||
|
RequestConfig requestConfig = RequestConfig.custom()
|
||||||
|
.setConnectionRequestTimeout(1000)
|
||||||
|
.setConnectTimeout(1000)
|
||||||
|
.setSocketTimeout(20)
|
||||||
|
.build();
|
||||||
|
HttpGet request = new HttpGet(SAMPLE_POST_URL);
|
||||||
|
request.setConfig(requestConfig);
|
||||||
|
|
||||||
|
assertThrows(SocketTimeoutException.class, () -> {
|
||||||
|
response = client.execute(request);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenLowSocketTimeOut_whenSettingTimeoutOnTheClient_thenException(){
|
||||||
|
RequestConfig requestConfig = RequestConfig.custom()
|
||||||
|
.setConnectionRequestTimeout(1000)
|
||||||
|
.setConnectTimeout(1000)
|
||||||
|
.setSocketTimeout(20).build();
|
||||||
|
HttpClientBuilder builder = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig);
|
||||||
|
|
||||||
|
client = builder.build();
|
||||||
|
HttpGet request = new HttpGet(SAMPLE_GET_URL);
|
||||||
|
|
||||||
|
assertThrows(SocketTimeoutException.class, () -> {
|
||||||
|
response = client.execute(request);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenExecutingPostRequest_thenNoExceptions() throws IOException {
|
||||||
|
response = client.execute(new HttpPost(SAMPLE_POST_URL));
|
||||||
|
assertThat(response.getStatusLine().getStatusCode(), equalTo(301));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenParametersAddedToRequest_thenCorrect() throws IOException {
|
||||||
|
final HttpPost httpPost = new HttpPost(SAMPLE_POST_URL);
|
||||||
|
final List<NameValuePair> params = new ArrayList<NameValuePair>();
|
||||||
|
params.add(new BasicNameValuePair("key1", "value1"));
|
||||||
|
params.add(new BasicNameValuePair("key2", "value2"));
|
||||||
|
httpPost.setEntity(new UrlEncodedFormEntity(params, Consts.UTF_8));
|
||||||
|
|
||||||
|
response = client.execute(new HttpPost(SAMPLE_POST_URL));
|
||||||
|
assertThat(response.getStatusLine().getStatusCode(), equalTo(301));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenRedirectsAreDisabled_whenConsumingUrlWhichRedirects_thenNotRedirected() throws IOException {
|
||||||
|
CloseableHttpClient client = HttpClientBuilder.create()
|
||||||
|
.disableRedirectHandling()
|
||||||
|
.build();
|
||||||
|
CloseableHttpResponse response = client.execute(new HttpGet("http://t.co/I5YYd9tddw"));
|
||||||
|
assertThat(response.getStatusLine().getStatusCode(), equalTo(301));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenHeadersAddedToRequest_thenCorrect() throws IOException {
|
||||||
|
HttpGet request = new HttpGet(SAMPLE_GET_URL);
|
||||||
|
request.addHeader(HttpHeaders.ACCEPT, "application/xml");
|
||||||
|
response = client.execute(request);
|
||||||
|
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenRequestWasSet_whenAnalyzingTheHeadersOfTheResponse_thenCorrect() throws IOException {
|
||||||
|
CloseableHttpResponse response = client.execute(new HttpGet(SAMPLE_GET_URL));
|
||||||
|
Header[] headers = response.getHeaders(HttpHeaders.CONTENT_TYPE);
|
||||||
|
assertThat(headers, not(emptyArray()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenStreamIsClosed_thenCloseResponse() throws IOException {
|
||||||
|
response = client.execute(new HttpGet(SAMPLE_GET_URL));
|
||||||
|
try {
|
||||||
|
HttpEntity entity = response.getEntity();
|
||||||
|
if (entity != null) {
|
||||||
|
InputStream instream = entity.getContent();
|
||||||
|
instream.close();
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
response.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user