広告

Java : HttpClient (HTTP通信) - API使用例

HttpClient (Java SE 21 & JDK 21) の使用例まとめです。
だいたいのメソッドを網羅済みです。
API仕様書のおともにどうぞ。


概要

HTTPクライアント。

クラス構成

HttpClient は、Java で HTTP通信をするための中心となるクラスです。

大まかな処理の流れとしては…

  1. まずは HttpRequest を生成。
    (通信先となる URI や、POST や GET といったリクエストメソッドを指定します)
  2. HttpClient.send メソッドに HttpRequest を指定して通信開始。
  3. 結果を HttpResponse として取得。

となります。

補足

  • Java 21 から HttpClient は AutoCloseable を実装するようになりました。
// --- PowerShell ---
//PS R:\java-work> cat .\index.html
//abcd
//
//PS R:\java-work> jwebserver.exe -o verbose
//Binding to loopback by default. For all interfaces use "-b 0.0.0.0" or "-b ::".
//Serving R:\java-work and subdirectories on 127.0.0.1 port 8000
//URL http://127.0.0.1:8000/

final var uri = URI.create("http://127.0.0.1:8000/");
System.out.println(uri); // http://127.0.0.1:8000/

final var request = HttpRequest.newBuilder(uri).build();
System.out.println(request); // http://127.0.0.1:8000/ GET

try (final var client = HttpClient.newHttpClient()) {
    final var response = client.send(
            request, HttpResponse.BodyHandlers.ofString());

    System.out.println(response); // (GET http://127.0.0.1:8000/) 200
    System.out.println(response.body()); // abcd
}

// --- PowerShell ---
//PS R:\java-work> jwebserver.exe -o verbose
// ...
//127.0.0.1 - - [30/Mar/2024:15:09:08 +0900] "GET / HTTP/1.1" 200 -
//Resource requested: R:\java-work
//> Connection: Upgrade, HTTP2-Settings
//> Http2-settings: AAEAAEAAAAIAAAABAAMAAABkAAQBAAAAAAUAAEAA
//> Host: 127.0.0.1:8000
//> Upgrade: h2c
//> User-agent: Java-http-client/21.0.1
//>
//< Date: Sat, 30 Mar 2024 06:09:08 GMT
//< Last-modified: Sat, 30 Mar 2024 05:30:53 GMT
//< Content-type: text/html
//< Content-length: 4
//<
// 非同期の例です。
final var uri = URI.create("http://127.0.0.1:8000/");
final var request = HttpRequest.newBuilder(uri).build();

try (final var client = HttpClient.newHttpClient()) {

    System.out.println("-- sendAsync start --");

    final var future = client.sendAsync(
                    request, HttpResponse.BodyHandlers.ofString())
            .thenApply(response -> {
                System.out.println("thenApply");
                System.out.println("  status code : " + response.statusCode());
                return response.body();
            })
            .thenAccept(body -> {
                System.out.println("thenAccept");
                System.out.println("  body : " + body);
            });

    System.out.println("-- sendAsync sending ...");

    future.join();

    System.out.println("-- end --");
}

// 結果
// ↓
//-- sendAsync start --
//-- sendAsync sending ...
//thenApply
//  status code : 200
//thenAccept
//  body : abcd
//-- end --

コンストラクタ

HttpClient ()

HttpClientを作成します。

protectedです。
独自にサブクラスを作ることは少ないと思いますので、コード例は割愛します。

メソッド

abstract Optional<Authenticator> authenticator ()

このクライアントに設定されたAuthenticatorを含むOptionalを返します。

テストサーバとして Apache HTTP Server を使っています。
Basic認証用に user=abcd, password=XYZ を用意します。

コード例では HTTP で通信していますが、通常は HTTPS を使ったほうがよいでしょう。

xxxx@xxxx:/var/www/html/auth-test# cat index.html
Auth OK!

xxxx@xxxx:/var/www/html/auth-test# cat .htaccess
AuthType Basic
AuthName "Restricted Files"
AuthUserFile /var/www/test/.htpasswd
Require valid-user

