Apache HttpClient Examples

HttpClient logo

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.10

pom.xml

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

1. Send GET Request

1.1 Close manually.

HttpClientExample1_1.java

package com.mkyong.http;

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;

import java.io.IOException;

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", "mkyong");
            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();
        }

    }

}

1.2 Close with try-with-resources.

HttpClientExample1_2.java

package com.mkyong.http;

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;

import java.io.IOException;

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", "mkyong");
        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.mkyong.http;

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

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

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)){

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

        return result;
    }

}

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.mkyong.http;

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;

import java.io.IOException;

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\":\"mkyong\",");
        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)) {

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

        return result;
    }

}

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

HttpClientExample3_2.java

package com.mkyong.http;

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;

import java.io.IOException;

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)) {

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

        return result;

    }

}

4. HTTP Basic Authentication

HttpClientExample4_1.java

package com.mkyong.http;

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;

import java.io.IOException;

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);
            }

        }

    }

}

Read this – Apache HttpClient Basic Authentication Examples

5. FAQs

5.1 Disabled Redirect.

HttpClientExample5_1.java

package com.mkyong.customer;

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;

import java.io.IOException;

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": "mkyong", 
    "Host": "httpbin.org", 
    "User-Agent": "Googlebot"
  }, 
  "origin": "202.168.71.227, 202.168.71.227", 
  "url": "https://httpbin.org/get"
}

References

