Apache HttpClient Examples
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
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.10</version>
</dependency>
1. Send GET Request
1.1 Close manually.
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.
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
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
.
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.
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
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.
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"
}
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 ?
a bit mistake with class name “HttpCilentExample” -> “HttpClient Example”, thank you mkyong 😀
how can I send a file via HttpRequest by method Post with a token?!
i am getting response code 200
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
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.
Examples are updated with try-with-resources, the client and request will be closed automatically.
There is also problem with not closing BufferedReader. I only had to close the stream to fix my problem with hanging.
Thanks a lot! I think this should also be corrected in the article. I kept blaming Facebook API for not giving me any answer.
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 ?
Hi, thanks for this helpful guide but, what is the version of hhtpCLient used here ?
What theme is it?
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
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..
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”);
How do we get callback response in our End using okhttp??
please Help
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
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.
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?
How to continuously poll the server for incoming response using these APIs?
How to poll using the APIs ?
code to hit https link using spring using CloseableHttpClient API, login credentials and getting a status code
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
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.
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 ??
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)
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 !
how do i print the xml which is forming using namevaluepairs ?
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…
Apparently, Apache says HTTPClient is at the end of its life
http://hc.apache.org/httpclient-3.x/
do you have the PHP code as well. I have trouble PAIRING them.. thanks for posting.
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.
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
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 🙂
SORRY
this comment is not here
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?