xxxx@xxxx:/var/www/html/auth-test# cat /var/www/test/.htpasswd
abcd:DkUltt9UxTbcs
(user = abcd, password = XYZ)

認証成功の例です。

final var authenticator = new Authenticator() {
    @Override
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication("abcd", "XYZ".toCharArray());
    }
};

final var uri = URI.create("http://192.168.1.9/auth-test/index.html");

final var request = HttpRequest.newBuilder(uri).build();
System.out.println(request); // http://192.168.1.9/auth-test/index.html GET

try (final var client = HttpClient.newBuilder()
        .authenticator(authenticator)
        .build()) {

    client.authenticator().ifPresent(a -> {
        System.out.println(a == authenticator); // true
    });

    final var response = client.send(request, HttpResponse.BodyHandlers.ofString());

    System.out.println(response); // (GET http://192.168.1.9/auth-test/index.html) 200
    System.out.println(response.body()); // Auth OK!
}

認証失敗の例です。

final var uri = URI.create("http://192.168.1.9/auth-test/index.html");

final var request = HttpRequest.newBuilder(uri).build();
System.out.println(request); // http://192.168.1.9/auth-test/index.html GET

try (final var client = HttpClient.newHttpClient()) {
    final var response = client.send(request, HttpResponse.BodyHandlers.ofString());

    System.out.println(response); // (GET http://192.168.1.9/auth-test/index.html) 401
    System.out.println(response.statusCode()); // 401
}

boolean awaitTermination (Duration duration)

シャットダウン・リクエストの後にすべての操作の実行が完了するか、durationが経過するか、現在のスレッドがinterrupted(いずれか早い方)になるまでブロックします。

このコード例では node.js をテストサーバとして使います。

// server.js

const http = require('http');
const server = http.createServer((req, res) => {

  console.log('sleep 5 seconds ...');

  setTimeout(() => {
    console.log('end');
    res.end();
  }, 5000);

});

server.listen(8001);
final var uri = URI.create("http://127.0.0.1:8001/");

final var request = HttpRequest.newBuilder(uri).build();
System.out.println(request); // http://127.0.0.1:8001/ GET

final var client = HttpClient.newHttpClient();
try {
    client.sendAsync(request, HttpResponse.BodyHandlers.ofString());
} finally {
    client.shutdown();
}

System.out.println("-- awaitTermination --");

final var startTime = System.nanoTime();

final var duration = Duration.ofSeconds(3);
final var term = client.awaitTermination(duration);
System.out.println("term : " + term);

final var elapsedSec = (System.nanoTime() - startTime) / 1000000000.0;
System.out.println("elapsed time : " + elapsedSec + " sec.");

System.out.println("isTerminated = " + client.isTerminated());

// 結果
// ↓
//-- awaitTermination --
//term : false
//elapsed time : 3.0043146 sec.
//isTerminated = false

// -- node.js --
//$ node server.js
//sleep 5 seconds ...
//end
final var uri = URI.create("http://127.0.0.1:8001/");

final var request = HttpRequest.newBuilder(uri).build();
System.out.println(request); // http://127.0.0.1:8001/ GET

final var client = HttpClient.newHttpClient();
try {
    client.sendAsync(request, HttpResponse.BodyHandlers.ofString());
} finally {
    client.shutdown();
}

System.out.println("-- awaitTermination --");

final var startTime = System.nanoTime();

final var duration = Duration.ofSeconds(10);
final var term = client.awaitTermination(duration);
System.out.println("term : " + term);

final var elapsedSec = (System.nanoTime() - startTime) / 1000000000.0;
System.out.println("elapsed time : " + elapsedSec + " sec.");

System.out.println("isTerminated = " + client.isTerminated());

// 結果
// ↓
//-- awaitTermination --
//term : true
//elapsed time : 5.00663 sec.
//isTerminated = true

// -- node.js --
//$ node server.js
//sleep 5 seconds ...
//end

void close ()

