Sending form data using Java 11 HttpClient
is fairly straightforward. The HttpClient
can be used to send both POST
and GET
requests, and form data is generally transmitted in POST
requests with the application/x-www-form-urlencoded
content type.
Here’s how you can send form data using Java 11’s HttpClient
:
Example: Sending Form Data
package org.kodejava.net.http;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpRequest.BodyPublishers;
public class HttpClientFormExample {
public static void main(String[] args) throws Exception {
// Create the HttpClient
HttpClient httpClient = HttpClient.newHttpClient();
// Form parameters
String formData = "param1=value1¶m2=value2";
// Create the HttpRequest
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://example.com/submit-form"))
.header("Content-Type", "application/x-www-form-urlencoded")
.POST(BodyPublishers.ofString(formData)) // Set the request body
.build();
// Send the POST request
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
// Output the response
System.out.println("Status Code: " + response.statusCode());
System.out.println("Response Body: " + response.body());
}
}
Steps Explained
- Create the
HttpClient
: TheHttpClient
object is used to build and send HTTP requests.HttpClient httpClient = HttpClient.newHttpClient();
- Form Data Encoding: The form data must be encoded in
application/x-www-form-urlencoded
format, e.g.,key1=value1&key2=value2
. You’ll need to manually build this format or use a utility method to encode special characters (like+
, spaces,&
, and=
). In simple cases:String formData = "param1=value1¶m2=value2";
- Build the Request:
- Use
HttpRequest.newBuilder()
to create your request. - Set the
URI
for the request. - Add the
Content-Type
header for the form data:"application/x-www-form-urlencoded"
. - Use
.POST()
withBodyPublishers.ofString(formData)
to send the body of the request.
HttpRequest request = HttpRequest.newBuilder() .uri(URI.create("https://example.com/submit-form")) .header("Content-Type", "application/x-www-form-urlencoded") .POST(BodyPublishers.ofString(formData)) .build();
- Use
- Send the Request: Use the
HttpClient.send()
method to send the request. Provide theHttpResponse.BodyHandlers.ofString()
to handle the response body as a plain string.HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
- Handle the Response: You can access the HTTP response status code and body.
System.out.println("Status Code: " + response.statusCode()); System.out.println("Response Body: " + response.body());
Notes
- If the form data contains special characters, you should encode them properly using
URLEncoder
:import java.net.URLEncoder; String param1 = URLEncoder.encode("value1", "UTF-8"); String param2 = URLEncoder.encode("value2", "UTF-8"); String formData = "param1=" + param1 + "¶m2=" + param2;
- Ensure the target server accepts
POST
requests with theapplication/x-www-form-urlencoded
content type.
This is all you need to send form data using Java 11’s HttpClient
.