Apache HttpClient 4.5 HTML FORM POST Example
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. The following tutorial shows how to send HTML Form parameters using apache HttpClient 4.5.
Maven dependencies
We use maven to manage our dependencies and are using Apache HttpClient version 4.5. Add the following dependency to your project.
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.memorynotfound.apache.httpclient.httmethods</groupId> <artifactId>http-post</artifactId> <version>1.0.0-SNAPSHOT</version> <url>https://memorynotfound.com</url> <name>httpclient - ${project.artifactId}</name> <dependencies> <!-- Apache HttpClient --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.2</version> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
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.memorynotfound.httpclient; 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("foo", "bar")); form.add(new BasicNameValuePair("employee", "John Doe")); 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
The previous application prints the following info to the console. Note: Apache HttpClient automatically discovered that the content is HTML form parameters and will fill the content-type to application/x-www-form-urlencoded.
Executing request POST http://httpbin.org/post HTTP/1.1 ---------------------------------------- { "args": {}, "data": "", "files": {}, "form": { "foo": "bar", "employee": "John Doe" }, "headers": { "Accept-Encoding": "gzip,deflate", "Connection": "close", "Content-Length": "27", "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8", "Host": "httpbin.org", "User-Agent": "Apache-HttpClient/4.5.3 (Java/1.8.0_20)" }, "json": null, "origin": "123.123.123.123", "url": "http://httpbin.org/post" }
Download
From:一号门
COMMENTS