diff --git a/httpclient-simple/pom.xml b/httpclient-simple/pom.xml
index 45da1e7a39..c39983564f 100644
--- a/httpclient-simple/pom.xml
+++ b/httpclient-simple/pom.xml
@@ -112,6 +112,18 @@
+
+
+ org.apache.httpcomponents.client5
+ httpclient5-fluent
+ ${httpclient5-fluent.version}
+
+
+ commons-logging
+ commons-logging
+
+
+
org.apache.commons
@@ -308,6 +320,7 @@
5.2
5.2
+ 5.2
1.6.1
diff --git a/httpclient-simple/src/test/java/com/baeldung/httpclient/HttpClientPostingLiveTest.java b/httpclient-simple/src/test/java/com/baeldung/httpclient/HttpClientPostingLiveTest.java
index f5dff8d757..b30921c990 100644
--- a/httpclient-simple/src/test/java/com/baeldung/httpclient/HttpClientPostingLiveTest.java
+++ b/httpclient-simple/src/test/java/com/baeldung/httpclient/HttpClientPostingLiveTest.java
@@ -1,74 +1,88 @@
package com.baeldung.httpclient;
-import org.apache.http.HttpEntity;
-import org.apache.http.HttpResponse;
-import org.apache.http.NameValuePair;
-import org.apache.http.auth.AuthenticationException;
-import org.apache.http.auth.UsernamePasswordCredentials;
-import org.apache.http.client.entity.UrlEncodedFormEntity;
-import org.apache.http.client.fluent.Form;
-import org.apache.http.client.fluent.Request;
-import org.apache.http.client.methods.CloseableHttpResponse;
-import org.apache.http.client.methods.HttpPost;
-import org.apache.http.entity.ContentType;
-import org.apache.http.entity.StringEntity;
-import org.apache.http.entity.mime.MultipartEntityBuilder;
-import org.apache.http.impl.auth.BasicScheme;
-import org.apache.http.impl.client.CloseableHttpClient;
-import org.apache.http.impl.client.HttpClients;
-import org.apache.http.message.BasicNameValuePair;
-import org.junit.Test;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.equalTo;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+
+import org.junit.jupiter.api.Test;
+
+import org.apache.hc.client5.http.auth.AuthScope;
+import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
+import org.apache.hc.client5.http.classic.methods.HttpPost;
+import org.apache.hc.client5.http.entity.UrlEncodedFormEntity;
+import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder;
+import org.apache.hc.client5.http.fluent.Form;
+import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider;
+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.HttpClients;
+import org.apache.hc.core5.http.ContentType;
+import org.apache.hc.core5.http.HttpEntity;
+import org.apache.hc.core5.http.HttpResponse;
+import org.apache.hc.core5.http.HttpStatus;
+import org.apache.hc.core5.http.NameValuePair;
+import org.apache.hc.client5.http.fluent.Request;
+import org.apache.hc.core5.http.io.entity.StringEntity;
+import org.apache.hc.core5.http.message.BasicNameValuePair;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
-import static org.hamcrest.Matchers.equalTo;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertThat;
+import com.baeldung.handler.CustomHttpClientResponseHandler;
/*
* NOTE : Need module spring-rest to be running
*/
-public class HttpClientPostingLiveTest {
+class HttpClientPostingLiveTest {
private static final String SAMPLE_URL = "http://www.example.com";
private static final String URL_SECURED_BY_BASIC_AUTHENTICATION = "http://browserspy.dk/password-ok.php";
private static final String DEFAULT_USER = "test";
private static final String DEFAULT_PASS = "test";
@Test
- public void whenSendPostRequestUsingHttpClient_thenCorrect() throws IOException {
- final CloseableHttpClient client = HttpClients.createDefault();
+ void whenSendPostRequestUsingHttpClient_thenCorrect() throws IOException {
final HttpPost httpPost = new HttpPost(SAMPLE_URL);
-
final List params = new ArrayList();
params.add(new BasicNameValuePair("username", DEFAULT_USER));
params.add(new BasicNameValuePair("password", DEFAULT_PASS));
httpPost.setEntity(new UrlEncodedFormEntity(params));
- final CloseableHttpResponse response = client.execute(httpPost);
- assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
- client.close();
+ try (CloseableHttpClient client = HttpClients.createDefault();
+ CloseableHttpResponse response = (CloseableHttpResponse) client
+ .execute(httpPost, new CustomHttpClientResponseHandler())) {
+
+ final int statusCode = response.getCode();
+ assertThat(statusCode, equalTo(HttpStatus.SC_OK));
+ }
}
@Test
- public void whenSendPostRequestWithAuthorizationUsingHttpClient_thenCorrect() throws IOException, AuthenticationException {
- final CloseableHttpClient client = HttpClients.createDefault();
+ void whenSendPostRequestWithAuthorizationUsingHttpClient_thenCorrect() throws IOException {
final HttpPost httpPost = new HttpPost(URL_SECURED_BY_BASIC_AUTHENTICATION);
-
httpPost.setEntity(new StringEntity("test post"));
- final UsernamePasswordCredentials creds = new UsernamePasswordCredentials(DEFAULT_USER, DEFAULT_PASS);
- httpPost.addHeader(new BasicScheme().authenticate(creds, httpPost, null));
- final CloseableHttpResponse response = client.execute(httpPost);
- assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
- client.close();
+ final BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
+ final UsernamePasswordCredentials credentials =
+ new UsernamePasswordCredentials(DEFAULT_USER, DEFAULT_PASS.toCharArray());
+
+ credsProvider.setCredentials(new AuthScope(URL_SECURED_BY_BASIC_AUTHENTICATION, 80) ,credentials);
+
+ try (CloseableHttpClient client = HttpClients.custom()
+ .setDefaultCredentialsProvider(credsProvider)
+ .build();
+
+ CloseableHttpResponse response = (CloseableHttpResponse) client
+ .execute(httpPost, new CustomHttpClientResponseHandler())) {
+
+ final int statusCode = response.getCode();
+ assertThat(statusCode, equalTo(HttpStatus.SC_OK));
+ }
}
@Test
- public void whenPostJsonUsingHttpClient_thenCorrect() throws IOException {
- final CloseableHttpClient client = HttpClients.createDefault();
+ void whenPostJsonUsingHttpClient_thenCorrect() throws IOException {
final HttpPost httpPost = new HttpPost(SAMPLE_URL);
final String json = "{\"id\":1,\"name\":\"John\"}";
@@ -77,68 +91,91 @@ public class HttpClientPostingLiveTest {
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
- final CloseableHttpResponse response = client.execute(httpPost);
- assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
- client.close();
+ try (CloseableHttpClient client = HttpClients.createDefault();
+ CloseableHttpResponse response = (CloseableHttpResponse) client
+ .execute(httpPost, new CustomHttpClientResponseHandler())) {
+
+ final int statusCode = response.getCode();
+ assertThat(statusCode, equalTo(HttpStatus.SC_OK));
+ }
}
@Test
- public void whenPostFormUsingHttpClientFluentAPI_thenCorrect() throws IOException {
- final HttpResponse response = Request.Post(SAMPLE_URL).bodyForm(Form.form().add("username", DEFAULT_USER).add("password", DEFAULT_PASS).build()).execute().returnResponse();
+ void whenPostFormUsingHttpClientFluentAPI_thenCorrect() throws IOException {
+ Request request = Request.post(SAMPLE_URL)
+ .bodyForm(Form.form()
+ .add("username", DEFAULT_USER)
+ .add("password", DEFAULT_PASS)
+ .build());
- assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
+ HttpResponse response = request.execute()
+ .returnResponse();
+ assertThat(response.getCode(), equalTo(HttpStatus.SC_OK));
}
@Test
- public void whenSendMultipartRequestUsingHttpClient_thenCorrect() throws IOException {
- final CloseableHttpClient client = HttpClients.createDefault();
+ void whenSendMultipartRequestUsingHttpClient_thenCorrect() throws IOException {
final HttpPost httpPost = new HttpPost(SAMPLE_URL);
final MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addTextBody("username", DEFAULT_USER);
builder.addTextBody("password", DEFAULT_PASS);
- builder.addBinaryBody("file", new File("src/test/resources/test.in"), ContentType.APPLICATION_OCTET_STREAM, "file.ext");
+ builder.addBinaryBody(
+ "file", new File("src/test/resources/test.in"), ContentType.APPLICATION_OCTET_STREAM, "file.ext");
+
+ final HttpEntity multipart = builder.build();
+ httpPost.setEntity(multipart);
+
+ try (CloseableHttpClient client = HttpClients.createDefault();
+ CloseableHttpResponse response = (CloseableHttpResponse) client
+ .execute(httpPost, new CustomHttpClientResponseHandler())) {
+
+ final int statusCode = response.getCode();
+ assertThat(statusCode, equalTo(HttpStatus.SC_OK));
+ }
+ }
+
+ @Test
+ void whenUploadFileUsingHttpClient_thenCorrect() throws IOException {
+ final HttpPost httpPost = new HttpPost(SAMPLE_URL);
+
+ final MultipartEntityBuilder builder = MultipartEntityBuilder.create();
+ builder.addBinaryBody(
+ "file", new File("src/test/resources/test.in"), ContentType.APPLICATION_OCTET_STREAM, "file.ext");
final HttpEntity multipart = builder.build();
httpPost.setEntity(multipart);
- final CloseableHttpResponse response = client.execute(httpPost);
- assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
- client.close();
+ try (CloseableHttpClient client = HttpClients.createDefault();
+ CloseableHttpResponse response = (CloseableHttpResponse) client
+ .execute(httpPost, new CustomHttpClientResponseHandler())) {
+
+ final int statusCode = response.getCode();
+ assertThat(statusCode, equalTo(HttpStatus.SC_OK));
+ }
}
@Test
- public void whenUploadFileUsingHttpClient_thenCorrect() throws IOException {
- final CloseableHttpClient client = HttpClients.createDefault();
+ void whenGetUploadFileProgressUsingHttpClient_thenCorrect() throws IOException {
final HttpPost httpPost = new HttpPost(SAMPLE_URL);
final MultipartEntityBuilder builder = MultipartEntityBuilder.create();
- builder.addBinaryBody("file", new File("src/test/resources/test.in"), ContentType.APPLICATION_OCTET_STREAM, "file.ext");
+ builder.addBinaryBody(
+ "file", new File("src/test/resources/test.in"), ContentType.APPLICATION_OCTET_STREAM, "file.ext");
final HttpEntity multipart = builder.build();
- httpPost.setEntity(multipart);
-
- final CloseableHttpResponse response = client.execute(httpPost);
- assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
- client.close();
- }
-
- @Test
- public void whenGetUploadFileProgressUsingHttpClient_thenCorrect() throws IOException {
- final CloseableHttpClient client = HttpClients.createDefault();
- final HttpPost httpPost = new HttpPost(SAMPLE_URL);
-
- final MultipartEntityBuilder builder = MultipartEntityBuilder.create();
- builder.addBinaryBody("file", new File("src/test/resources/test.in"), ContentType.APPLICATION_OCTET_STREAM, "file.ext");
- final HttpEntity multipart = builder.build();
-
- final ProgressEntityWrapper.ProgressListener pListener = percentage -> assertFalse(Float.compare(percentage, 100) > 0);
+ final ProgressEntityWrapper.ProgressListener pListener =
+ percentage -> assertFalse(Float.compare(percentage, 100) > 0);
httpPost.setEntity(new ProgressEntityWrapper(multipart, pListener));
- final CloseableHttpResponse response = client.execute(httpPost);
- assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
- client.close();
+ try (CloseableHttpClient client = HttpClients.createDefault();
+ CloseableHttpResponse response = (CloseableHttpResponse) client
+ .execute(httpPost, new CustomHttpClientResponseHandler())) {
+
+ final int statusCode = response.getCode();
+ assertThat(statusCode, equalTo(HttpStatus.SC_OK));
+ }
}
}
\ No newline at end of file
diff --git a/httpclient-simple/src/test/java/com/baeldung/httpclient/ProgressEntityWrapper.java b/httpclient-simple/src/test/java/com/baeldung/httpclient/ProgressEntityWrapper.java
index c7adf51b3e..0fbc6e5dcd 100644
--- a/httpclient-simple/src/test/java/com/baeldung/httpclient/ProgressEntityWrapper.java
+++ b/httpclient-simple/src/test/java/com/baeldung/httpclient/ProgressEntityWrapper.java
@@ -4,8 +4,8 @@ import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
-import org.apache.http.HttpEntity;
-import org.apache.http.entity.HttpEntityWrapper;
+import org.apache.hc.core5.http.HttpEntity;
+import org.apache.hc.core5.http.io.entity.HttpEntityWrapper;
public class ProgressEntityWrapper extends HttpEntityWrapper {
private final ProgressListener listener;