以前にsendまたはsendAsyncに送信されたリクエストが完了するまで実行されるが、新しいリクエストは受け入れられないように、正常な停止を開始します。

可能であれば try-with-resources文 を使うことをおすすめします。

final var uri = URI.create("http://127.0.0.1:8000/");
final var request = HttpRequest.newBuilder(uri).build();
System.out.println(request); // http://127.0.0.1:8000/ GET

try (final var client = HttpClient.newHttpClient()) {
    final var response = client.send(
            request, HttpResponse.BodyHandlers.ofString());

    System.out.println(response); // (GET http://127.0.0.1:8000/) 200
}

// -- jwebserver --
//PS R:\java-work> jwebserver.exe
//...
//127.0.0.1 - - [30/Mar/2024:18:39:04 +0900] "GET / HTTP/1.1" 200 -

try-with-resources文を使わない例です。

final var uri = URI.create("http://127.0.0.1:8000/");
final var request = HttpRequest.newBuilder(uri).build();
System.out.println(request); // http://127.0.0.1:8000/ GET

final var client = HttpClient.newHttpClient();
try {
    final var response = client.send(
            request, HttpResponse.BodyHandlers.ofString());

    System.out.println(response); // (GET http://127.0.0.1:8000/) 200
} finally {
    client.close();
}

// -- jwebserver --
//PS R:\java-work> jwebserver.exe
//...
//127.0.0.1 - - [30/Mar/2024:18:39:04 +0900] "GET / HTTP/1.1" 200 -

abstract Optional<Duration> connectTimeout ()

このクライアントの「接続タイムアウト時間」を含むOptionalを返します。

// タイムアウトを起こすために、example.com に対して意図的に 8000 ポートを使います。
final var uri = URI.create("https://example.com:8000/");

final var request = HttpRequest.newBuilder(uri).build();
System.out.println(request); // https://example.com:8000/ GET

try (final var client = HttpClient.newBuilder()
        .connectTimeout(Duration.ofSeconds(5))
        .build()) {

    System.out.println(client.connectTimeout()); // Optional[PT5S]

    final var startTime = System.nanoTime();

    try {
        System.out.println("-- send request --");
        final var response = client.send(
                request, HttpResponse.BodyHandlers.ofString());
    } catch (HttpConnectTimeoutException e) {
        System.out.println(e);

        final var elapsedSec = (System.nanoTime() - startTime) / 1000000000.0;
        System.out.println("timeout : " + elapsedSec + " sec.");
    }
}

// 結果
// ↓
//-- send request --
//java.net.http.HttpConnectTimeoutException: HTTP connect timed out
//timeout : 5.0700876 sec.
try (final var client = HttpClient.newHttpClient()) {
    System.out.println(client.connectTimeout()); // Optional.empty
}

abstract Optional<CookieHandler> cookieHandler ()

このクライアントCookieHandlerを含むOptionalを返します。

final var uri = URI.create("http://127.0.0.1:8000/");
final var request = HttpRequest.newBuilder(uri).build();

final var cookieHandler = new CookieHandler() {
    @Override
    public Map<String, List<String>> get(URI uri, Map<String, List<String>> requestHeaders) {
        System.out.println("-- cookie get --");
        System.out.println("uri : " + uri);
        System.out.println("requestHeaders : " + requestHeaders);
        return Map.of();
    }

    @Override
    public void put(URI uri, Map<String, List<String>> responseHeaders) {
        System.out.println("-- cookie put --");
        System.out.println("uri : " + uri);

        System.out.println("responseHeaders : ");
        for (final var entry : responseHeaders.entrySet()) {
            System.out.println("  " + entry.getKey() + " : " + entry.getValue());
        }
    }
};

try (final var client = HttpClient.newBuilder()
        .cookieHandler(cookieHandler)
        .build()) {

    client.cookieHandler().ifPresent(handler -> {
        System.out.println("cookieHandler : " + (cookieHandler == handler));
    });

    final var response = client.send(request, HttpResponse.BodyHandlers.ofString());
    System.out.println("res : " + response);
}

