In this quick article, we will discuss how to use Apache HttpClient 4.5+ to work with HTML forms.
Related Apache HttpClient useful articles:
Working with HTML forms
Many applications need to simulate the process of submitting an HTML form, for instance, in order to log in to a web application or submit input data. HttpClient provides the entity class UrlEncodedFormEntity to facilitate the process.
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("param1", "value1"));
formparams.add(new BasicNameValuePair("param2", "value2"));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
HttpPost httppost = new HttpPost("http://localhost/handler.do");
httppost.setEntity(entity);
The UrlEncodedFormEntity instance will use the so-called URL encoding to encode parameters and produce the following content:
param1=value1¶m2=value2
HttpClient supports out of the box all HTTP methods defined in the HTTP/1.1 specification: GET, HEAD, POST, PUT, DELETE, TRACE, and OPTIONS. There is a specific class for each method type.: HttpGet, HttpHead, HttpPost, HttpPut, HttpDelete, HttpTrace, and HttpOptions.
In this example, we will use HttpPost class to handle the POST HTTP method.
Using the Apache HttpClient - Maven dependencies
The Apache HttpClient library allows handling HTTP requests. To use this library add a dependency to your Maven or Gradle build file. You find the latest version here: https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient
We use maven to manage our dependencies and are using Apache HttpClient version 4.5. Add the following dependency to your project.
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5</version>
</dependency>
Development Steps
1. Create instance of CloseableHttpClient using helper class HttpClients.
CloseableHttpClient httpclient = HttpClients.createDefault()
The HttpClients.createDefault() method creates CloseableHttpClient instance with default configuration.
2. Create a basic POST request
HttpPost httpPost = new HttpPost("http://httpbin.org/post");
3. Prepare Form Object
List<NameValuePair> form = new ArrayList<>();
form.add(new BasicNameValuePair("John", "Cena"));
form.add(new BasicNameValuePair("Tom", "Cruise"));
form.add(new BasicNameValuePair("tony", "stark"));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(form, Consts.UTF_8);
4. Create a custom response handler
ResponseHandler < String > responseHandler = response - > {
int status = response.getStatusLine().getStatusCode();
if (status >= 200 && status < 300) {
HttpEntity entity = response.getEntity();
return entity != null ? EntityUtils.toString(entity) : null;
} else {
throw new ClientProtocolException("Unexpected response status: " + status);
}
};
6. Send basic POST request via execute() Method
String responseBody = httpclient.execute(httpPost, responseHandler);
Submitting HTML Form Parameters
In the following example, we post HTML Form parameters to the resource http://httpbin.org/post. This resources acknowledges the data and returns a JSON object which we’ll simply print to the console. When sending HTML Form parameters, you should normally set the content-type to application/x-www-form-urlencoded, but Apache HttpClient automatically detects the content type and will set it accordingly.
package com.javadevelopersguide.httpclient.examples;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
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.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* This example demonstrates the use of {@link HttpPost} request method. And
* sending HTML Form request parameters
*/
public class HttpClientHttpFormExample {
public static void main(String...args) throws IOException {
try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
List < NameValuePair > form = new ArrayList < > ();
form.add(new BasicNameValuePair("John", "Cena"));
form.add(new BasicNameValuePair("Tom", "Cruise"));
form.add(new BasicNameValuePair("tony", "stark"));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(form, Consts.UTF_8);
HttpPost httpPost = new HttpPost("http://httpbin.org/post");
httpPost.setEntity(entity);
System.out.println("Executing request " + httpPost.getRequestLine());
// Create a custom response handler
ResponseHandler < String > responseHandler = response - > {
int status = response.getStatusLine().getStatusCode();
if (status >= 200 && status < 300) {
HttpEntity responseEntity = response.getEntity();
return responseEntity != null ? EntityUtils.toString(responseEntity) : null;
} else {
throw new ClientProtocolException("Unexpected response status: " + status);
}
};
String responseBody = httpclient.execute(httpPost, responseHandler);
System.out.println("----------------------------------------");
System.out.println(responseBody);
}
}
}
Output
Executing request POST http://httpbin.org/post HTTP/1.1
----------------------------------------
{
"args": {},
"data": "",
"files": {},
"form": {
"John": "Cena",
"Tom": "Cruise",
"tony": "stark"
},
"headers": {
"Accept-Encoding": "gzip,deflate",
"Connection": "close",
"Content-Length": "31",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Host": "httpbin.org",
"User-Agent": "Apache-HttpClient/4.5 (Java/1.8.0_172)"
},
"json": null,
"origin": "49.35.12.218",
"url": "http://httpbin.org/post"
}
References
Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours
Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course
Comments
Post a Comment