HTTP CLIENT

official documentation Here : http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/HttpClient.html

HTTP CLIENT

  • HttpClient is NOT a browser. It is a client side HTTP transport library.
  • HttpClient’s purpose is to transmit and receive HTTP messages. HttpClient will not attempt to cache content, execute javascript embedded in HTML pages, try to guess content type, or reformat request / redirect location URIs, or other functionality unrelated to the HTTP transport.

Client-side HTTP transport library based on HttpCore [http://hc.apache.org/httpcomponents-core/itndex.html]. So, to start with, let us understand how its going to help a tester. Imagine you need to check if your website is up without opening a browser every time, what you can do is just create a GET request ready and throw it to us server and if its up it will give a http 200 status. Oh ya, I believe, you know the difference between GET and POST.

GET and POST method in HTTP and HTTPS are two most popular methods used to transfer data from client to server using HTTP(Hyper Text Transfer Protocol) protocol. Both GET and POST can be used to send request and receive response but there are significant difference between them. Difference between GET and POST in HTTP or HTTPS is also a popular interview question in JSP and any web programming interview. Since HTML is independent of any web server technology like Java, ASP or PHP and HTTP is core protocol in space of internet, importance of clear understanding of GET and POST method can not be ignored. In this tutorial we will What is GET HTTP Request, What is POST HTTP Request, When to use GET and POST HTTP method and finally some difference between GET and POST method in HTTP protocol.

1) GET method passes request parameter in URL String while POST method passes request parameter in request body.
2) GET request can only pass limited amount of data while POST method can pass large amount of data to server.
3) GET request can be bookmarked and cached unlike POST requests.
4) GET is mostly used for view purpose (e.g. SQL SELECT) while POST is mainly use for update purpose (e.g. SQL INSERT or UPDATE).

Read more: http://javarevisited.blogspot.com/2012/03/get-post-method-in-http-and-https.html#ixzz2UOVOCzut

Ok now, let us start with taking an example website. http://crackme.cenzic.com which is a demo site which can be used for testing. To make it more easy, I have created an user name and password for this site as terter_hyd/tester. Let us take a java example on httpclient as below and explain what is happening here:

package com.bijoymeethal.othertests;

import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
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.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

/**
* A example that demonstrates how HttpClient APIs can be used to perform
* form-based logon.
*/
public class HttpTrainer {

public static void main(String[] args) throws Exception {

DefaultHttpClient httpclient = new DefaultHttpClient();

HttpGet httpget = new HttpGet(“http://crackme.cenzic.com/Kelev/php/login.php”);

HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();

System.out.println(“Login form get: ” + response.getStatusLine());
if (entity != null) {
entity.consumeContent();
}
System.out.println(“Initial set of cookies:”);
List<Cookie> cookies = httpclient.getCookieStore().getCookies();
if (cookies.isEmpty()) {
System.out.println(“None”);
} else {
for (int i = 0; i < cookies.size(); i++) {
System.out.println(“- ” + cookies.get(i).toString());
}
}

HttpPost httpost = new HttpPost(“http://crackme.cenzic.com/Kelev/php/login.php?”+
“hLoginType=&”+
“hPageName=accttransaction.php&”+
“whoislog=Welcome+to+CrackMeBank+Investments&”+
“hUserId=0”);

List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair(“LoginName”, “tester_hyd”));
nvps.add(new BasicNameValuePair(“Password”, “tester”));

httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

response = httpclient.execute(httpost);
entity = response.getEntity();

System.out.println(“Login form get: ” + response.getStatusLine());
if (entity != null) {
entity.consumeContent();
}

System.out.println(“Post logon cookies:”);
cookies = httpclient.getCookieStore().getCookies();
if (cookies.isEmpty()) {
System.out.println(“None”);
} else {
for (int i = 0; i < cookies.size(); i++) {
System.out.println(“- ” + cookies.get(i).toString());
}
}

// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
}

Now let us see what we have just seen, We have initially created a GET to make sure the server is up and responding properly, then we have asked for cookies and then we have created a POST and attached a credential as LIST consisting on username and password which can be customized, look at the output you get , it will be similar to :

Login form get: HTTP/1.1 200 OK
Initial set of cookies:
None
Login form get: HTTP/1.1 200 OK
Post logon cookies:
– [version: 0][name: PHPSESSID][value: 8529d8e713ce08c1d47f0f369ed7ef14][domain: crackme.cenzic.com][path: /][expiry: null]

This shows when The first part of HTTP GET is requested it did not have any cookies to show whereas when user logged-in as POST with credentials has session-id in cookies. So, we have a start, we have a long way to go.. but start trying…

Leave a comment