Java : HttpClient.Version with Examples

HttpClient.Version (Java SE 21 & JDK 21) with Examples.
You will find code examples on most HttpClient.Version methods.


Summary

The HTTP protocol version.

Class diagram

final var version = HttpClient.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.newBuilder()
        .version(version)
        .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
}

Enum Constants

HTTP_1_1

HTTP version 1.1

final var version = HttpClient.Version.HTTP_1_1;
System.out.println(version); // HTTP_1_1

HTTP_2

HTTP version 2

final var version = HttpClient.Version.HTTP_2;
System.out.println(version); // HTTP_2

Methods

static HttpClient.Version valueOf (String name)

Returns the enum constant of this class with the specified name.

final var version = HttpClient.Version.valueOf("HTTP_2");
System.out.println(version); // HTTP_2

static HttpClient.Version[] values ()

Returns an array containing the constants of this enum class, in the order they are declared.

final var versions = HttpClient.Version.values();
System.out.println(Arrays.toString(versions)); // [HTTP_1_1, HTTP_2]

Methods declared in Enum

clone, compareTo, describeConstable, equals, finalize, getDeclaringClass, hashCode, name, ordinal, toString, valueOf

Please see the link below.


Related posts

To top of page