HttpClient post java code example
Example: java.net.http.httpclient post example
HttpClientPostJSON.java
package com.mkyong.java11.jep321;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
public class HttpClientPostJSON {
private static final HttpClient httpClient = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_2)
.connectTimeout(Duration.ofSeconds(10))
.build();
public static void main(String[] args) throws IOException, InterruptedException {
String json = new StringBuilder()
.append("{")
.append("\"name\":\"mkyong\",")
.append("\"notes\":\"hello\"")
.append("}").toString();
HttpRequest request = HttpRequest.newBuilder()
.POST(HttpRequest.BodyPublishers.ofString(json))
.uri(URI.create("https://httpbin.org/post"))
.setHeader("User-Agent", "Java 11 HttpClient Bot")
.header("Content-Type", "application/json")
.build();
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.statusCode());
System.out.println(response.body());
}
}