T
- the response body typepublic interface HttpResponse<T>
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());
The class BodyHandlers
provides implementations of many common response handlers. Alternatively, a custom BodyHandler
implementation can be used.
Modifier and Type | Interface | Description |
---|---|---|
static interface |
HttpResponse.BodyHandler<T> |
A handler for response bodies. |
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. |
static interface |
HttpResponse.BodySubscriber<T> |
A BodySubscriber consumes response body bytes and converts them into a higher-level Java type. |
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. |
static interface |
HttpResponse.PushPromiseHandler<T> |
A handler for push promises. |
static interface |
HttpResponse.ResponseInfo |
Initial response information supplied to a BodyHandler when a response is initially received and before the body is processed. |
Modifier and Type | Method | Description |
---|---|---|
T |
body() |
Returns the body. |
HttpHeaders |
headers() |
Returns the received response headers. |
Optional |
previousResponse() |
Returns an Optional containing the previous intermediate response if one was received. |
HttpRequest |
request() |
Returns the HttpRequest corresponding to this response. |
Optional |
sslSession() |
Returns an Optional containing the SSLSession in effect for this response. |
int |
statusCode() |
Returns the status code for this response. |
URI |
uri() |
Returns the URI that the response was received from. |
HttpClient.Version |
version() |
Returns the HTTP protocol version that was used for this response. |
int statusCode()
HttpRequest request()
HttpRequest
corresponding to this response. The returned HttpRequest
may not be the initiating request provided when sending. For example, if the initiating request was redirected, then the request returned by this method will have the redirected URI, which will be different from the initiating request URI.
Optional<HttpResponse<T>> previousResponse()
Optional
containing the previous intermediate response if one was received. An intermediate response is one that is received as a result of redirection or authentication. If no previous response was received then an empty Optional
is returned.HttpHeaders headers()
T body()
T
, the returned body may represent the body after it was read (such as byte[]
, or String
, or Path
) or it may represent an object with which the body is read, such as an InputStream
. If this HttpResponse
was returned from an invocation of previousResponse()
then this method returns null
Optional<SSLSession> sslSession()
Optional
containing the SSLSession
in effect for this response. Returns an empty Optional
if this is not a HTTPS response.Optional
containing the SSLSession
associated with the responseURI uri()
URI
that the response was received from. This may be different from the request URI
if redirection occurred.HttpClient.Version version()
© 1993, 2023, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/21/docs/api/java.net.http/java/net/http/HttpResponse.html