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

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
26 Comment threads
6 Thread replies
2 Followers
 
Most reacted comment
Hottest comment thread
28 Comment authors
mkyongRonald ReedMary ZinMarydkucharczyk Recent comment authors
newest oldest most voted
Alex Loginov
Guest
Alex Loginov

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.

Prachi Bhambani
Guest
Prachi Bhambani

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 ?

Dragos
Guest
Dragos

Thanks a lot! I think this should also be corrected in the article. I kept blaming Facebook API for not giving me any answer.

Lukas
Guest
Lukas

There is also problem with not closing BufferedReader. I only had to close the stream to fix my problem with hanging.

Th?o Võ
Guest
Th?o Võ

a bit mistake with class name “HttpCilentExample” -> “HttpClient Example”, thank you mkyong :D

vijay
Guest
vijay

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 ?

simon
Guest
simon

Hi, thanks for this helpful guide but, what is the version of hhtpCLient used here ?

Ronald Reed
Guest
Ronald Reed

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

Mary Zin
Guest
Mary Zin

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.

Mary
Guest
Mary

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?

aambuj
Guest
aambuj

How to continuously poll the server for incoming response using these APIs?

aambuj
Guest
aambuj

How to poll using the APIs ?

ashish
Guest
ashish

i am getting response code 200

Rimte Rocher
Guest
Rimte Rocher

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

Ahluwalia Pranjal
Guest
Ahluwalia Pranjal

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

Nguyên Tr?c
Guest
Nguyên Tr?c

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

dkucharczyk
Guest
dkucharczyk

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.

vineela
Guest
vineela

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 ??

Riddhi
Guest
Riddhi

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)

jagathan
Guest
jagathan

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 !

geek
Guest
geek

how do i print the xml which is forming using namevaluepairs ?

j0n
Guest
j0n

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…

Namo
Guest
Namo

Apparently, Apache says HTTPClient is at the end of its life

http://hc.apache.org/httpclient-3.x/

carterson2
Guest
carterson2

do you have the PHP code as well. I have trouble PAIRING them.. thanks for posting.

sisco
Guest
sisco

hi mkyong, how to store php login session (in this case, codeigniter framework based application) in java and how to send the session when execute HttpResponse.execute(HttpGet).

For experiment, i’ve developed a testing application and for login you can access http://sisco.mipropia.com/exp/auth/do_login?username=sisco&password=sisco. To test the session, you can access http://sisco.mipropia.com/exp/item/get_item?item_id=1.

Thank you in advance.

chiranjeevi
Guest
chiranjeevi

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

abdullah
Guest
abdullah

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

abdullah
Guest
abdullah

SORRY
this comment is not here

Damian
Guest
Damian

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?

ka4eli
Guest
ka4eli

Also I try to send another POST (not Gmail) after singing in : response code 200 but nothing happens.

ka4eli
Guest
ka4eli

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… Read more »