Apache HttpClient Get

By Arvind Rai, October 15, 2018
This page will walk through Apache HttpClient get example. Apache HttpGet class processes the request URI with HTTP GET method and returns response in the form of an entity. HttpGet provides methods to set headers, remove headers, cancel request and get entity etc. To add query parameters, we need to use URIBuilder. To handle response, HttpClient provides response handler. BasicHttpClientResponseHandler gives response as string and ContentResponseHandler gives response as Content. We can also create custom response handler using HttpClientResponseHandler interface and overriding its handleResponse method. In our example we will access URI using HttpGet, set parameters, headers and create custom response handler.

Technologies Used

Find the technologies being used in our example.
1. Java 9
2. HttpClient5 5.0-beta1
3. Jackson 2.9.4
4. Gradle 4.3.1
5. Eclipse Oxygen

Gradle File

Find the Gradle file used in the example.
build.gradle
apply plugin: 'java'
apply plugin: 'eclipse'
archivesBaseName = 'concretepage'
version = '1' 
repositories {
    mavenCentral()
}
dependencies {
    compile 'org.apache.httpcomponents.client5:httpclient5:5.0-beta1'
    compile 'org.apache.httpcomponents.client5:httpclient5-fluent:5.0-beta1'
    
    compile 'com.fasterxml.jackson.core:jackson-core:2.9.5'
    compile 'com.fasterxml.jackson.core:jackson-databind:2.9.5'
    compile 'com.fasterxml.jackson.core:jackson-annotations:2.9.5'    
} 

HttpGet

Apache HttpGet processes the request URI with HTTP GET method and returns response in the form of an entity.
Step-1: Instantiate CloseableHttpClient.
final CloseableHttpClient httpClient = HttpClients.createDefault(); 
Step-2: Instantiate HttpGet
final HttpGet httpGet = new HttpGet("http://httpbin.org/get"); 
Step-3: Instantiate HttpClientResponseHandler. We can use its implementations BasicHttpClientResponseHandler and ContentResponseHandler or can create custom response handler as following.
HttpClientResponseHandler<String> responseHandler = new HttpClientResponseHandler<>() {
    @Override
    public String handleResponse(ClassicHttpResponse response) throws HttpException, IOException {
       ------
       return responseBody;
    }
}; 
Step-4: Execute the request and receive the response.
final String responseBody = httpClient.execute(httpGet, responseHandler); 
Now we will create an example with HttpGet and custom response handler to return response as string.
ClientWithString.java
package com.concretepage;
import java.io.IOException;
import org.apache.hc.client5.http.ClientProtocolException;
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.HttpClients;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.HttpException;
import org.apache.hc.core5.http.HttpStatus;
import org.apache.hc.core5.http.io.HttpClientResponseHandler;
import org.apache.hc.core5.http.io.entity.EntityUtils;

public class ClientWithString {
  public final static void main(final String[] args) throws Exception {
	final CloseableHttpClient httpClient = HttpClients.createDefault();
	try (httpClient) {
	  final HttpGet httpGet = new HttpGet("http://httpbin.org/get");

	  HttpClientResponseHandler<String> responseHandler = new HttpClientResponseHandler<>() {
		@Override
		public String handleResponse(ClassicHttpResponse response) throws HttpException, IOException {
		  final int status = response.getCode();
		  System.out.println("Status: " + status);
		  if (status >= HttpStatus.SC_SUCCESS && status < HttpStatus.SC_REDIRECTION) {
			try (HttpEntity httpEntity = response.getEntity()) {
			  String responseBody = null; 
			  if (httpEntity != null) {
				responseBody = EntityUtils.toString(httpEntity);
			  }
			  return responseBody;
			}
		  } else {
			throw new ClientProtocolException("Unexpected response status: " + status);
		  }
		}
	  };

	  final String responseBody = httpClient.execute(httpGet, responseHandler);
	  System.out.println("------ Response Body ------");
	  System.out.println(responseBody);
	}
  }
} 

Parameter