38 comments on “Apache HttpClient Examples

  1. the example of 2.Send Normal POST Request doesn’t work, please do not post examples you have not tested. It took me half a day to figure it what’s wrong

  2. hello, when making a request I get the following error (org.apache.http.ConnectionClosedException: Premature end of Content-Length delimited message body (expected: 78,712; received: 46,275))

    CODE:

    public static void main(String[] args) {

        try {
          String result = blockIP();
          System.out.println(result);
        } catch (IOException e) {
          e.printStackTrace();
        }

      }

      private static String blockIP() throws IOException {

        String result = “”;

        String plainCredentials=”user:pass”;
        String base64Credentials = Base64.getEncoder().encodeToString(plainCredentials.getBytes());
        HttpPut post = new HttpPut(“http://IP:PORT/com.ibm.mdm.server.ws.restful/resources/MDMWSRESTful”);
        post.addHeader(“Accept”, “application/json”);
        post.addHeader(“Content-Type”, “application/json”);
        post.addHeader(“Accept”, “*/*”);
        post.addHeader(“Connection”, “keep-alive”);
        post.addHeader(“Authorization”, “Basic ” + base64Credentials);
        post.addHeader(“Accept-Encoding”, “gzip”);
         
        StringBuilder entity = new StringBuilder();
       …………….etc..

  3. Q) Need quick help on this issue. How to migrate below line of code in HttpClient-4.x..?
    if (!(org.apache.commons.httpclient.protocol.Protocol.getProtocol(“https”).getSocketFactory() instanceof LayeredConnectionSocketFactory)) {
    return;
    }
    org.apache.commons.httpclient.protocol.Protocol.unregisterProtocol(“https”);

  4. Hi ALL, MyKong,

    After lot of search I landed on this website. I am new to auto login. So far I could not not able to get profile page after login. Login returns code 200 but after I get again back to login page when I call getContent() method.

    I am using apacheHttp example exactly as it was given by MyKong.

    Please what is correct code to login website ??

    Thanks Ron

  5. Hi MyKong, setting the host in the request header using httpClient 4.3.6 causes a connection reset socket exception error. This is how the host is being set: request.setHeader(HttpHeaders.HOST, “localhost”); any ideas there would be helpful. Thank you.

  6. Hi MyKong, I am trying to set the the host in the request header with httpclient 4.3.6. like clientRequest.setHeader(HttpHeaders.HOST, “localhost:8080”) ; adding this header causes a Connection reset socket exception. Any helpful ideas there to resolve this?

  7. Nice write up ! Do you have a sample Java code to call a REST service with client certificate being passed along like a .pem file ?

  8. According to stackoverflow, HttpClient is not a class, it is an interface. You cannot use it for development in the way you mean. What you want is a class that implements the HttpClient interface, and that is CloseableHttpClient. Not sure your example works

  9. code to hit https link using spring using CloseableHttpClient API, login credentials and getting a status code

  10. Hi, might be helpful for someone: after you receive the response, please close HttpPost (HttpGet) :
    post.releaseConnection();

    Else after 2 times you will have no free connections and will have to wait infinitely for them to release – no exception will be thrown.

    1. Thank you so much for this note! It helped me out of a big issue! Cheers!
      and just wanted to know is it possible to follow the following in 1 consecutive logical flow : – Get, and then 6 consecutive Post requests ?

  11. Hi Mkyong,
    Thanks for your guide,
    But when i use
    HttpClient client = HttpClientBuilder.create().build();
    HttpGet request = new HttpGet(url);
    HttpResponse response = client.execute(request);
    There are a exception at
    HttpResponse response = client.execute(request);
    javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException:
    unable to find valid certification path to requested targetat sun.security.ssl.Alerts.getSSLException(Unknown Source)
    Could you please help me fix it
    Thanks a lot

    1. Hi Nguyên
      1. If you have this problems from UnitTest, and you are using PowerMockRunner, try this one:
      @PowerMockIgnore(“javax.net.ssl.*”)

      2. For simple things: you need to add locally in your code some trusted CA:
      URI uri = getClass().getClassLoader().getResource(“yourTrustedCA.jks”).toURI();
      System.setProperty(“javax.net.ssl.trustStore”, uri.getPath());
      System.setProperty(“javax.net.ssl.trustStorePassword”, “1qaz2wsx”);

      3. For long term work: add CA to your JDKJRE libs, example: https://docs.microsoft.com/en-us/azure/java-add-certificate-ca-store or just type in google: ‘add ca to java keystore’ and press enter.

  12. Hi mkyong, I have a problem that i need to pass div tag content as parameter to post request in java. As I am unable to pass the request. help me out that may I need to follow any format to send ??

  13. I m getting a following eror. Would be great If anybody can help..

    java.net.ConnectException: Connection refused: connect

    at java.net.PlainSocketImpl.socketConnect(Native Method)

    at java.net.PlainSocketImpl.doConnect(Unknown Source)

    at java.net.PlainSocketImpl.connectToAddress(Unknown Source)

    at java.net.PlainSocketImpl.connect(Unknown Source)

    at java.net.SocksSocketImpl.connect(Unknown Source)

    at java.net.Socket.connect(Unknown Source)

    at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:117)

    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:177)

    at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:304)

    at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:611)

    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:446)

    at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:863)

    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)

    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106)

    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57)

    at client.sendGet(client.java:44)

    at client.main(client.java:24)

  14. Hi
    I am trying to connect to moodle with this example and i am just getting the login page as response after the http post request. If anyone knows what else i have to do to connect to moodle please help 🙂
    Thanks in advance !

  15. How do you use httpclient post when your web service is secured with a FORM?
    The only response I get is the html code of my authentication form…

  16. i am getting SSLException as below when i am running the gmail connection example

    Exception in thread “main” javax.net.ssl.SSLException: java.lang.RuntimeException: Unexpected error: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty

  17. thank you sir,
    this code is worked with me but it is not login to gmail account
    it is back to login form

    i tired.. any help 🙂

  18. I have the same problem as ka4eli.
    I try to login on another service than goolge, but with Apache HttpClient after getting 200 nothing happen. With the HttpsURLConnection, a session is not keept;/

    How to solve there issues?

  19. Hi! I’ve tried both ways: using Apache HttpClient and HttpsURLConnection to connect to my Gmail and get my mail page. But in both cases the result page (when making html file from String result) wasn’t my mail, but it was a permanent loading bar (the same when you connect to Gmail and wait for authentification end).

    The same result was with “coursera.org”. And also with one another site, program returned login page with unknown signs(inb4:login and password were correct, php code with them managed to sign in).

    Can you explain this situations?
    Is it necessary to set http headers?

    Thank you a lot for you great job! I really appreciate you tutorials!

Leave a Comment

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