添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
extends Object implements Closeable
An HTTP response. Instances of this class are not immutable: the response body is a one-shot value that may be consumed only once and then closed. All other properties are immutable.

This class implements Closeable . Closing it simply closes its response body. See ResponseBody for an explanation and examples.

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.
List < Challenge > challenges ()
Returns the RFC 7235 authorization challenges appropriate for this response's code.
close ()
Closes the response body.
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.
String header ( String name) String header ( String name, String defaultValue) Headers headers () List < String > headers ( 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.
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 .
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.
sentRequestAtMillis ()
Returns a timestamp taken immediately before OkHttp transmitted the initiating request over the network.
String toString () Headers trailers ()
Returns the trailers after the HTTP response, which may be empty.

request

public Request request()
The wire-level request that initiated this HTTP response. This is not necessarily the same request issued by the application:
  • It may be transformed by the HTTP client. For example, the client may copy headers like Content-Length from the request body.
  • 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()
    Returns a non-null value if this response was passed to 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() .

    @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
  •