APIdia Launch Partner
Top Description Inners Methods
java.net.http

public Interface HttpResponse<T>

Known Direct Implementers
jdk.internal.net.http.HttpResponseImpl
Type Parameters
<T>
the response body type
Imports
java.io.BufferedReader, .IOException, .InputStream, java.net.URI, java.nio.ByteBuffer, java.nio.charset.Charset, .StandardCharsets, java.nio.channels.FileChannel, java.nio.file.OpenOption, .Path, java.util.List, .Objects, .Optional, java.util.concurrent.CompletableFuture, .CompletionStage, .ConcurrentMap, .Flow, .Flow.Subscriber, .Flow.Publisher, .Flow.Subscription, java.util.function.Consumer, .Function, .Supplier, java.util.stream.Stream, javax.net.ssl.SSLSession, jdk.internal.net.http.BufferingSubscriber, .LimitingSubscriber, .LineSubscriberAdapter, .ResponseBodyHandlers.FileDownloadBodyHandler, .ResponseBodyHandlers.PathBodyHandler, .ResponseBodyHandlers.PushPromisesHandlerWithMap, .ResponseSubscribers, .ResponseSubscribers.PathSubscriber

An HTTP response.

An HttpResponse is not created directly, but rather returned as a result of sending an HttpRequest. An HttpResponse is made available when the response status code and headers have been received, and typically after the response body has also been completely received. Whether or not the HttpResponse is made available before the response body has been completely received depends on the BodyHandler provided when sending the HttpRequest.

This class provides methods for accessing the response status code, headers, the response body, and the HttpRequest corresponding to this response.

The following is an example of retrieving a response as a String:

HttpResponse<String> response = client .send(request, BodyHandlers.ofString());
HttpResponse<String> response = client
  .send(request, BodyHandlers.ofString());

The class BodyHandlers provides implementations of many common response handlers. Alternatively, a custom BodyHandler implementation can be used.

Since
11

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface
HttpResponse.BodyHandler<
the response body type
T
>

A handler for response bodies.

public static class
HttpResponse.BodyHandlers

Implementations of BodyHandler that implement various useful handlers, such as handling the response body as a String, or streaming the response body to a file.

public static interface
HttpResponse.BodySubscriber<
the response body type
T
>

A BodySubscriber consumes response body bytes and converts them into a higher-level Java type.

public static class
HttpResponse.BodySubscribers

Implementations of BodySubscriber that implement various useful subscribers, such as converting the response body bytes into a String, or streaming the bytes to a file.

public static interface
HttpResponse.PushPromiseHandler<
the push promise response body type
T
>

A handler for push promises.

public static interface
HttpResponse.ResponseInfo

Initial response information supplied to a BodyHandler when a response is initially received and before the body is processed.

Method Summary

Modifier and TypeMethod and Description
public T

Returns:

the body
body
()

Returns the body.

public default Optional<String>

Returns:

if present, a label identifying the connection on which the response was received
connectionLabel
()

Returns if present, a label identifying the connection on which the response was received.

public HttpHeaders

Returns:

the response headers
headers
()

Returns the received response headers.

public Optional<HttpResponse<T>>

Returns:

an Optional containing the HttpResponse, if any.
previousResponse
()

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

public HttpRequest

Returns:

the request
request
()

Returns the HttpRequest corresponding to this response.

public Optional<SSLSession>

Returns:

an Optional containing the SSLSession associated with the response
sslSession
()

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

public int

Returns:

the response code
statusCode
()

Returns the status code for this response.

public URI

Returns:

the URI of the response
uri
()

Returns the URI that the response was received from.

public HttpClient.Version

Returns:

HTTP protocol version
version
()

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