java.lang.Object
org.miaixz.bus.http.Headers
Header fields of an HTTP message.
Maintains the order of header fields. Values are stored as uninterpreted strings, with leading and trailing
whitespace removed. Instances are immutable. It is recommended to interpret header fields through Request and
Response.
- Since:
- Java 17+
- Author:
- Kimi Liu
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionlongGets the encoded byte count of the headers.static longcontentLength(Headers headers) Gets the Content-Length from headers.static longcontentLength(Response response) Gets the Content-Length of a response.booleanCompares two Headers objects for equality.Gets the last value for the given header name.Gets the date value for the given header name.getInstant(String name) Gets the Instant value for the given header name.static booleanChecks if the response includes a body.inthashCode()Calculates the hash code.static booleanhasVaryAll(Headers responseHeaders) Checks for the presence of a Vary: * header.static booleanhasVaryAll(Response response) Checks for the presence of a Vary: * header.name(int index) Gets the header name at the specified index.names()Gets the set of header names.Creates a new Builder instance.static HeadersCreates a Headers instance from an array of names and values.static HeadersCreates a Headers instance from a map.parseChallenges(Headers responseHeaders, String headerName) Parses RFC 7235 challenges.static intparseSeconds(String value, int defaultValue) Parses seconds from a string.static voidreceiveHeaders(CookieJar cookieJar, UnoUrl url, Headers headers) Handles received Cookie headers.intsize()Gets the number of headers.static intSkips until a character from the given set is found.static intskipWhitespace(String input, int pos) Skips whitespace characters.Converts to a multi-valued map.toString()Returns the string representation of the headers.value(int index) Gets the header value at the specified index.Gets the list of values for the given header name.varyFields(Headers responseHeaders) Gets the set of Vary fields.static HeadersvaryHeaders(Headers requestHeaders, Headers responseHeaders) Gets the request headers that affect the response body.static HeadersvaryHeaders(Response response) Gets the request headers that affect the response body.static booleanvaryMatches(Response cachedResponse, Headers cachedRequest, Request newRequest) Checks if the Vary header matches.
-
Method Details
-
of
Creates a Headers instance from an array of names and values.Requires an even number of arguments, alternating between names and values.
- Parameters:
namesAndValues- The array of names and values.- Returns:
- A Headers instance.
- Throws:
NullPointerException- if namesAndValues is null.IllegalArgumentException- if the number of arguments is odd or contains nulls.
-
of
Creates a Headers instance from a map.- Parameters:
headers- The map of header names and values.- Returns:
- A Headers instance.
- Throws:
NullPointerException- if headers is null.IllegalArgumentException- if a name or value is null.
-
contentLength
Gets the Content-Length of a response.- Parameters:
response- The response.- Returns:
- The Content-Length value, or -1 if invalid.
-
contentLength
Gets the Content-Length from headers.- Parameters:
headers- The headers.- Returns:
- The Content-Length value, or -1 if invalid.
-
varyMatches
public static boolean varyMatches(Response cachedResponse, Headers cachedRequest, Request newRequest) Checks if the Vary header matches.- Parameters:
cachedResponse- The cached response.cachedRequest- The cached request.newRequest- The new request.- Returns:
- true if the Vary header matches.
-
hasVaryAll
Checks for the presence of a Vary: * header.- Parameters:
response- The response.- Returns:
- true if Vary: * is present.
-
hasVaryAll
Checks for the presence of a Vary: * header.- Parameters:
responseHeaders- The response headers.- Returns:
- true if Vary: * is present.
-
varyFields
Gets the set of Vary fields.- Parameters:
responseHeaders- The response headers.- Returns:
- A set of Vary fields.
-
varyHeaders
Gets the request headers that affect the response body.- Parameters:
response- The response.- Returns:
- The headers that affect the response body.
-
varyHeaders
Gets the request headers that affect the response body.- Parameters:
requestHeaders- The request headers.responseHeaders- The response headers.- Returns:
- The headers that affect the response body.
-
parseChallenges
Parses RFC 7235 challenges.- Parameters:
responseHeaders- The response headers.headerName- The header name.- Returns:
- A list of authentication challenges.
-
receiveHeaders
Handles received Cookie headers.- Parameters:
cookieJar- The cookie manager.url- The URL.headers- The headers.
-
hasBody
Checks if the response includes a body.- Parameters:
response- The response.- Returns:
- true if the response includes a body.
-
skipUntil
Skips until a character from the given set is found.- Parameters:
input- The input string.pos- The starting position.characters- The set of target characters.- Returns:
- The position of the target character.
-
skipWhitespace
Skips whitespace characters.- Parameters:
input- The input string.pos- The starting position.- Returns:
- The position of the first non-whitespace character.
-
parseSeconds
Parses seconds from a string.- Parameters:
value- The string value.defaultValue- The default value.- Returns:
- The number of seconds.
-
newBuilder
Creates a new Builder instance.- Returns:
- A Builder instance.
-
hashCode
public int hashCode()Calculates the hash code. -
get
Gets the last value for the given header name.- Parameters:
name- The header name.- Returns:
- The header value, or null if it does not exist.
-
getDate
Gets the date value for the given header name.- Parameters:
name- The header name.- Returns:
- The date value, or null if invalid.
-
getInstant
Gets the Instant value for the given header name.- Parameters:
name- The header name.- Returns:
- The Instant value, or null if invalid.
-
size
public int size()Gets the number of headers.- Returns:
- The number of headers.
-
name
Gets the header name at the specified index.- Parameters:
index- The index.- Returns:
- The header name.
-
value
Gets the header value at the specified index.- Parameters:
index- The index.- Returns:
- The header value.
-
names
Gets the set of header names.- Returns:
- An unmodifiable set of header names.
-
values
Gets the list of values for the given header name.- Parameters:
name- The header name.- Returns:
- An unmodifiable list of header values.
-
byteCount
public long byteCount()Gets the encoded byte count of the headers.- Returns:
- The encoded byte count.
-
equals
Compares two Headers objects for equality. -
toString
Returns the string representation of the headers. -
toMultimap
Converts to a multi-valued map.- Returns:
- A map of header names to lists of values.
-