// 結果
// ↓
//cookieHandler : true
//-- cookie get --
//uri : http://127.0.0.1:8000/
//requestHeaders : {}
//-- cookie put --
//uri : http://127.0.0.1:8000/
//responseHeaders :
//  content-length : [4]
//  content-type : [text/html]
//  date : [Sat, 30 Mar 2024 06:29:22 GMT]
//  last-modified : [Sat, 30 Mar 2024 05:30:53 GMT]
//res : (GET http://127.0.0.1:8000/) 200

// --- PowerShell ---
//PS R:\java-work> cat .\index.html
//abcd
//
//PS R:\java-work> jwebserver.exe
//...
//127.0.0.1 - - [31/Mar/2024:16:05:09 +0900] "GET / HTTP/1.1" 200 -
try (final var client = HttpClient.newHttpClient()) {
    System.out.println(client.cookieHandler()); // Optional.empty
}

abstract Optional<Executor> executor ()

このクライアントExecutorを含むOptionalを返します。

final var uri = URI.create("http://127.0.0.1:8000/");
final var request = HttpRequest.newBuilder(uri).build();

try (final var executor = Executors.newSingleThreadExecutor();
     final var client = HttpClient.newBuilder().executor(executor).build()) {

    client.executor().ifPresent(e -> {
        System.out.println("executor : " + (e == executor));
    });

    final var response = client.send(request, HttpResponse.BodyHandlers.ofString());
    System.out.println("res : " + response);
}

// 結果
// ↓
//executor : true
//res : (GET http://127.0.0.1:8000/) 200

// -- jwebserver --
//PS R:\java-work> jwebserver.exe
//...
//127.0.0.1 - - [30/Mar/2024:15:38:41 +0900] "GET / HTTP/1.1" 200 -
try (final var client = HttpClient.newHttpClient()) {
    System.out.println(client.executor()); // Optional.empty
}

abstract HttpClient.Redirect followRedirects ()

このクライアントのフォロー・リダイレクト・ポリシーを返します。

リダイレクト用のテストサーバとして Apache HTTP Server を使っています。
http://192.168.1.9/redirect-test/src.html にアクセスすると https://example.com/ へリダイレクトします。

xxxx@xxxx:/var/www/html/redirect-test$ cat .htaccess
RewriteEngine on
RewriteRule ^src.html$ https://example.com/ [L,R=301]
final var uri = URI.create("http://192.168.1.9/redirect-test/src.html");

final var request = HttpRequest.newBuilder(uri).build();
System.out.println(request); // http://192.168.1.9/redirect-test/src.html GET

try (final var client = HttpClient.newBuilder()
        .followRedirects(HttpClient.Redirect.NORMAL)
        .build()) {
    System.out.println(client.followRedirects()); // NORMAL

    final var response = client.send(request, HttpResponse.BodyHandlers.ofString());
    System.out.println(response); // (GET https://example.com/) 200

    // Optional[(GET http://192.168.1.9/redirect-test/src.html) 301]
    System.out.println(response.previousResponse());
}
final var uri = URI.create("http://192.168.1.9/redirect-test/src.html");

final var request = HttpRequest.newBuilder(uri).build();
System.out.println(request); // http://192.168.1.9/redirect-test/src.html GET

try (final var client = HttpClient.newHttpClient()) {
    System.out.println(client.followRedirects()); // NEVER

    final var response = client.send(request, HttpResponse.BodyHandlers.ofString());
    System.out.println(response); // (GET http://192.168.1.9/redirect-test/src.html) 301
}

boolean isTerminated ()

停止後にすべての操作が完了した場合は、trueを返します。

このコード例では node.js をテストサーバとして使います。

// server.js

const http = require('http');
const server = http.createServer((req, res) => {

  console.log('sleep 5 seconds ...');

  setTimeout(() => {
    console.log('end');
    res.end();
  }, 5000);

});

server.listen(8001);
final var uri = URI.create("http://127.0.0.1:8001/");

