- Type Parameters:
T- the response body type
HttpResponse is not created directly, but rather returned as
a result of sending an
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
This class provides methods for accessing the response status code,
headers, the response body, and the
to this response.
The following is an example of retrieving a response as a String:
HttpResponse<String> response = client .send(request, BodyHandlers.ofString());
BodyHandlers provides implementations
of many common response handlers. Alternatively, a custom
implementation can be used.
Nested Class SummaryModifier and TypeInterfaceDescription
static interfaceA handler for response bodies.
static classImplementations of
BodyHandlerthat implement various useful handlers, such as handling the response body as a String, or streaming the response body to a file.
BodySubscriberconsumes response body bytes and converts them into a higher-level Java type.
static classImplementations of
BodySubscriberthat implement various useful subscribers, such as converting the response body bytes into a String, or streaming the bytes to a file.
static interfaceA handler for push promises.
static interfaceInitial response information supplied to a
BodyHandlerwhen a response is initially received and before the body is processed.
Method SummaryModifier and TypeMethodDescription
body()Returns the body.
headers()Returns the received response headers.Returns an
Optionalcontaining the previous intermediate response if one was received.
HttpRequestcorresponding to this response.Returns an
SSLSessionin effect for this response.
intReturns the status code for this response.
URIthat the response was received from.
version()Returns the HTTP protocol version that was used for this response.
statusCodeint statusCode()Returns the status code for this response.
- the response code
requestHttpRequest request()Returns the
HttpRequestcorresponding to this response.
HttpRequestmay 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.
- the request
- See Also:
previousResponseOptional<HttpResponse<T>> previousResponse()Returns an
Optionalcontaining 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
- an Optional containing the HttpResponse, if any.
headersHttpHeaders headers()Returns the received response headers.
- the response headers
bodyT body()Returns the body. Depending on the type of
T, the returned body may represent the body after it was read (such as
Path) or it may represent an object with which the body is read, such as an
HttpResponsewas returned from an invocation of
previousResponse()then this method returns
- the body
sslSessionOptional<SSLSession> sslSession()Returns an
SSLSessionin effect for this response. Returns an empty
Optionalif this is not a HTTPS response.
SSLSessionassociated with the response
uriURI uri()Returns the
URIthat the response was received from. This may be different from the request
URIif redirection occurred.
- the URI of the response
versionHttpClient.Version version()Returns the HTTP protocol version that was used for this response.
- HTTP protocol version