Apache HttpClient 4.5 HTTP GET Request Method Example
This tutorial demonstrates how to use Apache HttpClient 4.5 to make a Http GET request. The Http GET method represents a representation of the specified resource. This could be as simple as getting an HTML page, or Getting resources formatted in JSON, XML or etc. Requests using HTTP GET Request methods should be Idempotent, meaning: these should only retrieve data and should have no other effects.
Maven dependencies
We use maven to manage our dependencies and are using Apache HttpClient version 4.5. Add the following dependency to your project in order to make HTTP Get request method.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.memorynotfound.apache.httpclient.httmethods</groupId>
<artifactId>http-get</artifactId>
<version>1.0.0-SNAPSHOT</version>
<url>https://memorynotfound.com</url>
<name>httpclient - ${project.artifactId}</name>
<dependencies>
<!-- Apache Commons IO -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
HTTP GET request method example
In the following example we retrieve a resource from http://httpbin.org/get. This resources returns a JSON object which we’ll simply print to the console. Note: we are using Java 7 try-with-resources to automatically handle the closing of the ClosableHttpClient
. Next we are using Java 8 lambdas for the ResponseHandler
. Here we are evaluating the Http Status code, when everything is ok we return the body of the response which we parse to a String
. When the status code is not what we expect, we throw a ClientProtocolException
, indicating that the Http GET request method failed. Finally, we print the response body to the console.
package com.memorynotfound.httpclient;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
/**
* This example demonstrates the use of {@link HttpGet} request method.
*/
public class HttpGetRequestMethodExample {
public static void main(String... args) throws IOException {
try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
HttpGet httpget = new HttpGet("http://httpbin.org/get");
System.out.println("Executing request " + httpget.getRequestLine());
// Create a custom response handler
ResponseHandler<String> responseHandler = response -> {
int status = response.getStatusLine().getStatusCode();
if (status >= 200 && status < 300) {
HttpEntity entity = response.getEntity();
return entity != null ? EntityUtils.toString(entity) : null;
} else {
throw new ClientProtocolException("Unexpected response status: " + status);
}
};
String responseBody = httpclient.execute(httpget, responseHandler);
System.out.println("----------------------------------------");
System.out.println(responseBody);
}
}
}
Output
Executing request GET http://httpbin.org/get HTTP/1.1
----------------------------------------
{
"args": {},
"headers": {
"Accept-Encoding": "gzip,deflate",
"Connection": "close",
"Host": "httpbin.org",
"User-Agent": "Apache-HttpClient/4.5.2 (Java/1.8.0_20)"
},
"origin": "84.196.76.228",
"url": "http://httpbin.org/get"
}