- All Implemented Interfaces:
Closeable,AutoCloseable
Note: All properties of this class are immutable except for the response body. The response body is a one-shot resource that must be closed after reading and can only be consumed once.
- Since:
- Java 17+
- Author:
- Kimi Liu
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classA builder for creating and modifyingResponseinstances. -
Method Summary
Modifier and TypeMethodDescriptionbody()Returns the response body.Returns the cache control directives.Returns the cache response.Returns a list of RFC 7235 authentication challenges.voidclose()Closes the response body.intcode()Returns the HTTP status code.Returns the TLS handshake information.Returns the first header value for the given name.Returns the first header value for the given name, with a default value.headers()Returns all response headers.Returns a list of header values for the given name.booleanReturns whether this response is a redirect.booleanReturns whether the request was successful (status code in the range [200..300)).message()Returns the HTTP status message.Returns the network response.Creates a new builder instance initialized with this response's properties.peekBody(long byteCount) Peeks at the response body content, up to a specified number of bytes.Returns the prior response.org.miaixz.bus.core.net.Protocolprotocol()Returns the HTTP protocol that was used.longReturns the timestamp when the response headers were received.request()Returns the original request that initiated this response.longReturns the timestamp when the request was sent.toString()Returns a string representation of this response.trailers()Returns the trailer headers of the response.
-
Method Details
-
request
Returns the original request that initiated this response.Note: This request may differ from the one issued by the application if it was modified by the HTTP client (e.g., by adding a Content-Length header) or if a new request was generated due to a redirect or authentication (the URL may be different).
- Returns:
- The
Requestobject that initiated the response.
-
protocol
public org.miaixz.bus.core.net.Protocol protocol()Returns the HTTP protocol that was used.- Returns:
- The protocol, such as HTTP/1.1 or HTTP/2.
-
code
public int code()Returns the HTTP status code.- Returns:
- The status code (e.g., 200, 404).
-
isSuccessful
public boolean isSuccessful()Returns whether the request was successful (status code in the range [200..300)).- Returns:
trueif the request was successfully received, understood, and accepted.
-
message
Returns the HTTP status message.- Returns:
- The status message (e.g., "OK", "Not Found").
-
handshake
Returns the TLS handshake information.- Returns:
- The
Handshakeobject, or null for non-TLS connections.
-
headers
Returns a list of header values for the given name.- Parameters:
name- The header name.- Returns:
- A list of header values, which may be empty.
-
header
Returns the first header value for the given name.- Parameters:
name- The header name.- Returns:
- The header value, or null if not found.
-
header
Returns the first header value for the given name, with a default value.- Parameters:
name- The header name.defaultValue- The default value to return if the header is not found.- Returns:
- The header value or the default value.
-
headers
Returns all response headers.- Returns:
- The
Headersobject.
-
trailers
Returns the trailer headers of the response.Note: This must be called after the response body has been fully consumed, otherwise an
IllegalStateExceptionwill be thrown.- Returns:
- The trailer
Headersobject, which may be empty. - Throws:
IOException- if the trailers could not be retrieved.IllegalStateException- if the response body has not been fully consumed.
-
peekBody
Peeks at the response body content, up to a specified number of bytes.Returns a new
ResponseBodycontaining up tobyteCountbytes of the response body. If the body is smaller thanbyteCount, the entire content is returned; otherwise, it is truncated.Note: This method loads the requested bytes into memory, so it is recommended to set a reasonable
byteCount(e.g., 1MB). Calling this after the response body has been consumed will result in an error.- Parameters:
byteCount- The maximum number of bytes to peek.- Returns:
- A new
ResponseBodyobject. - Throws:
IOException- if reading the response body fails.
-
body
Returns the response body.Note: The response body is a one-shot resource that must be closed and can only be consumed once. For responses returned by
cacheResponse,networkResponse, andpriorResponse, this method returns null.- Returns:
- The
ResponseBodyobject, which may be null.
-
newBuilder
Creates a new builder instance initialized with this response's properties.- Returns:
- A new
Response.Builderinstance.
-
isRedirect
public boolean isRedirect()Returns whether this response is a redirect.- Returns:
trueif the status code indicates a redirect (300, 301, 302, 303, 307, 308).
-
networkResponse
Returns the network response.If the response was served directly from the network (not from cache), this returns the original response; otherwise, it returns null. The body of the returned response should not be read.
- Returns:
- The network
Responseobject, which may be null.
-
cacheResponse
Returns the cache response.If the response was served from the cache, this returns the cached response; otherwise, it returns null. For conditional GET requests, both a cache and network response may be present. The body of the returned response should not be read.
- Returns:
- The cached
Responseobject, which may be null.
-
priorResponse
Returns the prior response.If this response was triggered by a redirect or authentication challenge, this returns the prior response; otherwise, it returns null. The body of the returned response should not be read (it has already been consumed).
- Returns:
- The prior
Responseobject, which may be null.
-
challenges
Returns a list of RFC 7235 authentication challenges.For a 401 (Unauthorized) status code, this returns "WWW-Authenticate" challenges. For a 407 (Proxy Authentication Required) status code, it returns "Proxy-Authenticate" challenges. Other status codes return an empty list.
- Returns:
- A list of authentication challenges, which may be empty.
-
cacheControl
Returns the cache control directives.This returns a non-null
CacheControlobject even if the response does not have a "Cache-Control" header. Lazily initialized for performance.- Returns:
- The
CacheControlobject.
-
sentRequestAtMillis
public long sentRequestAtMillis()Returns the timestamp when the request was sent.- Returns:
- The timestamp in milliseconds (from System.currentTimeMillis()).
-
receivedResponseAtMillis
public long receivedResponseAtMillis()Returns the timestamp when the response headers were received.- Returns:
- The timestamp in milliseconds (from System.currentTimeMillis()).
-
close
public void close()Closes the response body.This is equivalent to calling
body().close(). For responses without a body (like those fromcacheResponse,networkResponse, orpriorResponse), this will throw an exception.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IllegalStateException- if the response does not have a body.
-
toString
Returns a string representation of this response.
-