-
- Type Parameters:
T- the response body type
public interface HttpResponse<T>An HTTP response.An
HttpResponseis not created directly, but rather returned as a result of sending anHttpRequest. AnHttpResponseis 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 theHttpResponseis made available before the response body has been completely received depends on theBodyHandlerprovided when sending theHttpRequest.This class provides methods for accessing the response status code, headers, the response body, and the
HttpRequestcorresponding 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
BodyHandlersprovides implementations of many common response handlers. Alternatively, a customBodyHandlerimplementation can be used.- Since:
- 11
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static interfaceHttpResponse.BodyHandler<T>A handler for response bodies.static classHttpResponse.BodyHandlersImplementations ofBodyHandlerthat implement various useful handlers, such as handling the response body as a String, or streaming the response body to a file.static interfaceHttpResponse.BodySubscriber<T>ABodySubscriberconsumes response body bytes and converts them into a higher-level Java type.static classHttpResponse.BodySubscribersImplementations ofBodySubscriberthat implement various useful subscribers, such as converting the response body bytes into a String, or streaming the bytes to a file.static interfaceHttpResponse.PushPromiseHandler<T>A handler for push promises.static interfaceHttpResponse.ResponseInfoInitial response information supplied to aBodyHandlerwhen a response is initially received and before the body is processed.
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description Tbody()Returns the body.HttpHeadersheaders()Returns the received response headers.Optional<HttpResponse<T>>previousResponse()Returns anOptionalcontaining the previous intermediate response if one was received.HttpRequestrequest()Returns theHttpRequestcorresponding to this response.Optional<SSLSession>sslSession()Returns anOptionalcontaining theSSLSessionin effect for this response.intstatusCode()Returns the status code for this response.URIuri()Returns theURIthat the response was received from.HttpClient.Versionversion()Returns the HTTP protocol version that was used for this response.
-
-
-
Method Detail
-
statusCode
int statusCode()
Returns the status code for this response.- Returns:
- the response code
-
request
HttpRequest request()
Returns theHttpRequestcorresponding to this response.The returned
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.- Returns:
- the request
- See Also:
previousResponse()
-
previousResponse
Optional<HttpResponse<T>> previousResponse()
Returns anOptionalcontaining 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 emptyOptionalis returned.- Returns:
- an Optional containing the HttpResponse, if any.
-
headers
HttpHeaders headers()
Returns the received response headers.- Returns:
- the response headers
-
body
T body()
Returns the body. Depending on the type ofT, the returned body may represent the body after it was read (such asbyte[], orString, orPath) or it may represent an object with which the body is read, such as anInputStream.If this
HttpResponsewas returned from an invocation ofpreviousResponse()then this method returnsnull- Returns:
- the body
-
sslSession
Optional<SSLSession> sslSession()
Returns anOptionalcontaining theSSLSessionin effect for this response. Returns an emptyOptionalif this is not a HTTPS response.- Returns:
- an
Optionalcontaining theSSLSessionassociated with the response
-
uri
URI uri()
Returns theURIthat the response was received from. This may be different from the requestURIif redirection occurred.- Returns:
- the URI of the response
-
version
HttpClient.Version version()
Returns the HTTP protocol version that was used for this response.- Returns:
- HTTP protocol version
-
-