final var request = HttpRequest.newBuilder(uri).build();
System.out.println(request); // http://127.0.0.1:8001/ GET

final var client = HttpClient.newHttpClient();
try {
    client.sendAsync(request, HttpResponse.BodyHandlers.ofString());
} finally {
    client.shutdown();
}

System.out.println("-- awaitTermination --");

final var startTime = System.nanoTime();

final var duration = Duration.ofSeconds(3);
final var term = client.awaitTermination(duration);
System.out.println("term : " + term);

final var elapsedSec = (System.nanoTime() - startTime) / 1000000000.0;
System.out.println("elapsed time : " + elapsedSec + " sec.");

System.out.println("isTerminated = " + client.isTerminated());

// 結果
// ↓
//-- awaitTermination --
//term : false
//elapsed time : 3.0043146 sec.
//isTerminated = false

// -- node.js --
//$ node server.js
//sleep 5 seconds ...
//end
final var uri = URI.create("http://127.0.0.1:8001/");

final var request = HttpRequest.newBuilder(uri).build();
System.out.println(request); // http://127.0.0.1:8001/ GET

final var client = HttpClient.newHttpClient();
try {
    client.sendAsync(request, HttpResponse.BodyHandlers.ofString());
} finally {
    client.shutdown();
}

System.out.println("-- awaitTermination --");

final var startTime = System.nanoTime();

final var duration = Duration.ofSeconds(10);
final var term = client.awaitTermination(duration);
System.out.println("term : " + term);

final var elapsedSec = (System.nanoTime() - startTime) / 1000000000.0;
System.out.println("elapsed time : " + elapsedSec + " sec.");

System.out.println("isTerminated = " + client.isTerminated());

// 結果
// ↓
//-- awaitTermination --
//term : true
//elapsed time : 5.00663 sec.
//isTerminated = true

// -- node.js --
//$ node server.js
//sleep 5 seconds ...
//end

static HttpClient.Builder newBuilder ()

新しいHttpClientビルダーを作成します。

try (final var client = HttpClient.newBuilder()
        .connectTimeout(Duration.ofSeconds(15))
        .version(HttpClient.Version.HTTP_1_1)
        .build()) {

    System.out.println(client.connectTimeout()); // Optional[PT15S]
    System.out.println(client.version()); // HTTP_1_1

    final var uri = URI.create("http://127.0.0.1:8000/");
    final var request = HttpRequest.newBuilder(uri).build();

    final var response = client.send(request, HttpResponse.BodyHandlers.ofString());
    System.out.println(response); // (GET http://127.0.0.1:8000/) 200
}

// -- jwebserver --
//PS R:\java-work> jwebserver.exe
//...
//127.0.0.1 - - [30/Mar/2024:15:41:22 +0900] "GET / HTTP/1.1" 200 -
try (final var client = HttpClient.newHttpClient()) {

    System.out.println(client.connectTimeout()); // Optional.empty
    System.out.println(client.version()); // HTTP_2

    final var uri = URI.create("http://127.0.0.1:8000/");
    final var request = HttpRequest.newBuilder(uri).build();

    final var response = client.send(request, HttpResponse.BodyHandlers.ofString());
    System.out.println(response); // (GET http://127.0.0.1:8000/) 200
}

// -- jwebserver --
//PS R:\java-work> jwebserver.exe
//...
//127.0.0.1 - - [30/Mar/2024:15:41:22 +0900] "GET / HTTP/1.1" 200 -

static HttpClient newHttpClient ()

デフォルト設定を使用して新しいHttpClientを返します。

try (final var client = HttpClient.newBuilder()
        .connectTimeout(Duration.ofSeconds(15))
        .version(HttpClient.Version.HTTP_1_1)
        .build()) {

    System.out.println(client.connectTimeout()); // Optional[PT15S]
    System.out.println(client.version()); // HTTP_1_1

    final var uri = URI.create("http://127.0.0.1:8000/");
    final var request = HttpRequest.newBuilder(uri).build();

    final var response = client.send(request, HttpResponse.BodyHandlers.ofString());
    System.out.println(response); // (GET http://127.0.0.1:8000/) 200
}

