Java : HttpResponse - API Examples

HttpResponse (Java SE 18 & JDK 18) API Examples.
You will find code examples on most HttpResponse methods.


Summary

An HTTP response.

Class diagram

Note: Examples on this page may use JDK's Simple Web Server (jwebserver) or Apache HTTP Server.

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();
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 -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/
//127.0.0.1 - - [27/Jul/2022:15:30:50 +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/18.0.1.1
//> Content-length: 0
//>
//< Date: Wed, 27 Jul 2022 06:30:50 GMT
//< Last-modified: Wed, 27 Jul 2022 06:00:22 GMT
//< Content-type: text/html
//< Content-length: 4
//<

Methods

T body ()

Returns the body.

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();

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

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 - - [27/Jul/2022:15:33:20 +0900] "GET / HTTP/1.1" 200 -
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();

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

System.out.println(response); // (GET http://127.0.0.1:8000/) 200
System.out.println(Arrays.toString(response.body())); // [97, 98, 99, 100]

// --- PowerShell ---
//PS R:\java-work> cat .\index.html
//abcd
//
//PS R:\java-work> jwebserver.exe
//...
//127.0.0.1 - - [27/Jul/2022:15:35:16 +0900] "GET / HTTP/1.1" 200 -

HttpHeaders headers ()

Returns the received response headers.

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();
final var response = client.send(request, HttpResponse.BodyHandlers.ofString());

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

System.out.println("-- headers --");
for (final var header : response.headers().map().entrySet()) {
    System.out.println(header.getKey() + " : " + header.getValue());
}

// Result
// ↓
//-- headers --
//content-length : [4]
//content-type : [text/html]
//date : [Wed, 27 Jul 2022 06:39:52 GMT]
//last-modified : [Wed, 27 Jul 2022 06:00:22 GMT]

// --- PowerShell ---
//PS R:\java-work> cat .\index.html
//abcd
//
//PS R:\java-work> jwebserver.exe
//...
//127.0.0.1 - - [27/Jul/2022:15:39:52 +0900] "GET / HTTP/1.1" 200 -

Optional<HttpResponse<T>> previousResponse ()

Returns an Optional containing the previous intermediate response if one was received.

// Test the redirect on the Apache HTTP server.
//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

final var client = HttpClient.newBuilder()
        .followRedirects(HttpClient.Redirect.NORMAL)
        .build();

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());

HttpRequest request ()

Returns the HttpRequest corresponding to this response.

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();
final var response = client.send(request, HttpResponse.BodyHandlers.ofString());

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

final var ret = response.request();
System.out.println(ret); // http://127.0.0.1:8000/ GET
System.out.println(ret.equals(request)); // true

// -- jwebserver --
// ...
//127.0.0.1 - - [27/Jul/2022:15:39:52 +0900] "GET / HTTP/1.1" 200 -

Optional<SSLSession> sslSession ()

Returns an Optional containing the SSLSession in effect for this response.

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

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

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

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

response.sslSession().ifPresent(ssl -> {
    System.out.println(ssl.getPeerHost()); // example.com
    System.out.println(ssl.getProtocol()); // TLSv1.3
    System.out.println(ssl.getCipherSuite()); // TLS_AES_256_GCM_SHA384
});

int statusCode ()

Returns the status code for this response.

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();
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.statusCode()); // 200

// -- jwebserver --
// ...
//127.0.0.1 - - [27/Jul/2022:16:10:04 +0900] "GET / HTTP/1.1" 200 -
final var uri = URI.create("http://127.0.0.1:8000/");

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

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

System.out.println(response); // (DELETE http://127.0.0.1:8000/) 405
System.out.println(response.statusCode()); // 405

// -- jwebserver --
// ...
//127.0.0.1 - - [27/Jul/2022:16:10:04 +0900] "DELETE / HTTP/1.1" 405 -

URI uri ()

Returns the URI that the response was received from.

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();
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.uri()); // http://127.0.0.1:8000/

// -- jwebserver --
// ...
//127.0.0.1 - - [27/Jul/2022:16:12:51 +0900] "GET / HTTP/1.1" 200 -

HttpClient.Version version ()

Returns the HTTP protocol version that was used for this response.

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();
System.out.println(client.version()); // HTTP_2

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.version()); // HTTP_1_1

// -- jwebserver --
// ...
//127.0.0.1 - - [27/Jul/2022:16:14:45 +0900] "GET / HTTP/1.1" 200 -
final var uri = URI.create("https://example.com/");

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

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

Related posts

To top of page