Apache HttpClient GET request example

Ads
 

Apache HttpClient GET request example

In this tutorial we are going to us the Apache HttpClient library to execute get request in our Java program.

In this tutorial we are going to us the Apache HttpClient library to execute get request in our Java program.

Apache HttpClient Example - How to run get request using Apache Httpclient library?

Apache HttpClient library is very popular library in Java for working with the HTTP server and this API allows you to develop program in Java that can interact with the HTTP server. You can use this library to develop a simple program that can execute the get request against HTTP server for getting the data from the server.

In this example we will show you two ways to connect to HTTP server and get the HTML data begin severed by the HTTP server. We are going to make a call to our website https://www.roseindia.net and retrieve the data from the website. You can update this code to interact with your own http server.

The HTTP is most significant protocol on the Internet and it stands for Hyper-Text Transfer Protocol. The Hyper-Text Transfer Protocol (HTTP) can be used over the secure connection called HTTPs and it used by most of the website on the Internet to secure the server-client communications. These days the use of HTTP/HTTPs protocol is growing beyond the web browsers and other applications are also using HTTP protocol for communication. You will find the wide range use of HTTP protocols in Web services, network-enabled applications other network computing use cases. In this tutorial we are going to use the Apache HTTP client library for accessing a resource over the internet (website).

The java.net package from core Java library provides basics functionality of accessing the HTTP server, but it does have abstracted API for the developers to quickly develop applications for interacting with the HTTP server. The HttpClient API from Apache comes with many high level API for fast development of application that interacts with the HTTP server. So, developer should learn the Apache HttpClient library and understand various API's of this package.

Here is complete example code for using the Apache HttpClient to run get request:


package net.roseindia;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
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;
//Example program to read data from url using apache Http client library
// https://www.roseindia.net

public class HTTPReadExample {

	public static void main(String[] args) {
		System.out.println("HTTP Read eaxmple using Apache HTTP Util package.");
		String url="https://www.roseindia.net";
		try {
			String content = HTTPReadExample.getUrlContent(url);
			System.out.println(content);
			
			content = HTTPReadExample.getUrlContentMethod2(url);
			System.out.println(content);
			
		} catch (Exception e) {
			System.out.println("Error is: "+e.getMessage());
		}
	}

	public static String getUrlContent(String url) throws Exception {
		String content="";
		String USER_AGENT = "Mozilla/5.0";
		CloseableHttpClient client = HttpClients.createDefault();
		HttpGet httpGet = new HttpGet(url);
		httpGet.addHeader("User-Agent", USER_AGENT);
		CloseableHttpResponse response = client.execute(httpGet);
		
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				response.getEntity().getContent()));

		String inputLine;
		StringBuffer out = new StringBuffer();

		while ((inputLine = reader.readLine()) != null) {
			out.append(inputLine);
		}
		reader.close();
		client.close();
		content = out.toString();
		
		return content;
	}

	public static String getUrlContentMethod2(String url) throws Exception {
		String content="";
		String USER_AGENT = "Mozilla/5.0";
		CloseableHttpClient client = HttpClients.createDefault();
		HttpGet httpGet = new HttpGet(url);
		httpGet.addHeader("User-Agent", USER_AGENT);
		CloseableHttpResponse response = client.execute(httpGet);
		
		HttpEntity entity = response.getEntity();
		content = EntityUtils.toString(entity);

		return content;
	}
}

In the above code we are connecting to https://www.roseindia.net and getting the page data and printing on the console. Here is the step by step explanation of the code:

Step 1: Add Apache HttpClient Maven Dependency

First of all you have to bring the Apache HttpClient dependency jar files into your project. The easiest way is to use the maven for your project and add following dependency code:


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

In the above code we are importing the maven dependency of Apache HttpClient version 4.5.13 which was released on 8 October 2020.

Step 2: Import Apache HttpClient required classes in the code

We have to import the classes of Apache HttpClient to use in the example code. Here are the list of classes we have to import:

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
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;

Step 3: Make HTTP get request using HttpClient

Now we can make the HTTP GET request with the help of following code:

String USER_AGENT = "Mozilla/5.0";
CloseableHttpClient client = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);
httpGet.addHeader("User-Agent", USER_AGENT);
CloseableHttpResponse response = client.execute(httpGet);

Here we are creating the instance of CloseableHttpClient, HttpGet and finally calling the client.execute() method to execute the GET method on the HTTP server. The execute() method returns the CloseableHttpResponse that can be used to get the response details.

Step 4: Reading the response text

Now we can read the response text in two ways:

  1. BufferedReader class
  2. HttpEntity class

Here is the example code of reading it using the BufferedReader class

	
public static String getUrlContent(String url) throws Exception {
	String content="";
	String USER_AGENT = "Mozilla/5.0";
	CloseableHttpClient client = HttpClients.createDefault();
	HttpGet httpGet = new HttpGet(url);
	httpGet.addHeader("User-Agent", USER_AGENT);
	CloseableHttpResponse response = client.execute(httpGet);
	
	BufferedReader reader = new BufferedReader(new InputStreamReader(
			response.getEntity().getContent()));

	String inputLine;
	StringBuffer out = new StringBuffer();

	while ((inputLine = reader.readLine()) != null) {
		out.append(inputLine);
	}
	reader.close();
	client.close();
	content = out.toString();
	
	return content;
}

Here is the code example of reading the response with the HttpEntity class:

	
public static String getUrlContentMethod2(String url) throws Exception {
	String content="";
	String USER_AGENT = "Mozilla/5.0";
	CloseableHttpClient client = HttpClients.createDefault();
	HttpGet httpGet = new HttpGet(url);
	httpGet.addHeader("User-Agent", USER_AGENT);
	CloseableHttpResponse response = client.execute(httpGet);
	
	HttpEntity entity = response.getEntity();
	content = EntityUtils.toString(entity);

	return content;
}

Here is the output of the program if you run in the Eclipse IDE:

Apache HttpClient Example

Related Tutorials:

Ads

Ads