// -- jwebserver --
//PS R:\java-work> jwebserver.exe
//...
//127.0.0.1 - - [30/Mar/2024:15:41:22 +0900] "GET / HTTP/1.1" 200 -
try (final var client = HttpClient.newHttpClient()) {

    System.out.println(client.connectTimeout()); // Optional.empty
    System.out.println(client.version()); // HTTP_2

    final var uri = URI.create("http://127.0.0.1:8000/");
    final var request = HttpRequest.newBuilder(uri).build();

    final var response = client.send(request, HttpResponse.BodyHandlers.ofString());
    System.out.println(response); // (GET http://127.0.0.1:8000/) 200
}

// -- jwebserver --
//PS R:\java-work> jwebserver.exe
//...
//127.0.0.1 - - [30/Mar/2024:15:41:22 +0900] "GET / HTTP/1.1" 200 -

WebSocket.Builder newWebSocketBuilder ()

新しいWebSocketビルダー(オプションの操作)を作成します。

WebSocket のテストサーバには、下記の wscat を使っています。
https://github.com/websockets/wscat

try (final var client = HttpClient.newBuilder()
        .connectTimeout(Duration.ofSeconds(5))
        .build()) {

    final var uri = URI.create("ws://127.0.0.1:3000/");
    final var data = "abcd";

    final var future = client.newWebSocketBuilder()
            .buildAsync(uri, new WebSocket.Listener() {
                @Override
                public void onOpen(WebSocket webSocket) {
                    System.out.println("-- onOpen --");
                }
            })
            .thenCompose(ws -> {
                System.out.println("sendText : " + data);
                return ws.sendText(data, true);
            })
            .thenCompose(ws -> {
                System.out.println("sendClose");
                return ws.sendClose(WebSocket.NORMAL_CLOSURE, "ok");
            })
            .thenRun(() -> {
                System.out.println("-- shutdown --");
                client.shutdownNow();
            });

    future.join();
}

// 結果
// ↓
//-- onOpen --
//sendText : abcd
//sendClose
//-- shutdown --

// -- wscat --
//$ wscat -l 3000
//> abcd

abstract Optional<ProxySelector> proxy ()

このクライアントに提供されたProxySelectorを含むOptionalを返します。

final var uri = URI.create("http://127.0.0.1:8000/");

final var request = HttpRequest.newBuilder(uri).build();
System.out.println(request); // http://127.0.0.1:8000/ GET

final var proxy = ProxySelector.getDefault();

try (final var client = HttpClient.newBuilder()
        .proxy(proxy)
        .build()) {

    client.proxy().ifPresent(p -> {
        System.out.println(p == proxy); // true
    });

    final var response = client.send(request, HttpResponse.BodyHandlers.ofString());
    System.out.println(response); // (GET http://127.0.0.1:8000/) 200
}

// -- jwebserver --
//PS R:\java-work> jwebserver.exe
//...
//127.0.0.1 - - [30/Mar/2024:16:13:58 +0900] "GET / HTTP/1.1" 200 -
try (final var client = HttpClient.newHttpClient()) {
    System.out.println(client.proxy()); // Optional.empty
}

abstract <T> HttpResponse<T> send (HttpRequest request, HttpResponse.BodyHandler<T> responseBodyHandler)

このクライアントを使用して指定されたリクエストを送信し、必要に応じてブロックしてレスポンスを取得します。

// 結果を文字列で取得する例です。
final var uri = URI.create("http://127.0.0.1:8000/");

final var request = HttpRequest.newBuilder(uri).build();
System.out.println(request); // http://127.0.0.1:8000/ GET

try (final var client = HttpClient.newHttpClient()) {
    final var response = client.send(
            request, HttpResponse.BodyHandlers.ofString());

    System.out.println(response); // (GET http://127.0.0.1:8000/) 200
    System.out.println(response.body()); // abcd
}

