In this post, we are going to discuss about HttpComponentClient which can be used for automating your REST services.
Concept:
To start with, add the below dependency in your project POM file
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.5</version>
</dependency>
The general process for using HttpClient consists of a number of steps:
Concept:
To start with, add the below dependency in your project POM file
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.5</version>
</dependency>
The general process for using HttpClient consists of a number of steps:
- Create an instance of HttpClient.
- Create an instance of the method of requirement. The URL to connect to is passed in to the the method constructor.
- Tell HttpClient to execute the method.
- Read the response.
- Release the connection.
- Deal with the response.
1. Create an instance of HttpClient:
Instantiate the closeableHttpClient client with default instance.
CloseableHttpClient httpclient = HttpClients.createDefault();
2. Creating a Method:
The various methods defined by the HTTP specification correspond to the various classes in HttpClient which implement the HttpMethod interface. Below are the most used Actions and their instantiation.
For GET: HttpGet httpGet = new HttpGet(url); For POST: HttpPost httpPost= new HttpPost(url); For PUT: HttpPut httpPut = new HttpPut(url);
3. Execute method:
The actual execution of the method is performed by calling executeMethod on the client
and passing in the method to execute. Since networks connections are unreliable, we also
need to deal with any errors that occur.
HttpResponse httpResponse; try { httpResponse = httpGet .execute(httpRequest); } catch (IOException e) { // throw the exception here; }catch (HttpException e) { // throw the exception here; }
4. Read the response:
To Read the response, first we need to get the entity of it and then with the help of IOUtils
we need to copy to the response string.StringWriter writer = new StringWriter(); try { if(null != httpResponse.getEntity()) { IOUtils.copy(httpResponse.getEntity().getContent(), writer); } } catch (IOException e) { //throw the exception here; } String responseBodyString = writer.toString();
5. Release the connection:
This is a crucial step to keep things flowing. We must tell HttpClient that we are done with
the connection and that it can now be reused. Without doing this HttpClientwill wait
indefinitely for a connection to free up so that it can be reused. method.releaseConnection(); Let's look at the full implementation below:
CloseableHttpClient client = HttpClients.createDefault(); HttpGet httpGet = new HttpGet("http://targethost/homepage"); HttpResponse httpResponse1; try { httpResponse1 = client.execute(httpGet); } catch (IOException e) { // throw the exception here; } catch (HttpException e) { // throw the exception here; } try { System.out.println(httpResponse1.getStatusLine()); StringWriter writer = new StringWriter(); try { if(null != httpResponse1 .getEntity()) { IOUtils.copy(httpResponse1.getEntity().getContent(),
writer); } } catch (IOException e) { //throw exception here } String responseBodyString = writer.toString(); } finally { httpResponse1.close(); } //This is for the POST action. HttpPost httpPost = new HttpPost("http://targethost/login"); List <NameValuePair> nvps = new ArrayList <NameValuePair>(); nvps.add(new BasicNameValuePair("username", "vip")); nvps.add(new BasicNameValuePair("password", "secret")); httpPost.setEntity(new UrlEncodedFormEntity(nvps)); HttpResponse httpResponse2; try { httpResponse2 = client.execute(httpPost); } catch (IOException e) { // throw the exception here } catch (HttpException e) { // throw the exception here; } try { System.out.println(httpResponse2.getStatusLine()); StringWriter writer = new StringWriter(); try { if(null != httpResponse2.getEntity()) { IOUtils.copy(httpResponse2.getEntity().getContent(), writer); } } catch (IOException e) { //throw the exception here } String responseBodyString = writer.toString(); } finally { httpResponse2.close(); }
For the big scale projects we might need to customize the httpconnectClient for https
requests and also for network bypassing.
In our next posts, we will discuss about UrlconnectClient and RestAssuredClient
No comments:
Post a Comment