Java : HttpHeaders (HTTP通信) - API使用例
HttpHeaders (Java SE 21 & JDK 21) の使用例まとめです。
だいたいのメソッドを網羅済みです。
API仕様書のおともにどうぞ。
概要
HttpHeaders は HTTP通信のリクエストおよびレスポンスのHTTPヘッダを表します。
HttpRequest と HttpResponse の headers() メソッドから取得できます。
final var uri = URI.create("https://example.com/");
final var request = HttpRequest.newBuilder(uri)
.POST(HttpRequest.BodyPublishers.ofString("xyz"))
.header("Content-Type", "text/plain; charset=utf-8")
.header("Content-Language", "en")
.build();
System.out.println(request); // https://example.com/ POST
final var headers = request.headers();
// {Content-Language=[en], Content-Type=[text/plain; charset=utf-8]}
System.out.println(headers.map());
メソッド
List<String> allValues (String name)
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();
final var headers = request.headers();
System.out.println(headers.allValues("aaa")); // [X]
System.out.println(headers.allValues("bbb")); // [Y1, Y2]
System.out.println(headers.allValues("ccc")); // [Z]
System.out.println(headers.allValues("ddd")); // []
final boolean equals (Object obj)
final var headers1 = HttpHeaders.of(
Map.of(
"aaa", List.of("X1", "X2"),
"bbb", List.of("Y")
),
(n, v) -> true);
final var headers2 = HttpHeaders.of(
Map.of(
"aaa", List.of("X1", "X2"),
"bbb", List.of("Y")
),
(n, v) -> true);
final var headers3 = HttpHeaders.of(
Map.of(
"aaa", List.of("X1", "X2"),
"ccc", List.of("Z")
),
(n, v) -> true);
System.out.println(headers1.equals(headers2)); // true
System.out.println(headers1.equals(headers3)); // false
Optional<String> firstValue (String name)
final var uri = URI.create("https://example.com/");
final var request = HttpRequest.newBuilder(uri)
.header("aaa", "X")
.header("bbb", "Y1")
.header("bbb", "Y2")
.build();
final var headers = request.headers();
System.out.println(headers.firstValue("aaa")); // Optional[X]
System.out.println(headers.firstValue("bbb")); // Optional[Y1]
System.out.println(headers.firstValue("ccc")); // Optional.empty
OptionalLong firstValueAsLong (String name)
final var uri = URI.create("https://example.com/");
final var request = HttpRequest.newBuilder(uri)
.header("aaa", "123")
.header("bbb", "456")
.header("bbb", "789")
.build();
final var headers = request.headers();
System.out.println(headers.firstValueAsLong("aaa")); // OptionalLong[123]
System.out.println(headers.firstValueAsLong("bbb")); // OptionalLong[456]
System.out.println(headers.firstValueAsLong("ccc")); // OptionalLong.empty
final int hashCode ()
final var headers1 = HttpHeaders.of(
Map.of(
"aaa", List.of("X1", "X2"),
"bbb", List.of("Y")
),
(n, v) -> true);
final var headers2 = HttpHeaders.of(
Map.of(
"ccc", List.of("Z")
),
(n, v) -> true);
final var headers3 = HttpHeaders.of(
Map.of(
"ddd", List.of("123")
),
(n, v) -> true);
System.out.println(headers1.hashCode()); // 107261
System.out.println(headers2.hashCode()); // 98426
System.out.println(headers3.hashCode()); // 81333
Map<String,List<String>> map ()
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();
final var headers = request.headers();
final var map = headers.map();
System.out.println("-- for --");
for (final var entry : map.entrySet()) {
System.out.println(entry.getKey() + " : " + entry.getValue());
}
// 結果
// ↓
//-- for --
//aaa : [X]
//bbb : [Y1, Y2]
//ccc : [Z]
static HttpHeaders of (Map<String,List<String>> headerMap, BiPredicate<String,String> filter)
final var headerMap = Map.of(
"aaa", List.of("X"),
"bbb", List.of("Y1", "Y2"),
"ccc", List.of("Z")
);
final var headers1 = HttpHeaders.of(headerMap, (n, v) -> true);
System.out.println(headers1.map()); // {aaa=[X], bbb=[Y1, Y2], ccc=[Z]}
final var headers2 = HttpHeaders.of(headerMap, (n, v) -> false);
System.out.println(headers2.map()); // {}
final var headers3 = HttpHeaders.of(headerMap,
(name, value) -> !name.equals("aaa"));
System.out.println(headers3.map()); // {bbb=[Y1, Y2], ccc=[Z]}
final var headers4 = HttpHeaders.of(headerMap,
(name, value) -> value.equals("Z"));
System.out.println(headers4.map()); // {ccc=[Z]}
final var headers5 = HttpHeaders.of(headerMap,
(name, value) -> value.equals("Y2"));
System.out.println(headers5.map()); // {bbb=[Y2]}
String toString ()
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();
final var headers = request.headers();
// java.net.http.HttpHeaders@31790 { {aaa=[X], bbb=[Y1, Y2], ccc=[Z]} }
System.out.println(headers.toString());