// --- PowerShell ---
//PS R:\java-work> cat .\index.html
//abcd
//
//PS R:\java-work> jwebserver.exe
//...
//127.0.0.1 - - [31/Mar/2024:16:06:04 +0900] "GET / HTTP/1.1" 200 -
// 結果をファイルに保存する例です。
final var file = Path.of("R:", "java-work", "save.txt");
System.out.println(file); // R:\java-work\save.txt

System.out.println(Files.notExists(file)); // true

final var uri = URI.create("http://127.0.0.1:8000/");

final var request = HttpRequest.newBuilder(uri).build();
System.out.println(request); // http://127.0.0.1:8000/ GET

try (final var client = HttpClient.newHttpClient()) {
    final var response = client.send(
            request, HttpResponse.BodyHandlers.ofFile(file));

    System.out.println(response); // (GET http://127.0.0.1:8000/) 200
}

System.out.println(Files.readString(file)); // abcd

abstract <T> CompletableFuture<HttpResponse<T>> sendAsync (HttpRequest request, HttpResponse.BodyHandler<T> responseBodyHandler)

指定されたレスポンス本文ハンドラとともに、このクライアントを使用して、指定されたリクエストを非同期に送信します。

final var uri = URI.create("http://127.0.0.1:8000/");
final var request = HttpRequest.newBuilder(uri).build();

try (final var client = HttpClient.newHttpClient()) {

    System.out.println("-- sendAsync start --");

    final var future = client.sendAsync(
                    request, HttpResponse.BodyHandlers.ofString())
            .thenApply(response -> {
                System.out.println("thenApply");
                System.out.println("  status code : " + response.statusCode());
                return response.body();
            })
            .thenAccept(body -> {
                System.out.println("thenAccept");
                System.out.println("  body : " + body);
            });

    System.out.println("-- sendAsync sending ...");

    future.get();

    System.out.println("-- end --");
}

// 結果
// ↓
//-- sendAsync start --
//-- sendAsync sending ...
//thenApply
//  status code : 200
//thenAccept
//  body : abcd
//-- end --

// --- PowerShell ---
//PS R:\java-work> cat .\index.html
//abcd
//
//PS R:\java-work> jwebserver.exe
//...
//127.0.0.1 - - [31/Mar/2024:16:06:53 +0900] "GET / HTTP/1.1" 200 -

abstract <T> CompletableFuture<HttpResponse<T>> sendAsync (HttpRequest request, HttpResponse.BodyHandler<T> responseBodyHandler, HttpResponse.PushPromiseHandler<T> pushPromiseHandler)

指定されたレスポンス本文ハンドラとともにこのクライアントを使用して、指定されたリクエストを非同期に送信し、プッシュ・プロミス・ハンドラをプッシュします。

コード例準備中です…

void shutdown ()

sendまたはsendAsyncで以前に送信されたリクエストが完了まで実行され、新しいリクエストは受け入れられないように、正常な停止を開始します。

final var uri = URI.create("http://127.0.0.1:8001/");

final var request = HttpRequest.newBuilder(uri).build();
System.out.println(request); // http://127.0.0.1:8001/ GET

final var client = HttpClient.newHttpClient();
try {
    client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
            .thenAccept(res -> {
                System.out.println("-- accept --");
                System.out.println("res : " + res);
            });
} finally {
    System.out.println("-- shutdown --");
    client.shutdown();
}

final var term = client.awaitTermination(Duration.ofSeconds(10));
System.out.println("term : " + term);

// 結果
// ↓
//http://127.0.0.1:8001/ GET
//-- shutdown --
//-- accept --
//res : (GET http://127.0.0.1:8001/) 200
//term : true
final var uri = URI.create("http://127.0.0.1:8001/");

final var request = HttpRequest.newBuilder(uri).build();
System.out.println(request); // http://127.0.0.1:8001/ GET

final var client = HttpClient.newHttpClient();
try {
    client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
            .thenAccept(res -> {
                System.out.println("-- accept --");
                System.out.println("res : " + res);
            });
} finally {
    System.out.println("-- shutdownNow --");
    client.shutdownNow();
}

