Java : HttpRequest.Builder (HTTP通信) - API使用例
HttpRequest.Builder (Java SE 21 & JDK 21) の使用例まとめです。
だいたいのメソッドを網羅済みです。
API仕様書のおともにどうぞ。
概要
HttpRequest.Builder クラスは、HttpRequest を作成するための ビルダークラス です。
final var uri = URI.create("https://example.com/");
final var builder = HttpRequest.newBuilder(uri);
final var request = builder
.POST(HttpRequest.BodyPublishers.ofString("XYZ"))
.header("Content-Type", "text/plain")
.header("Content-Language", "en")
.timeout(Duration.ofSeconds(30))
.version(HttpClient.Version.HTTP_1_1)
.build();
System.out.println(request); // https://example.com/ POST
System.out.println(request.method()); // POST
// {Content-Language=[en], Content-Type=[text/plain]}
System.out.println(request.headers().map());
System.out.println(request.timeout()); // Optional[PT30S]
System.out.println(request.version()); // Optional[HTTP_1_1]
メソッド
HttpRequest build ()
final var uri = URI.create("https://example.com/");
final var request = HttpRequest.newBuilder(uri)
.POST(HttpRequest.BodyPublishers.ofString("XYZ"))
.build();
System.out.println(request); // https://example.com/ POST
HttpRequest.Builder copy ()
final var uri = URI.create("https://example.com/");
final var src = HttpRequest.newBuilder(uri).DELETE();
final var copied = src.copy();
System.out.println(src.build()); // https://example.com/ DELETE
System.out.println(copied.build()); // https://example.com/ DELETE
copied.GET();
System.out.println(src.build()); // https://example.com/ DELETE
System.out.println(copied.build()); // https://example.com/ GET
src.uri(URI.create("http://127.0.0.1:8000/"));
System.out.println(src.build()); // http://127.0.0.1:8000/ DELETE
System.out.println(copied.build()); // https://example.com/ GET
HttpRequest.Builder DELETE ()
final var uri = URI.create("https://example.com/");
final var request = HttpRequest.newBuilder(uri).DELETE().build();
System.out.println(request); // https://example.com/ DELETE
System.out.println(request.method()); // DELETE
HttpRequest.Builder expectContinue (boolean enable)
final var uri = URI.create("https://example.com/");
final var request = HttpRequest.newBuilder(uri).expectContinue(true).build();
System.out.println(request); // https://example.com/ GET
System.out.println(request.expectContinue()); // true
final var uri = URI.create("https://example.com/");
final var request = HttpRequest.newBuilder(uri).build();
System.out.println(request.expectContinue()); // false
HttpRequest.Builder GET ()
final var uri = URI.create("https://example.com/");
final var request = HttpRequest.newBuilder(uri).GET().build();
System.out.println(request); // https://example.com/ GET
System.out.println(request.method()); // GET
default HttpRequest.Builder HEAD ()
final var uri = URI.create("https://example.com/");
final var request = HttpRequest.newBuilder(uri).HEAD().build();
System.out.println(request); // https://example.com/ HEAD
System.out.println(request.method()); // HEAD
HttpRequest.Builder header (String name, String value)
final var uri = URI.create("https://example.com/");
final var request = HttpRequest.newBuilder(uri)
.header("aaa", "X")
.header("bbb", "Y1")
.header("bbb", "Y2")
.header("ccc", "Z")
.build();
// https://example.com/ GET
System.out.println(request);
// {aaa=[X], bbb=[Y1, Y2], ccc=[Z]}
System.out.println(request.headers().map());
HttpRequest.Builder headers (String... headers)
final var uri = URI.create("https://example.com/");
final var request = HttpRequest.newBuilder(uri)
.headers("aaa", "X", "bbb", "Y1", "bbb", "Y2", "ccc", "Z")
.build();
// https://example.com/ GET
System.out.println(request);
// {aaa=[X], bbb=[Y1, Y2], ccc=[Z]}
System.out.println(request.headers().map());
HttpRequest.Builder method (String method, HttpRequest.BodyPublisher bodyPublisher)
final var uri = URI.create("https://example.com/");
final var request = HttpRequest.newBuilder(uri)
.method("HEAD", HttpRequest.BodyPublishers.noBody())
.build();
System.out.println(request); // https://example.com/ HEAD
System.out.println(request.method()); // HEAD
HttpRequest.Builder POST (HttpRequest.BodyPublisher bodyPublisher)
final var uri = URI.create("https://example.com/");
final var request = HttpRequest.newBuilder(uri)
.POST(HttpRequest.BodyPublishers.ofString("XYZ"))
.build();
System.out.println(request); // https://example.com/ POST
System.out.println(request.method()); // POST
HttpRequest.Builder PUT (HttpRequest.BodyPublisher bodyPublisher)
final var uri = URI.create("https://example.com/");
final var request = HttpRequest.newBuilder(uri)
.PUT(HttpRequest.BodyPublishers.ofString("XYZ"))
.build();
System.out.println(request); // https://example.com/ PUT
System.out.println(request.method()); // PUT
HttpRequest.Builder setHeader (String name, String value)
final var uri = URI.create("https://example.com/");
final var builder = HttpRequest.newBuilder(uri);
final var request1 = builder
.header("aaa", "X1")
.header("aaa", "X2")
.header("bbb", "Y")
.build();
// {aaa=[X1, X2], bbb=[Y]}
System.out.println(request1.headers().map());
final var request2 = builder
.setHeader("aaa", "X3")
.build();
// {aaa=[X3], bbb=[Y]}
System.out.println(request2.headers().map());
HttpRequest.Builder timeout (Duration duration)
final var uri = URI.create("https://example.com/");
final var request = HttpRequest.newBuilder(uri)
.timeout(Duration.ofSeconds(30))
.build();
System.out.println(request); // https://example.com/ GET
System.out.println(request.timeout()); // Optional[PT30S]
final var uri = URI.create("https://example.com/");
final var request = HttpRequest.newBuilder(uri).build();
System.out.println(request.timeout()); // Optional.empty
HttpRequest.Builder uri (URI uri)
final var uri = URI.create("https://example.com/");
final var request = HttpRequest.newBuilder().uri(uri).build();
System.out.println(request); // https://example.com/ GET
HttpRequest.Builder version (HttpClient.Version version)
final var uri = URI.create("https://example.com/");
final var request = HttpRequest.newBuilder(uri)
.version(HttpClient.Version.HTTP_1_1)
.build();
System.out.println(request); // https://example.com/ GET
System.out.println(request.version()); // Optional[HTTP_1_1]
final var uri = URI.create("https://example.com/");
final var request = HttpRequest.newBuilder(uri).build();
System.out.println(request.version()); // Optional.empty