public final class Response
extends java.lang.Object
implements java.io.Closeable
This class implements Closeable
. Closing it simply closes its response body. See
ResponseBody
for an explanation and examples.
Modifier and Type | Class and Description |
---|---|
static class |
Response.Builder |
Modifier and Type | Field and Description |
---|---|
(package private) ResponseBody |
body |
private CacheControl |
cacheControl |
(package private) Response |
cacheResponse |
(package private) int |
code |
(package private) Exchange |
exchange |
(package private) Handshake |
handshake |
(package private) Headers |
headers |
(package private) java.lang.String |
message |
(package private) Response |
networkResponse |
(package private) Response |
priorResponse |
(package private) Protocol |
protocol |
(package private) long |
receivedResponseAtMillis |
(package private) Request |
request |
(package private) long |
sentRequestAtMillis |
Constructor and Description |
---|
Response(Response.Builder builder) |
Modifier and Type | Method and Description |
---|---|
ResponseBody |
body()
Returns a non-null value if this response was passed to
Callback.onResponse(okhttp3.Call, okhttp3.Response) or returned
from Call.execute() . |
CacheControl |
cacheControl()
Returns the cache control directives for this response.
|
Response |
cacheResponse()
Returns the raw response received from the cache.
|
java.util.List<Challenge> |
challenges()
Returns the RFC 7235 authorization challenges appropriate for this response's code.
|
void |
close()
Closes the response body.
|
int |
code()
Returns the HTTP status code.
|
Handshake |
handshake()
Returns the TLS handshake of the connection that carried this response, or null if the response
was received without TLS.
|
java.lang.String |
header(java.lang.String name) |
java.lang.String |
header(java.lang.String name,
java.lang.String defaultValue) |
Headers |
headers() |
java.util.List<java.lang.String> |
headers(java.lang.String name) |
boolean |
isRedirect()
Returns true if this response redirects to another resource.
|
boolean |
isSuccessful()
Returns true if the code is in [200..300), which means the request was successfully received,
understood, and accepted.
|
java.lang.String |
message()
Returns the HTTP status message.
|
Response |
networkResponse()
Returns the raw response received from the network.
|
Response.Builder |
newBuilder() |
ResponseBody |
peekBody(long byteCount)
Peeks up to
byteCount bytes from the response body and returns them as a new response
body. |
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.
|
Protocol |
protocol()
Returns the HTTP protocol, such as
Protocol.HTTP_1_1 or Protocol.HTTP_1_0 . |
long |
receivedResponseAtMillis()
Returns a timestamp taken immediately after OkHttp
received this response's headers from the network.
|
Request |
request()
The wire-level request that initiated this HTTP response.
|
long |
sentRequestAtMillis()
Returns a timestamp taken immediately before OkHttp
transmitted the initiating request over the network.
|
java.lang.String |
toString() |
Headers |
trailers()
Returns the trailers after the HTTP response, which may be empty.
|
final Request request
final Protocol protocol
final int code
final java.lang.String message
@Nullable final Handshake handshake
final Headers headers
@Nullable final ResponseBody body
@Nullable final Response networkResponse
@Nullable final Response cacheResponse
@Nullable final Response priorResponse
final long sentRequestAtMillis
final long receivedResponseAtMillis
@Nullable final Exchange exchange
@Nullable private volatile CacheControl cacheControl
Response(Response.Builder builder)
public Request request()
Content-Length
from the request body.
public Protocol protocol()
Protocol.HTTP_1_1
or Protocol.HTTP_1_0
.public int code()
public boolean isSuccessful()
public java.lang.String message()
@Nullable public Handshake handshake()
public java.util.List<java.lang.String> headers(java.lang.String name)
@Nullable public java.lang.String header(java.lang.String name)
@Nullable public java.lang.String header(java.lang.String name, @Nullable java.lang.String defaultValue)
public Headers headers()
public Headers trailers() throws java.io.IOException
java.io.IOException
public ResponseBody peekBody(long byteCount) throws java.io.IOException
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.
java.io.IOException
@Nullable public ResponseBody body()
Callback.onResponse(okhttp3.Call, okhttp3.Response)
or returned
from Call.execute()
. Response bodies must be closed and may
be consumed only once.
This always returns null on responses returned from cacheResponse
, networkResponse
, and priorResponse()
.
public Response.Builder newBuilder()
public boolean isRedirect()
@Nullable public Response networkResponse()
@Nullable public Response cacheResponse()
@Nullable public Response priorResponse()
public java.util.List<Challenge> 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.
public CacheControl cacheControl()
Cache-Control
header.public long sentRequestAtMillis()
public long receivedResponseAtMillis()
public void close()
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()
.
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
public java.lang.String toString()
toString
in class java.lang.Object