To build a URI, Apache API provides URIBuilder. URI is obtained by its build() method. To add or set parameters to URI query, URIBuilder provides following methods.
1. addParameter(String param, String value): Adds a parameter.
final URIBuilder builder = new URIBuilder("https://jsonplaceholder.typicode.com/todos");
builder.addParameter("userId", "10");
builder.addParameter("completed", "true");
System.out.println( builder.build()); 
URL will be created as following.
https://jsonplaceholder.typicode.com/todos?userId=10&completed=true 
2. addParameters(List<NameValuePair> nvps): Adds one or more parameters.
final URIBuilder builder = new URIBuilder("https://jsonplaceholder.typicode.com/todos");
NameValuePair p1 = new BasicNameValuePair("userId", "10");
NameValuePair p2 = new BasicNameValuePair("completed", "true");
builder.addParameters(Arrays.asList(p1, p2));
System.out.println(builder.build()); 
3. setParameter(String param, String value): Sets a parameter. If the parameter name is already available in URI query, that will be overridden with value and if not then new parameter will be added.
final URIBuilder builder = new URIBuilder("https://jsonplaceholder.typicode.com/todos");
builder.setParameter("userId", "10");
builder.setParameter("completed", "true");
System.out.println( builder.build()); 
4. setParameters(List nvps): Sets one or more parameters. If the parameter name is already available in URI query, that will be overridden with value and if not then new parameter will be added.
final URIBuilder builder = new URIBuilder("https://jsonplaceholder.typicode.com/todos");
NameValuePair p1 = new BasicNameValuePair("userId", "10");
NameValuePair p2 = new BasicNameValuePair("completed", "true");
builder.setParameters(Arrays.asList(p1, p2));
System.out.println( builder.build()); 
Headers are added to request using HttpGet instance. HttpGet inherits methods from BasicHttpRequest and HeaderGroup. Following methods can be used to add headers to request using HttpGet instance.
1. addHeader(String name, Object value): Adds a header to the message. HttpGet inherits this method from BasicHttpRequest.
final HttpGet httpGet = new HttpGet(builder.build());
httpGet.addHeader("Accept", "application/json");
httpGet.addHeader("Accept-Charset", "utf-8");
Stream.of(httpGet.getAllHeaders())
  .forEach(h -> System.out.println("Name: " + h.getName() + ", Value: " + h.getValue() )); 
2. addHeader(Header header) : Adds a header. HttpGet inherits this method from HeaderGroup.
final HttpGet httpGet = new HttpGet(builder.build());
httpGet.addHeader(new BasicHeader("Accept", "application/json"));
httpGet.addHeader(new BasicHeader("Accept-Charset", "utf-8")); 
3. setHeader(String name, Object value): Sets a header to the message. If the header is already present, its value will be overridden. HttpGet inherits this method from BasicHttpRequest.
final HttpGet httpGet = new HttpGet(builder.build());
httpGet.setHeader("Accept", "application/json");
httpGet.setHeader("Accept-Charset", "utf-8"); 
4. setHeader(Header header): Sets a header to the message. If the header is already present, its value will be overridden. HttpGet inherits this method from HeaderGroup.
final HttpGet httpGet = new HttpGet(builder.build());
httpGet.setHeader(new BasicHeader("Accept", "application/json"));
httpGet.setHeader(new BasicHeader("Accept-Charset", "utf-8")); 
5. setHeaders(Header... headers): Sets one or more headers to the message. If the header is already present, its value will be overridden. HttpGet inherits this method from HeaderGroup.
final HttpGet httpGet = new HttpGet(builder.build());
Header h1 = new BasicHeader("Accept", "application/json");
Header h2 = new BasicHeader("Accept-Charset", "utf-8");
httpGet.setHeaders(h1, h2); 

HttpClient Get Example with Parameters and Headers

Find the complete example to set parameters and headers with request.
ClientWithParamNHeader.java
package com.concretepage;
import java.io.IOException;
import java.io.InputStream;
import java.util.stream.Stream;
import org.apache.hc.client5.http.ClientProtocolException;
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.HttpClients;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.HttpException;
import org.apache.hc.core5.http.HttpStatus;
import org.apache.hc.core5.http.io.HttpClientResponseHandler;
import org.apache.hc.core5.net.URIBuilder;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;

