Java : HttpRequest.BodyPublishers (HTTP通信) - API使用例
HttpRequest.BodyPublishers (Java SE 21 & JDK 21) の使用例まとめです。
だいたいのメソッドを網羅済みです。
API仕様書のおともにどうぞ。
概要
HttpRequest.BodyPublishers は HttpRequest.BodyPublisher を作成するためのクラスです。
BodyPublisher は HttpRequest のボディ(リクエスト本文)を表します。
※このページのコード例では、テストサーバとして node.js を使うことがあります。
// server-post.js
const http = require('http');
const server = http.createServer((req, res) => {
if (req.method == 'POST') {
let body = '';
req.on('data', (chunk) => {
body += chunk;
});
req.on('end', () => {
console.log('body : ' + body);
res.end();
});
}
});
server.listen(8001);
final var uri = URI.create("http://127.0.0.1:8001/");
final var body = HttpRequest.BodyPublishers.ofString("post text");
final var request = HttpRequest.newBuilder(uri)
.POST(body)
.build();
System.out.println(request); // http://127.0.0.1:8001/ POST
try (final var client = HttpClient.newHttpClient()) {
final var response = client.send(
request, HttpResponse.BodyHandlers.ofString());
System.out.println(response); // (POST http://127.0.0.1:8001/) 200
}
// -- node.js --
//$ node server-post.js
//body : post text
メソッド
static HttpRequest.BodyPublisher concat (HttpRequest.BodyPublisher... publishers)
final var uri = URI.create("http://127.0.0.1:8001/");
final var body1 = HttpRequest.BodyPublishers.ofString("abcd");
final var body2 = HttpRequest.BodyPublishers.ofString("XYZ");
final var concatenatedBody = HttpRequest.BodyPublishers.concat(body1, body2);
final var request = HttpRequest.newBuilder(uri)
.POST(concatenatedBody)
.header("Content-Type", "text/plain; charset=UTF-8")
.build();
System.out.println(request); // http://127.0.0.1:8001/ POST
try (final var client = HttpClient.newHttpClient()) {
final var response = client.send(
request, HttpResponse.BodyHandlers.ofString());
System.out.println(response); // (POST http://127.0.0.1:8001/) 200
}
// -- node.js --
//$ node server-post.js
//body : abcdXYZ
static HttpRequest.BodyPublisher fromPublisher (Flow.Publisher<? extends ByteBuffer> publisher)
final var path = Path.of("R:", "java-work", "aaa.txt");
System.out.println(path); // R:\java-work\aaa.txt
Files.writeString(path, "abcd");
try (final var is = Files.newInputStream(path)) {
final var uri = URI.create("http://127.0.0.1:8001/");
final var publisher = HttpRequest.BodyPublishers.ofInputStream(() -> is);
final var body = HttpRequest.BodyPublishers.fromPublisher(publisher);
final var request = HttpRequest.newBuilder(uri)
.POST(body)
.header("Content-Type", "text/plain; charset=UTF-8")
.build();
System.out.println(request); // http://127.0.0.1:8001/ POST
try (final var client = HttpClient.newHttpClient()) {
final var response = client.send(
request, HttpResponse.BodyHandlers.ofString());
System.out.println(response); // (POST http://127.0.0.1:8001/) 200
}
}
// -- node.js --
//$ node server-post.js
//body : abcd
static HttpRequest.BodyPublisher fromPublisher (Flow.Publisher<? extends ByteBuffer> publisher, long contentLength)
final var path = Path.of("R:", "java-work", "aaa.txt");
System.out.println(path); // R:\java-work\aaa.txt
Files.writeString(path, "abcd");
final var length = Files.size(path);
try (final var is = Files.newInputStream(path)) {
final var uri = URI.create("http://127.0.0.1:8001/");
final var publisher = HttpRequest.BodyPublishers.ofInputStream(() -> is);
final var body = HttpRequest.BodyPublishers.fromPublisher(publisher, length);
final var request = HttpRequest.newBuilder(uri)
.POST(body)
.header("Content-Type", "text/plain; charset=UTF-8")
.build();
System.out.println(request); // http://127.0.0.1:8001/ POST
try (final var client = HttpClient.newHttpClient()) {
final var response = client.send(
request, HttpResponse.BodyHandlers.ofString());
System.out.println(response); // (POST http://127.0.0.1:8001/) 200
}
}
// -- node.js --
//$ node server-post.js
//body : abcd
static HttpRequest.BodyPublisher noBody ()
final var uri = URI.create("http://127.0.0.1:8001/");
final var body = HttpRequest.BodyPublishers.noBody();
final var request = HttpRequest.newBuilder(uri)
.POST(body)
.build();
System.out.println(request); // http://127.0.0.1:8001/ POST
try (final var client = HttpClient.newHttpClient()) {
final var response = client.send(
request, HttpResponse.BodyHandlers.ofString());
System.out.println(response); // (POST http://127.0.0.1:8001/) 200
}
// -- node.js --
//$ node server-post.js
//body :
static HttpRequest.BodyPublisher ofByteArray (byte[] buf)
final var uri = URI.create("http://127.0.0.1:8001/");
final byte[] buf = "abcd".getBytes();
System.out.println(Arrays.toString(buf)); // [97, 98, 99, 100]
final var body = HttpRequest.BodyPublishers.ofByteArray(buf);
final var request = HttpRequest.newBuilder(uri)
.POST(body)
.build();
System.out.println(request); // http://127.0.0.1:8001/ POST
try (final var client = HttpClient.newHttpClient()) {
final var response = client.send(
request, HttpResponse.BodyHandlers.ofString());
System.out.println(response); // (POST http://127.0.0.1:8001/) 200
}
// -- node.js --
//$ node server-post.js
//body : abcd
static HttpRequest.BodyPublisher ofByteArray (byte[] buf, int offset, int length)
final var uri = URI.create("http://127.0.0.1:8001/");
final byte[] buf = "abcdef".getBytes();
System.out.println(Arrays.toString(buf)); // [97, 98, 99, 100, 101, 102]
final var body = HttpRequest.BodyPublishers.ofByteArray(buf, 2, 3);
final var request = HttpRequest.newBuilder(uri)
.POST(body)
.build();
System.out.println(request); // http://127.0.0.1:8001/ POST
try (final var client = HttpClient.newHttpClient()) {
final var response = client.send(
request, HttpResponse.BodyHandlers.ofString());
System.out.println(response); // (POST http://127.0.0.1:8001/) 200
}
// -- node.js --
//$ node server-post.js
//body : cde
static HttpRequest.BodyPublisher ofByteArrays (Iterable<byte[]> iter)
final var uri = URI.create("http://127.0.0.1:8001/");
final byte[] buf1 = "ab".getBytes();
final byte[] buf2 = "cd".getBytes();
final byte[] buf3 = "efg".getBytes();
System.out.println(Arrays.toString(buf1)); // [97, 98]
System.out.println(Arrays.toString(buf2)); // [99, 100]
System.out.println(Arrays.toString(buf3)); // [101, 102, 103]
final var iter = List.of(buf1, buf2, buf3);
final var body = HttpRequest.BodyPublishers.ofByteArrays(iter);
final var request = HttpRequest.newBuilder(uri)
.POST(body)
.build();
System.out.println(request); // http://127.0.0.1:8001/ POST
try (final var client = HttpClient.newHttpClient()) {
final var response = client.send(
request, HttpResponse.BodyHandlers.ofString());
System.out.println(response); // (POST http://127.0.0.1:8001/) 200
}
// -- node.js --
//$ node server-post.js
//body : abcdefg
static HttpRequest.BodyPublisher ofFile (Path path)
final var path = Path.of("R:", "java-work", "aaa.txt");
System.out.println(path); // R:\java-work\aaa.txt
Files.writeString(path, "abcd");
final var uri = URI.create("http://127.0.0.1:8001/");
final var body = HttpRequest.BodyPublishers.ofFile(path);
final var request = HttpRequest.newBuilder(uri)
.POST(body)
.header("Content-Type", "text/plain; charset=UTF-8")
.build();
System.out.println(request); // http://127.0.0.1:8001/ POST
try (final var client = HttpClient.newHttpClient()) {
final var response = client.send(
request, HttpResponse.BodyHandlers.ofString());
System.out.println(response); // (POST http://127.0.0.1:8001/) 200
}
// -- node.js --
//$ node server-post.js
//body : abcd
static HttpRequest.BodyPublisher ofInputStream (Supplier<? extends InputStream> streamSupplier)
final Supplier<InputStream> streamSupplier = () -> {
final byte[] buf = "abcd".getBytes();
return new ByteArrayInputStream(buf);
};
final var uri = URI.create("http://127.0.0.1:8001/");
final var body = HttpRequest.BodyPublishers.ofInputStream(streamSupplier);
final var request = HttpRequest.newBuilder(uri)
.POST(body)
.build();
System.out.println(request); // http://127.0.0.1:8001/ POST
try (final var client = HttpClient.newHttpClient()) {
final var response = client.send(
request, HttpResponse.BodyHandlers.ofString());
System.out.println(response); // (POST http://127.0.0.1:8001/) 200
}
// -- node.js --
//$ node server-post.js
//body : abcd
static HttpRequest.BodyPublisher ofString (String body)
final var uri = URI.create("http://127.0.0.1:8001/");
final var body = HttpRequest.BodyPublishers.ofString("post text");
final var request = HttpRequest.newBuilder(uri)
.POST(body)
.build();
System.out.println(request); // http://127.0.0.1:8001/ POST
try (final var client = HttpClient.newHttpClient()) {
final var response = client.send(
request, HttpResponse.BodyHandlers.ofString());
System.out.println(response); // (POST http://127.0.0.1:8001/) 200
}
// -- node.js --
//$ node server-post.js
//body : post text
static HttpRequest.BodyPublisher ofString (String s, Charset charset)
final var uri = URI.create("http://127.0.0.1:8001/");
final var body = HttpRequest.BodyPublishers.ofString(
"abcd", StandardCharsets.US_ASCII);
final var request = HttpRequest.newBuilder(uri)
.POST(body)
.header("Content-Type", "text/plain; charset=US-ASCII")
.build();
System.out.println(request); // http://127.0.0.1:8001/ POST
try (final var client = HttpClient.newHttpClient()) {
final var response = client.send(
request, HttpResponse.BodyHandlers.ofString());
System.out.println(response); // (POST http://127.0.0.1:8001/) 200
}
// -- node.js --
//$ node server-post.js
//body : abcd