Background
Assuming you know what REST or Restful APIs (GET, POST etc) are in this post we will cover how can we make these REST calls with Java program. As the title of the post suggests the library that we are going to use is - Apache HttpClient.
To test the same REST calls we can use the CURL command line utility. You can learn how to do so from my previous posts on CURL
Dependency
We need to include the Apache HTTP client library in the code. I always use Ivy as a dependency manager for my java programming.
My Ivy file looks like -
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <info organisation= "OpenSourceForGeeks" module= "Demos" status= "integration" > </info> <dependencies> <dependency org= "org.apache.httpcomponents" name= "httpclient" rev= "4.3.1" /> </dependencies> </ivy-module> |
Note : Whenever you want to add any dependency in any dependency Manager like Maven, Ivy, gradle etc you can search the library in Maven Repository.
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; /** * * @author athakur * */ public class HttpClientDemo { private static final String USER_AGENT = "Mozilla/5.0" ; private static final int TIMEOUT = 5000 ; public static void main(String args[]) { HttpClient httpClient = new DefaultHttpClient(); HttpClientDemo httpClientDemo = new HttpClientDemo(); HttpParams httpParams = httpClient.getParams(); httpParams.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, TIMEOUT); httpParams.setParameter(CoreConnectionPNames.SO_TIMEOUT, TIMEOUT); System.out.println( "GET Response : " + getResponse); System.out.println( "GET Response : " + postResponse); } private String performGetRequest(HttpClient httpClient, String url) { HttpGet httprequest = new HttpGet(url); httprequest.addHeader( "User-Agent" , USER_AGENT); try { System.out.println( "Performing GET request on + " + url); HttpResponse httpResponse = httpClient.execute(httprequest); System.out.println( "Status code : " + httpResponse.getStatusLine().getStatusCode()); BufferedReader reader = new BufferedReader( new InputStreamReader(httpResponse.getEntity().getContent())); StringBuffer requestResult = new StringBuffer(); String line = "" ; while ((line = reader.readLine()) != null ) { requestResult.append(line); } return requestResult.toString(); } catch (IOException e) { System.out.println( "Exception occurred while performing GET request" ); e.printStackTrace(); return null ; } finally { httprequest.releaseConnection(); } } private String performPostRequest(HttpClient httpClient, String url) { HttpPost httprequest = new HttpPost(url); httprequest.addHeader( "User-Agent" , USER_AGENT); List<NameValuePair> urlPostParameters = new ArrayList<NameValuePair>(); urlPostParameters.add( new BasicNameValuePair( "key" , "value" )); try { httprequest.setEntity( new UrlEncodedFormEntity(urlPostParameters)); } catch (UnsupportedEncodingException e1) { // TODO Auto-generated catch block System.out.println( "Encoding not supported" ); e1.printStackTrace(); return null ; } try { System.out.println( "Performing POST request on + " + url); HttpResponse httpResponse = httpClient.execute(httprequest); System.out.println( "Status code : " + httpResponse.getStatusLine().getStatusCode()); BufferedReader reader = new BufferedReader( new InputStreamReader(httpResponse.getEntity().getContent())); StringBuffer requestResult = new StringBuffer(); String line = "" ; while ((line = reader.readLine()) != null ) { requestResult.append(line); } return requestResult.toString(); } catch (IOException e) { System.out.println( "Exception occurred while performing POST request" ); e.printStackTrace(); return null ; } finally { httprequest.releaseConnection(); } } } |
Output
Performing GET request on + http://www.google.co.in
Status code : 200
GET Response : <!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="en-IN">...
Performing POST request on + http://www.google.co.in
Status code : 405
GET Response : <!DOCTYPE html><html lang=en> .......
Status code : 200
GET Response : <!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="en-IN">...
Performing POST request on + http://www.google.co.in
Status code : 405
GET Response : <!DOCTYPE html><html lang=en> .......
Note : POST call will not return valid output (200 status code). We got 405 (method not allowed) for post request because POST is not a valid method type for URL http://www.google.com. It only accepts a GET request.
is alternate way to do
Setting Timeout
Just for the record -
1 2 3 | HttpParams httpParams = httpClient.getParams(); httpParams.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, TIMEOUT); httpParams.setParameter(CoreConnectionPNames.SO_TIMEOUT, TIMEOUT); |
is alternate way to do
1 2 3 4 | HttpParams httpParams = httpClient.getParams(); HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT); HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT); |
With new APIs introduced in version 4.3 we can do
1 2 3 4 5 6 | RequestConfig requestConfig = RequestConfig.custom() .setConnectTimeout(TIMEOUT) .setConnectionRequestTimeout(TIMEOUT) .setSocketTimeout(TIMEOUT).build(); CloseableHttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build(); |
Understanding Timeouts -
- Connection Timeout – Time to establish the connection with the remote host.
- Socket Timeout – Time waiting for response of a request – after the connection is established.
- Connection Manager Timeout – Time to wait for a connection from the connection manager/pool .
Refer to following diagram to understand how REST APIs work -
Related Links
- Apache HTTP client tutorial
- How to send HTTP request GET/POST in Java
- What is the difference between the setConnectionTimeout , setSoTimeout and “http.connection-manager.timeout” in apache HttpClient API
No comments:
Post a Comment