This article shows you how to use Apache HttpClient to send an HTTP GET/POST requests, JSON, authentication, timeout, redirection and some frequent used examples.

P.S Tested with HttpClient 4.5.13

pom.xml

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

1. Send GET Request

1.1 Close manually.

HttpClientExample1_1.java

package com.favtuts.http;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
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;

public class HttpClientExample1_1 {
    
    public static void main(String[] args) throws IOException {
        
        CloseableHttpClient httpClient = HttpClients.createDefault();

        try {
            
            HttpGet request = new HttpGet("https://httpbin.org/get");

            // add request headers
            request.addHeader("custom-key", "favtuts");
            request.addHeader(HttpHeaders.USER_AGENT, "Googlebot");

            CloseableHttpResponse response = httpClient.execute(request);

            try {
                
                // Get HttpResponse Status
                System.out.println(response.getProtocolVersion());              // HTTP/1.1
                System.out.println(response.getStatusLine().getStatusCode());   // 200
                System.out.println(response.getStatusLine().getReasonPhrase()); // OK
                System.out.println(response.getStatusLine().toString());        // HTTP/1.1 200 OK

                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    // return it as a string
                    String result = EntityUtils.toString(entity);
                    System.out.println(result);
                }

            } finally {
                response.close();
            }

        } finally {
            httpClient.close();
        }
    }

}

Output

HTTP/1.1
200
OK
HTTP/1.1 200 OK
{
  "args": {}, 
  "headers": {
    "Accept-Encoding": "gzip,deflate", 
    "Custom-Key": "favtuts", 
    "Host": "httpbin.org", 
    "User-Agent": "Googlebot", 
    "X-Amzn-Trace-Id": "Root=1-6279d49f-18519ba7435c0e1c7d927a47"
  }, 
  "origin": "14.162.33.98", 
  "url": "https://httpbin.org/get"
}

1.2 Close with try-with-resources.

HttpClientExample1_2.java

package com.favtuts.http;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
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;

public class HttpClientExample1_2 {

    public static void main(String[] args) throws IOException {
        
        HttpGet request = new HttpGet("https://httpbin.org/get");
        
        // add request headers
        request.addHeader("custom-key", "favtuts");
        request.addHeader(HttpHeaders.USER_AGENT, "Googlebot");

        try (CloseableHttpClient httpClient = HttpClients.createDefault();
            CloseableHttpResponse response = httpClient.execute(request)) {
            
            // Get HttpResponse Status
            System.out.println(response.getProtocolVersion());              // HTTP/1.1
            System.out.println(response.getStatusLine().getStatusCode());   // 200
            System.out.println(response.getStatusLine().getReasonPhrase()); // OK
            System.out.println(response.getStatusLine().toString());        // HTTP/1.1 200 OK

            HttpEntity entity = response.getEntity();
            if (entity != null) {
                // return it as a String
                String result = EntityUtils.toString(entity);
                System.out.println(result);
            }
        }
    }
    
}

2. Send Normal POST Request

HttpClientExample2_1.java

