Class Response

java.lang.Object
org.miaixz.bus.http.Response
All Implemented Interfaces:
Closeable, AutoCloseable

public class Response extends Object implements Closeable
HTTP响应。该类的实例不是不可变的: 响应体是一次性的值,可能只使用一次,然后关闭。所有其他属性都是不可变的.
Since:
Java 17+
Author:
Kimi Liu
  • Method Details

    • 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.
    • protocol

      public Protocol protocol()
      Returns the HTTP protocol, such as Protocol.HTTP_1_1 or Protocol.HTTP_1_0.
    • code

      public int code()
      Returns the HTTP status code.
    • isSuccessful

      public boolean isSuccessful()
      Returns true if the code is in [200..300), which means the request was successfully received, understood, and accepted.
    • message

      public String message()
      Returns the HTTP status message.
    • handshake

      public Handshake handshake()
      Returns the TLS handshake of the connection that carried this response, or null if the response was received without TLS.
    • headers

      public List<String> headers(String name)
    • header

      public String header(String name)
    • header

      public String header(String name, String defaultValue)
    • headers

      public Headers headers()
    • trailers

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

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

      public ResponseBody body()
      Returns a non-null value if this response was passed to Callback.onResponse(org.miaixz.bus.http.NewCall, org.miaixz.bus.http.Response) or returned from NewCall.execute(). Response bodies must be closed and may be consumed only once.

      This always returns null on responses returned from cacheResponse, networkResponse, and priorResponse().

    • newBuilder

      public Response.Builder newBuilder()
    • isRedirect

      public boolean isRedirect()
      Returns true if this response redirects to another resource.
    • networkResponse

      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.
    • cacheResponse

      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.
    • priorResponse

      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 Http 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 Http 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.
    • close

      public void close()
      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 AutoCloseable
      Specified by:
      close in interface Closeable
    • toString

      public String toString()
      Overrides:
      toString in class Object