final var term = client.awaitTermination(Duration.ofSeconds(10));
System.out.println("term : " + term);

// 結果
// ↓
//http://127.0.0.1:8001/ GET
//-- shutdownNow --
//term : true

void shutdownNow ()

このメソッドは、即時シャットダウンを開始しようとします。

final var uri = URI.create("http://127.0.0.1:8001/");

final var request = HttpRequest.newBuilder(uri).build();
System.out.println(request); // http://127.0.0.1:8001/ GET

final var client = HttpClient.newHttpClient();
try {
    client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
            .thenAccept(res -> {
                System.out.println("-- accept --");
                System.out.println("res : " + res);
            });
} finally {
    System.out.println("-- shutdown --");
    client.shutdown();
}

final var term = client.awaitTermination(Duration.ofSeconds(10));
System.out.println("term : " + term);

// 結果
// ↓
//http://127.0.0.1:8001/ GET
//-- shutdown --
//-- accept --
//res : (GET http://127.0.0.1:8001/) 200
//term : true
final var uri = URI.create("http://127.0.0.1:8001/");

final var request = HttpRequest.newBuilder(uri).build();
System.out.println(request); // http://127.0.0.1:8001/ GET

final var client = HttpClient.newHttpClient();
try {
    client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
            .thenAccept(res -> {
                System.out.println("-- accept --");
                System.out.println("res : " + res);
            });
} finally {
    System.out.println("-- shutdownNow --");
    client.shutdownNow();
}

final var term = client.awaitTermination(Duration.ofSeconds(10));
System.out.println("term : " + term);

// 結果
// ↓
//http://127.0.0.1:8001/ GET
//-- shutdownNow --
//term : true

abstract SSLContext sslContext ()

このクライアントのSSLContextを返します。

final var uri = URI.create("https://example.com/");

final var request = HttpRequest.newBuilder(uri).build();
System.out.println(request); // https://example.com/

try (final var client = HttpClient.newHttpClient()) {

    System.out.println(client.sslContext().getProtocol()); // Default

    final var response = client.send(request, HttpResponse.BodyHandlers.ofString());

    System.out.println(response); // (GET https://example.com/) 200
}

abstract SSLParameters sslParameters ()

このクライアントのSSLParametersのコピーを返します。

final var uri = URI.create("https://example.com/");

final var request = HttpRequest.newBuilder(uri).build();
System.out.println(request); // https://example.com/ GET

try (final var client = HttpClient.newHttpClient()) {

    final var sslParams = client.sslParameters();
    System.out.println(Arrays.toString(sslParams.getProtocols())); // [TLSv1.3, TLSv1.2]

    final var response = client.send(request, HttpResponse.BodyHandlers.ofString());

    System.out.println(response); // (GET https://example.com/) 200
}

abstract HttpClient.Version version ()

このクライアントの優先HTTPプロトコル・バージョンを返します。

final var uri = URI.create("https://example.com/");

final var request = HttpRequest.newBuilder(uri).build();
System.out.println(request); // https://example.com/ GET

try (final var client = HttpClient.newBuilder()
        .version(HttpClient.Version.HTTP_1_1)
        .build()) {
    System.out.println(client.version()); // HTTP_1_1

    final var response = client.send(request, HttpResponse.BodyHandlers.ofString());

    System.out.println(response); // (GET https://example.com/) 200
    System.out.println(response.version()); // HTTP_1_1
}
final var uri = URI.create("https://example.com/");

final var request = HttpRequest.newBuilder(uri).build();
System.out.println(request); // https://example.com/ GET

try (final var client = HttpClient.newHttpClient()) {
    System.out.println(client.version()); // HTTP_2

    final var response = client.send(request, HttpResponse.BodyHandlers.ofString());

    System.out.println(response); // (GET https://example.com/) 200
    System.out.println(response.version()); // HTTP_2
}

関連記事

ページの先頭へ