The wire-level request that initiated this HTTP response. This is not necessarily the same
request issued by the application:
It may be the request generated in response to an HTTP redirect or authentication
challenge. In this case the request URL may be different than the initial request URL.
isSuccessful
public boolean isSuccessful()
Returns true if the code is in [200..300), which means the request was successfully received,
understood, and accepted.
@Nullable
public Handshake handshake()
Returns the TLS handshake of the connection that carried this response, or null if the response
was received without TLS.
public Headers trailers()
throws IOException
Returns the trailers after the HTTP response, which may be empty. It is an error to call this
before the entire HTTP response body has been consumed.
Throws:
IOException
public ResponseBody peekBody(long byteCount)
throws IOException
Peeks up to
byteCount
bytes from the response body and returns them as a new response
body. If fewer than
byteCount
bytes are in the response body, the full response body is
returned. If more than
byteCount
bytes are in the response body, the returned value
will be truncated to
byteCount
bytes.
It is an error to call this method after the body has been consumed.
Warning:
this method loads the requested bytes into memory. Most
applications should set a modest limit on
byteCount
, such as 1 MiB.
Throws:
IOException
@Nullable
public ResponseBody body()
@Nullable
public Response networkResponse()
Returns the raw response received from the network. Will be null if this response didn't use
the network, such as when the response is fully cached. The body of the returned response
should not be read.
@Nullable
public Response cacheResponse()
Returns the raw response received from the cache. Will be null if this response didn't use the
cache. For conditional get requests the cache response and network response may both be
non-null. The body of the returned response should not be read.
@Nullable
public Response priorResponse()
Returns the response for the HTTP redirect or authorization challenge that triggered this
response, or null if this response wasn't triggered by an automatic retry. The body of the
returned response should not be read because it has already been consumed by the redirecting
client.
challenges
public List<Challenge> challenges()
Returns the RFC 7235 authorization challenges appropriate for this response's code. If the
response code is 401 unauthorized, this returns the "WWW-Authenticate" challenges. If the
response code is 407 proxy unauthorized, this returns the "Proxy-Authenticate" challenges.
Otherwise this returns an empty list of challenges.
If a challenge uses the
token68
variant instead of auth params, there is exactly one
auth param in the challenge at key
null
. Invalid headers and challenges are ignored.
No semantic validation is done, for example that
Basic
auth must have a
realm
auth param, this is up to the caller that interprets these challenges.
cacheControl
public CacheControl cacheControl()
Returns the cache control directives for this response. This is never null, even if this
response contains no
Cache-Control
header.
sentRequestAtMillis
public long sentRequestAtMillis()
Returns a
timestamp
taken immediately before OkHttp
transmitted the initiating request over the network. If this response is being served from the
cache then this is the timestamp of the original request.
receivedResponseAtMillis
public long receivedResponseAtMillis()
Returns a
timestamp
taken immediately after OkHttp
received this response's headers from the network. If this response is being served from the
cache then this is the timestamp of the original response.
Closes the response body. Equivalent to
body().close()
.
It is an error to close a response that is not eligible for a body. This includes the
responses returned from
cacheResponse
,
networkResponse
, and
priorResponse()
.
Specified by:
close
in interface
Closeable
Specified by:
close
in interface
AutoCloseable