public class ClientWithParamNHeader {
  public final static void main(final String[] args) throws Exception {
	final CloseableHttpClient httpClient = HttpClients.createDefault();
	try (httpClient) {
	  final URIBuilder builder = new URIBuilder("https://jsonplaceholder.typicode.com/todos");
	  builder.setParameter("userId", "10");
	  builder.setParameter("completed", "true");

	  System.out.println("URL: " + builder.build());

	  final HttpGet httpGet = new HttpGet(builder.build());
	  httpGet.setHeader("Accept", "application/json");
	  httpGet.setHeader("Accept-Charset", "utf-8");

	  Stream.of(httpGet.getAllHeaders())
		  .forEach(h -> System.out.println("Name: " + h.getName() + ", Value: " + h.getValue()));

	  HttpClientResponseHandler<User[]> responseHandler = new HttpClientResponseHandler<>() {
		@Override
		public User[] handleResponse(ClassicHttpResponse response) throws HttpException, IOException {
		  final int status = response.getCode();
		  System.out.println("Status: " + status);
		  if (status >= HttpStatus.SC_SUCCESS && status < HttpStatus.SC_REDIRECTION) {
			final JsonFactory jsonFactory = new JsonFactory();

			try (HttpEntity httpEntity = response.getEntity(); InputStream inputStream = httpEntity.getContent()) {

			  JsonParser jsonParser = jsonFactory.createParser(inputStream);
			  jsonParser.setCodec(new ObjectMapper());
			  User users[] = jsonParser.readValueAs(User[].class);
			  return users;
			}
		  } else {
			throw new ClientProtocolException("Unexpected response status: " + status);
		  }
		}
	  };

	  final User users[] = httpClient.execute(httpGet, responseHandler);
	  System.out.println("Total Users:" + users.length);
	  Stream.of(users).forEach(u -> System.out.println(u.getTitle()));
	}
  }
} 

Custom Response Handler for JSON

Here we will create custom response handler by implementing HttpClientResponseHandler and overriding its handleResponse method. For the demo we are using a sample URL that will return JSON response as following.
{
  "userId": 1,
  "id": 1,
  "title": "delectus aut autem",
  "completed": false
} 
Within handleResponse method, we will deserialize JSON data into User object using Jackson API. Find the example.
CustomResponseHandler.java
package com.concretepage;
import java.io.IOException;
import java.io.InputStream;
import org.apache.hc.client5.http.ClientProtocolException;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.HttpException;
import org.apache.hc.core5.http.HttpStatus;
import org.apache.hc.core5.http.io.HttpClientResponseHandler;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;

public class CustomResponseHandler implements HttpClientResponseHandler<User> {
 @Override
 public User handleResponse(ClassicHttpResponse response) throws HttpException, IOException {
  final int status = response.getCode();
  System.out.println("Status: "+ status);
  if (status >= HttpStatus.SC_SUCCESS && status < HttpStatus.SC_REDIRECTION) {
   final JsonFactory jsonFactory = new JsonFactory();

   try (HttpEntity httpEntity = response.getEntity(); 
		InputStream inputStream = httpEntity.getContent()) {

    JsonParser jsonParser = jsonFactory.createParser(inputStream);
    jsonParser.setCodec(new ObjectMapper());
    User user = jsonParser.readValueAs(User.class);
    return user;
   }
  } else {
   throw new ClientProtocolException("Unexpected response status: " + status);
  }
 }
} 
User.java
package com.concretepage;

public class User {
 private String id;
 private String userId;
 private String title;
 private Boolean completed;

 public String getId() {
  return id;
 }
 public String getUserId() {
  return userId;
 }
 public String getTitle() {
  return title;
 }
 public Boolean getCompleted() {
  return completed;
 }
} 
ClientWithJSON.java
package com.concretepage;
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.HttpClients;
import org.apache.hc.core5.http.io.HttpClientResponseHandler;

public class ClientWithJSON {
 public final static void main(final String[] args) throws Exception {
  CloseableHttpClient httpClient = HttpClients.createDefault();
  try (httpClient) {
   final HttpGet httpGet = new HttpGet("https://jsonplaceholder.typicode.com/todos/1");

   HttpClientResponseHandler <User> responseHandler = new CustomResponseHandler();
   final User user = httpClient.execute(httpGet, responseHandler);

   System.out.println("UserId: " + user.getUserId());
   System.out.println("Title: " + user.getTitle());
  }
 }
} 
Output
Status: 200
UserId: 1
Title: delectus aut autem 

References

HttpComponents HttpClient Overview
Apache HttpClient Response Handler

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us