Using the HttpClient library you can send a request or, login to a form by passing parameters.

Follow the steps given below to login to a form.

Step 1 - Create an HttpClient object

The createDefault() method of the HttpClients class returns an object of the class CloseableHttpClient, which is the base implementation of the HttpClient interface. Using this method, create an HttpClient object −

  1. CloseableHttpClient httpClient = HttpClients.createDefault();

Step 2 - Create a RequestBuilder object

The class RequestBuilder is used to build request by adding parameters to it. If the request type is PUT or POST, it adds the parameters to the request as URL encoded entity

Create a RequestBuilder object (of type POST) using the post() method.

  1. //Building the post request object
  2. RequestBuilder reqbuilder = RequestBuilder.post();

Step 3 - Set Uri and parameters to the RequestBuilder.

Set the URI and parameters to the RequestBuilder object using the setUri() and addParameter() methods of the RequestBuilder class.

  1. //Set URI and parameters
  2. RequestBuilder reqbuilder = reqbuilder.setUri("http://httpbin.org/post");
  3. reqbuilder = reqbuilder1.addParameter("Name", "username").addParameter("password", "password");

Step 4 - Build the HttpUriRequest object

After setting the required parameters, build the HttpUriRequest object using the build() method.

  1. //Building the HttpUriRequest object
  2. HttpUriRequest httppost = reqbuilder2.build();

Step 5 - Execute the request

The execute method of the CloseableHttpClient object accepts a HttpUriRequest (interface) object (i.e. HttpGet, HttpPost, HttpPut, HttpHead etc.) and returns a response object.

Execute the HttpUriRequest created in the previous steps by passing it to the execute() method.

  1. //Execute the request
  2. HttpResponse httpresponse = httpclient.execute(httppost);

Example

Following example demonstrates how to logon to a form by sending login credentials. Here, we have sent two parameters − username and password to a form and tried to print the message entity and status of the request.

  1. import org.apache.http.HttpResponse;
  2. import org.apache.http.client.methods.HttpUriRequest;
  3. import org.apache.http.client.methods.RequestBuilder;
  4. import org.apache.http.impl.client.CloseableHttpClient;
  5. import org.apache.http.impl.client.HttpClients;
  6. import org.apache.http.util.EntityUtils;
  7. import java.io.IOException;
  8. import java.net.URISyntaxException;
  9.  
  10. public class FormLoginExample {
  11.  
  12. public static void main(String args[]) throws Exception {
  13.  
  14. //Creating CloseableHttpClient object
  15. CloseableHttpClient httpclient = HttpClients.createDefault();
  16.  
  17. //Creating the RequestBuilder object
  18. RequestBuilder reqbuilder = RequestBuilder.post();
  19.  
  20. //Setting URI and parameters
  21. RequestBuilder reqbuilder1 = reqbuilder.setUri("http://httpbin.org/post");
  22. RequestBuilder reqbuilder2 = reqbuilder1.addParameter("Name",
  23. "username").addParameter("password", "password");
  24.  
  25. //Building the HttpUriRequest object
  26. HttpUriRequest httppost = reqbuilder2.build();
  27.  
  28. //Executing the request
  29. HttpResponse httpresponse = httpclient.execute(httppost);
  30.  
  31. //Printing the status and the contents of the response
  32. System.out.println(EntityUtils.toString(httpresponse.getEntity()));
  33. System.out.println(httpresponse.getStatusLine());
  34. }
  35. }

Output

On executing, the above program generates the following output −

  1. {
  2. "args": {},
  3. "data": "",
  4. "files": {},
  5. "form": {
  6. "Name": "username",
  7. "password": "password"
  8. },
  9. "headers": {
  10. "Accept-Encoding": "gzip,deflate",
  11. "Connection": "close",
  12. "Content-Length": "31",
  13. "Content-Type": "application/x-www-form-urlencoded; charset = UTF-8",
  14. "Host": "httpbin.org",
  15. "User-Agent": "Apache-HttpClient/4.5.6 (Java/1.8.0_91)"
  16. },
  17. "json": null,
  18. "origin": "117.216.245.180",
  19. "url": "http://httpbin.org/post"
  20. }
  21. HTTP/1.1 200 OK

Form Login with Cookies

If your form stores cookies, instead of creating default CloseableHttpClient object.

Create a CookieStore object by instantiating the BasicCookieStore class.

  1. //Creating a BasicCookieStore object
  2. BasicCookieStore cookieStore = new BasicCookieStore();

Create a HttpClientBuilder using the custom() method of the HttpClients class.

  1. //Creating an HttpClientBuilder object
  2. HttpClientBuilder clientbuilder = HttpClients.custom();

Set the cookie store to the client builder using the setDefaultCookieStore() method.

  1. //Setting default cookie store to the client builder object
  2. Clientbuilder = clientbuilder.setDefaultCookieStore(cookieStore);

Build the CloseableHttpClient object using the build() method.

  1. //Building the CloseableHttpClient object
  2. CloseableHttpClient httpclient = clientbuilder1.build();

Build the HttpUriRequest object as specified above by passing execute the request.

If the page stores cookies, the parameters you have passed will be added to the cookie store.

You can print the contents of the CookieStore object where you can see your parameters (along with the previous ones the page stored in case).

To print the cookies, get all the cookies from the CookieStore object using the getCookies() method. This method returns a List object. Using Iterator, print the list objects contents as shown below −

  1. //Printing the cookies
  2. List list = cookieStore.getCookies();
  3.  
  4. System.out.println("list of cookies");
  5. Iterator it = list.iterator();
  6. if(it.hasNext()) {
  7. System.out.println(it.next());
  8. }