How to Send HTTP Requests in Java Using Apache HttpClient

This article guides Java Developers on how to send HTTP Requests using HTTP Methods (GET and POST) and reads the https response status and content.

You need the httpclient and httpmime jar to manage HTTP requests and responses, and multipart files respectively. Add the following lines of code to your pom.xml to add these dependencies.

<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId
<artifactId>httpclient</artifactId
<version>4.5.10</version>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId
<artifactId>httpmime</artifactId>
<version>4.3.1</version><type>jar</type>
</dependency>
</dependencies>

As discussed, to make HTTP Request and receive responses, there needs to be an existing service. Below are some details about the service that we are going to use for testing purpose:

1. Endpoint : http://httpbin.org/get
Request Type : GET

2. Endpoint : http://httpbin.org/post
Request Type : POST

Create a java class and make the following imports.

import java.io.File;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

The code below demonstrates how to make a GET Request to the 1st endpoint;

public void getRequest() throws IOException {
//Address of service to be consumed
String getEndpoint = "http://httpbin.org/get";

CloseableHttpClient httpclient =
HttpClients.createDefault();

// build httpentity object and assign data you
// desire to send
HttpEntity getData = MultipartEntityBuilder.create()
.addTextBody("name", "Nuhu Ibrahim")
// You can add many more form body
.build();

// build http request and assign httpentity object to
// it that we build above
HttpUriRequest getRequest =
RequestBuilder
.get(getEndpoint)
.setEntity(getData)
.build();

HttpResponse response = httpclient.execute(getRequest);

// displaty the response code
System.out.println("Response Status Code: ");
System.out.println(response
.getStatusLine()
.getStatusCode());

// displaty the response content in as a json string
System.out.println("Response Content: ");
System.out.println(
EntityUtils.toString(response.getEntity()));

}

The code below demonstrates how to make a POST Request to the 2nd endpoint;

public void postRequest() throws IOException {
//Address of service to be consumed
String postEndpoint = "http://httpbin.org/post";

//Specify your own location and file
File testUploadFile =
new File("/Users/nuhuibrahim/Downloads/8.png");

CloseableHttpClient httpclient =
HttpClients.createDefault();

// build httpentity object and assign data you
// desire to send
HttpEntity postData = MultipartEntityBuilder.create()
.addTextBody("name", "Nuhu Ibrahim")
.addBinaryBody("file", testUploadFile)
// You can add many more form body
.build();

// build http request and assign httpentity object to
// it that we build above
HttpUriRequest postRequest =
RequestBuilder
.post(postEndpoint)
.setEntity(postData)
.build();

HttpResponse response = httpclient.execute(postRequest);

// displaty the response code
System.out.println("Response Status Code: ");
System.out.println(response
.getStatusLine()
.getStatusCode());

// displaty the response content in as a json string
System.out.println("Response Content: ");
System.out.println(
EntityUtils.toString(response.getEntity()));
}

Originally published at https://nuhuibrahim.com.