package com.favtuts.http;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class HttpClientExample2_1 {

    public static void main(String[] args) {
        
        try {
            String result = sendPOST("https://httpbin.org/post");
            System.out.println(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static String sendPOST(String url) throws IOException {

        String result = "";
        HttpPost post = new HttpPost(url);

        // add request parameters or form parameters
        List<NameValuePair> urlParameters = new ArrayList<>();
        urlParameters.add(new BasicNameValuePair("username", "abc"));
        urlParameters.add(new BasicNameValuePair("password", "123"));
        urlParameters.add(new BasicNameValuePair("custom", "secret"));

        post.setEntity(new UrlEncodedFormEntity(urlParameters));

        try (CloseableHttpClient httpClient = HttpClients.createDefault();
            CloseableHttpResponse response = httpClient.execute(post)) {

            // Get HttpResponse Status
            System.out.println(response.getProtocolVersion());              // HTTP/1.1
            System.out.println(response.getStatusLine().getStatusCode());   // 200
            System.out.println(response.getStatusLine().getReasonPhrase()); // OK
            System.out.println(response.getStatusLine().toString());        // HTTP/1.1 200 OK
            
            result = EntityUtils.toString(response.getEntity());
            
        }

        return result;
    }
    
}

Output

HTTP/1.1
200
OK
HTTP/1.1 200 OK
{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "custom": "secret", 
    "password": "123", 
    "username": "abc"
  }, 
  "headers": {
    "Accept-Encoding": "gzip,deflate", 
    "Content-Length": "39", 
    "Content-Type": "application/x-www-form-urlencoded", 
    "Host": "httpbin.org", 
    "User-Agent": "Apache-HttpClient/4.5.13 (Java/11.0.15)", 
    "X-Amzn-Trace-Id": "Root=1-6279d8de-1e60919a2717110a683c4f55"
  }, 
  "json": null, 
  "origin": "14.162.33.98", 
  "url": "https://httpbin.org/post"
}

3. Send JSON POST Request

3.1 Send a POST request with JSON formatted data. The key is passed the JSON formatted date into a StringEntity.

HttpClientExample3_1.java

package com.favtuts.http;

import java.io.IOException;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class HttpClientExample3_1 {

    public static void main(String[] args) {
        
        try {
            String result = sendPOST("https://httpbin.org/post");
            System.out.println(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
        
    }

    private static String sendPOST(String url) throws IOException {

        String result = "";
        HttpPost post = new HttpPost(url);

        StringBuilder json = new StringBuilder();
        json.append("{");
        json.append("\"name\":\"favtuts\",");
        json.append("\"notes\":\"hello\"");
        json.append("}");

        // send a JSON data
        post.setEntity(new StringEntity(json.toString()));

        try (CloseableHttpClient httpClient = HttpClients.createDefault();
            CloseableHttpResponse response = httpClient.execute(post)) {

            // Get HttpResponse Status
            System.out.println(response.getProtocolVersion());              // HTTP/1.1
            System.out.println(response.getStatusLine().getStatusCode());   // 200
            System.out.println(response.getStatusLine().getReasonPhrase()); // OK
            System.out.println(response.getStatusLine().toString());        // HTTP/1.1 200 OK
            
            result = EntityUtils.toString(response.getEntity());
            
        }

        return result;

    }

}

Output

HTTP/1.1
200
OK
HTTP/1.1 200 OK
{
  "args": {}, 
  "data": "{\"name\":\"favtuts\",\"notes\":\"hello\"}", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept-Encoding": "gzip,deflate", 
    "Content-Length": "34", 
    "Content-Type": "text/plain; charset=ISO-8859-1", 
    "Host": "httpbin.org", 
    "User-Agent": "Apache-HttpClient/4.5.13 (Java/11.0.15)", 
    "X-Amzn-Trace-Id": "Root=1-6279dae2-29d06fc2143147a43b7c9bb7"
  }, 
  "json": {
    "name": "favtuts", 
    "notes": "hello"
  }, 
  "origin": "14.162.33.98", 
  "url": "https://httpbin.org/post"
}

3.2 Send a POST request to Cloudflare API to block an IP address. Authentication in headers.

HttpClientExample3_2.java

package com.favtuts.http;

import java.io.IOException;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class HttpClientExample3_2 {

    public static void main(String[] args) {
        
        try {
            String result = blockIP("1.1.1.1");
            System.out.println(result);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    
    private static String blockIP(String ip) throws IOException {

        String result = "";

        HttpPost post = new HttpPost("https://api.cloudflare.com/client/v4/user/firewall/access_rules/rules");
        post.addHeader("content-type", "application/json");
        post.addHeader("X-Auth-Email", "email");
        post.addHeader("X-Auth-Key", "token123");

        String block = "{\"target\":\"ip\",\"value\":\"" + ip + "\"}";

        StringBuilder entity = new StringBuilder();
        entity.append("{");
        entity.append("\"mode\":\"block\",");
        entity.append("\"configuration\":" + block + ",");
        entity.append("\"notes\":\"hello\"");
        entity.append("}");

        // send a JSON data
        post.setEntity(new StringEntity(entity.toString()));

        try (CloseableHttpClient httpClient = HttpClients.createDefault();
             CloseableHttpResponse response = httpClient.execute(post)) {

            // Get HttpResponse Status
            System.out.println(response.getProtocolVersion());              // HTTP/1.1
            System.out.println(response.getStatusLine().getStatusCode());   // 200
            System.out.println(response.getStatusLine().getReasonPhrase()); // OK
            System.out.println(response.getStatusLine().toString());        // HTTP/1.1 200 OK

            result = EntityUtils.toString(response.getEntity());
        }

        return result;
    }
}

4. HTTP Basic Authentication

This section shows you how to use Apache HttpClient to perform an HTTP basic authentication.

4.1 Basic Authentication

Start a simple Spring Security WebApp providing HTTP basic authentication, and test it with the HttpClient

The key is to configure CredentialsProvider and pass it to the HttpClientBuilder.

HttpClientExample4_1.java

package com.favtuts.http;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

public class HttpClientExample4_1 {

    public static void main(String[] args) throws IOException {
        
        HttpGet request = new HttpGet("http://localhost:8080/books");

        CredentialsProvider provider = new BasicCredentialsProvider();
        provider.setCredentials(
            AuthScope.ANY, 
            new UsernamePasswordCredentials("user", "password")
        );

        try (CloseableHttpClient httpClient = HttpClientBuilder.create()
                .setDefaultCredentialsProvider(provider)
                .build();
             CloseableHttpResponse response = httpClient.execute(request)) {

            // 401 if wrong user/password
            System.out.println(response.getStatusLine().getStatusCode());

            HttpEntity entity = response.getEntity();
            if (entity != null) {
                // return it as a String
                String result = EntityUtils.toString(entity);
                System.out.println(result);
            }

        }
    }
    
}

Output

200
[
	{"id":1,"name":"A Guide to the Bodhisattva Way of Life","author":"Santideva","price":15.41},
	{"id":2,"name":"The Life-Changing Magic of Tidying Up","author":"Marie Kondo","price":9.69},
	{"id":3,"name":"Refactoring: Improving the Design of Existing Code","author":"Martin Fowler","price":47.99}
]

If the login is incorrect!

401
{
	"timestamp":"2019-10-09T07:06:57.966+0000",
	"status":401,
	"error":"Unauthorized",
	"message":"Unauthorized",
	"path":"/books"
}

4.2 Preemptive Basic Authentication

This preemptive basic authentication will reduce the overhead of making the connection, read this HttpClient Authentication

HttpClientExample4_2.java

package com.favtuts.http;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

public class HttpClientExample4_2 {

    public static void main(String[] args) throws IOException {

        HttpGet request = new HttpGet("http://localhost:8080/books");
        HttpHost target = new HttpHost("localhost", 8080, "http");

        CredentialsProvider provider = new BasicCredentialsProvider();
        provider.setCredentials(
                new AuthScope(target.getHostName(), target.getPort()),
                new UsernamePasswordCredentials("user", "password")
        );

        AuthCache authCache = new BasicAuthCache();
        authCache.put(target, new BasicScheme());
        
        HttpClientContext localContext = HttpClientContext.create();
        localContext.setAuthCache(authCache);

        try (CloseableHttpClient httpClient = HttpClientBuilder.create()
                .setDefaultCredentialsProvider(provider)
                .build();
             CloseableHttpResponse response = httpClient.execute(target, request, localContext)) {

            // 401 if wrong user/password
            System.out.println(response.getStatusLine().getStatusCode());

            HttpEntity entity = response.getEntity();
            if (entity != null) {
                // return it as a String
                String result = EntityUtils.toString(entity);
                System.out.println(result);
            }

        }
    }
    
}

5. FAQs

5.1 Disabled Redirect.

HttpClientExample5_1.java

package com.favtuts.http;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
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.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

public class HttpClientExample5_1 {

    public static void main(String[] args) throws IOException {
        
        HttpGet request = new HttpGet("https://t.co/calv72DH8f");
        request.addHeader(HttpHeaders.USER_AGENT, "Googlebot");

        try (CloseableHttpClient httpClient = HttpClientBuilder.create().disableRedirectHandling().build();
             CloseableHttpResponse response = httpClient.execute(request)) {

            // Get HttpResponse Status
            System.out.println(response.getProtocolVersion());              // HTTP/1.1
            System.out.println(response.getStatusLine().getStatusCode());   // 301
            System.out.println(response.getStatusLine().getReasonPhrase()); // Moved Permanently
            System.out.println(response.getStatusLine().toString());        // HTTP/1.1 301 Moved Permanently

            HttpEntity entity = response.getEntity();
            if (entity != null) {
                // return it as a String
                String result = EntityUtils.toString(entity);
                System.out.println(result);
            }

        }
    }
    
}

5.2 Connection time out on request level.

	HttpGet request = new HttpGet("https://httpbin.org/get");

	// 5 seconds timeout
	RequestConfig requestConfig = RequestConfig.custom()
			.setConnectionRequestTimeout(5000)
			.setConnectTimeout(5000)
			.setSocketTimeout(5000)
			.build();

	request.setConfig(requestConfig);

	try (CloseableHttpClient httpClient = HttpClients.createDefault();
		 CloseableHttpResponse response = httpClient.execute(request)) {

		//...

	}

5.3 Connection time out on client level.

	HttpGet request = new HttpGet("https://httpbin.org/get");

	// 5 seconds timeout
	RequestConfig requestConfig = RequestConfig.custom()
			.setConnectionRequestTimeout(5000)
			.setConnectTimeout(5000)
			.setSocketTimeout(5000)
			.build();

	//request.setConfig(requestConfig);

	try (CloseableHttpClient httpClient = HttpClientBuilder.create()
			.setDefaultRequestConfig(requestConfig)
			.build();
		 CloseableHttpResponse response = httpClient.execute(request)) {

		//...

	}	

5.4 Configure a proxy server. Read HttpClient proxy configuration

	HttpGet request = new HttpGet("https://httpbin.org/get");

	RequestConfig requestConfig = RequestConfig.custom()
			.setProxy(new HttpHost("company.proxy.url", 8080))
            .build();

	request.setConfig(requestConfig);

	try (CloseableHttpClient httpClient = HttpClients.createDefault();
		 CloseableHttpResponse response = httpClient.execute(request)) {

		//...

	}

5.5 Turn on cookie. Read HTTP cookies

	HttpGet request = new HttpGet("https://httpbin.org/get");
	
	RequestConfig requestConfig = RequestConfig.custom()
			.setCookieSpec(CookieSpecs.DEFAULT)
			.build();

	request.setConfig(requestConfig);
	
	try (CloseableHttpClient httpClient = HttpClients.createDefault();
	 CloseableHttpResponse response = httpClient.execute(request)) {

	//...

	}

5.6 Get response header and also the media type.

	try (CloseableHttpClient httpClient = HttpClients.createDefault();
         CloseableHttpResponse response = httpClient.execute(request)) {

		HttpEntity entity = response.getEntity();
		Header headers = entity.getContentType();
		System.out.println(headers);

		//...
	}

Sample

Content-Type: application/json
{
  "args": {}, 
  "headers": {
    "Accept-Encoding": "gzip,deflate", 
    "Custom-Key": "favtuts", 
    "Host": "httpbin.org", 
    "User-Agent": "Googlebot"
  }, 
  "origin": "202.168.71.227, 202.168.71.227", 
  "url": "https://httpbin.org/get"
}

Download Source Code

$ git clone https://github.com/favtuts/java-core-tutorials-examples

$ cd java-misc/http

References

Leave a Reply

Your email address will not be published. Required fields are marked *