Class HttpHeaders
java.lang.Object
cloud.opencode.base.web.http.HttpHeaders
HTTP Headers - HTTP Request/Response Headers Container
HTTP 头部 - HTTP 请求/响应头部容器
This class provides a case-insensitive header container with support for multiple values per header name.
此类提供不区分大小写的头部容器,支持每个头部名称有多个值。
Example | 示例:
HttpHeaders headers = HttpHeaders.of()
.add("Content-Type", "application/json")
.add("Accept", "application/json")
.add("X-Custom", "value1")
.add("X-Custom", "value2");
String contentType = headers.get("content-type"); // Case-insensitive
List<String> custom = headers.getAll("X-Custom");
Features | 主要功能:
- Case-insensitive header storage - 不区分大小写的头部存储
- Multi-value header support - 多值头部支持
- Fluent API for header manipulation - 流式头部操作API
- Standard header name constants - 标准头部名称常量
Usage Examples | 使用示例:
HttpHeaders headers = HttpHeaders.of()
.contentType("application/json")
.bearerAuth("token123");
String ct = headers.get("content-type");
Security | 安全性:
- Thread-safe: No - 否
- Null-safe: No (header name should not be null) - 否(头部名称不应为null)
- Since:
- JDK 25, opencode-base-web V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final String -
Method Summary
Modifier and TypeMethodDescriptionSets Accept header.Adds a header value.Sets Authorization header with Basic auth.bearerAuth(String token) Sets Authorization header with Bearer token.clear()Clears all headers.booleanChecks if a header exists.contentType(ContentType contentType) Sets Content-Type header.contentType(String contentType) Sets Content-Type header.static HttpHeaderscopyOf(HttpHeaders source) Creates a copy of the headers.static HttpHeadersfrom(HttpHeaders jdkHeaders) Creates headers from JDK HttpHeaders.Gets the first value for a header.Gets all values for a header.longGets Content-Length header.Gets Content-Type header.getOrDefault(String name, String defaultValue) Gets the first value for a header with default.booleanisEmpty()Checks if headers are empty.iterator()names()Gets all header names.static HttpHeadersof()Creates empty headers.static HttpHeadersCreates headers from a map.Removes a header.Sets a header value (replaces existing).Sets multiple values for a header.intsize()Gets the number of headers.toMap()Converts to unmodifiable map.Converts to single-value map.toString()Sets User-Agent header.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface Iterable
forEach, spliterator
-
Field Details
-
ACCEPT
- See Also:
-
ACCEPT_CHARSET
- See Also:
-
ACCEPT_ENCODING
- See Also:
-
ACCEPT_LANGUAGE
- See Also:
-
AUTHORIZATION
- See Also:
-
CACHE_CONTROL
- See Also:
-
CONNECTION
- See Also:
-
CONTENT_DISPOSITION
- See Also:
-
CONTENT_ENCODING
- See Also:
-
CONTENT_LENGTH
- See Also:
-
CONTENT_TYPE
- See Also:
-
COOKIE
- See Also:
-
DATE
- See Also:
-
ETAG
- See Also:
-
EXPIRES
- See Also:
-
HOST
- See Also:
-
IF_MATCH
- See Also:
-
IF_MODIFIED_SINCE
- See Also:
-
IF_NONE_MATCH
- See Also:
-
LAST_MODIFIED
- See Also:
-
LOCATION
- See Also:
-
ORIGIN
- See Also:
-
PRAGMA
- See Also:
-
RANGE
- See Also:
-
REFERER
- See Also:
-
RETRY_AFTER
- See Also:
-
SERVER
- See Also:
-
SET_COOKIE
- See Also:
-
TRANSFER_ENCODING
- See Also:
-
USER_AGENT
- See Also:
-
WWW_AUTHENTICATE
- See Also:
-
X_FORWARDED_FOR
- See Also:
-
X_REQUESTED_WITH
- See Also:
-
-
Method Details
-
of
Creates empty headers. 创建空头部。- Returns:
- empty HttpHeaders - 空的 HttpHeaders
-
of
Creates headers from a map. 从 Map 创建头部。- Parameters:
map- the headers map - 头部 Map- Returns:
- HttpHeaders - HttpHeaders
-
from
Creates headers from JDK HttpHeaders. 从 JDK HttpHeaders 创建头部。- Parameters:
jdkHeaders- the JDK headers - JDK 头部- Returns:
- HttpHeaders - HttpHeaders
-
copyOf
Creates a copy of the headers. 创建头部的副本。- Parameters:
source- the source headers - 源头部- Returns:
- HttpHeaders copy - HttpHeaders 副本
-
add
Adds a header value. 添加头部值。- Parameters:
name- the header name - 头部名称value- the header value - 头部值- Returns:
- this for chaining - 用于链式调用
-
set
Sets a header value (replaces existing). 设置头部值(替换现有值)。- Parameters:
name- the header name - 头部名称value- the header value - 头部值- Returns:
- this for chaining - 用于链式调用
-
set
Sets multiple values for a header. 为头部设置多个值。- Parameters:
name- the header name - 头部名称values- the header values - 头部值列表- Returns:
- this for chaining - 用于链式调用
-
remove
Removes a header. 移除头部。- Parameters:
name- the header name - 头部名称- Returns:
- this for chaining - 用于链式调用
-
clear
-
get
-
getOrDefault
-
getAll
-
contains
Checks if a header exists. 检查头部是否存在。- Parameters:
name- the header name - 头部名称- Returns:
- true if exists - 如果存在返回 true
-
size
public int size()Gets the number of headers. 获取头部数量。- Returns:
- the count - 数量
-
isEmpty
public boolean isEmpty()Checks if headers are empty. 检查头部是否为空。- Returns:
- true if empty - 如果为空返回 true
-
names
-
toMap
-
toSingleValueMap
-
getContentType
Gets Content-Type header. 获取 Content-Type 头部。- Returns:
- the content type or null - 内容类型或 null
-
getContentLength
public long getContentLength()Gets Content-Length header. 获取 Content-Length 头部。- Returns:
- the content length or -1 - 内容长度或 -1
-
contentType
Sets Content-Type header. 设置 Content-Type 头部。- Parameters:
contentType- the content type - 内容类型- Returns:
- this for chaining - 用于链式调用
-
contentType
Sets Content-Type header. 设置 Content-Type 头部。- Parameters:
contentType- the content type - 内容类型- Returns:
- this for chaining - 用于链式调用
-
accept
Sets Accept header. 设置 Accept 头部。- Parameters:
accept- the accept value - Accept 值- Returns:
- this for chaining - 用于链式调用
-
bearerAuth
Sets Authorization header with Bearer token. 使用 Bearer token 设置 Authorization 头部。- Parameters:
token- the bearer token - Bearer token- Returns:
- this for chaining - 用于链式调用
-
basicAuth
Sets Authorization header with Basic auth. 使用 Basic auth 设置 Authorization 头部。- Parameters:
username- the username - 用户名password- the password - 密码- Returns:
- this for chaining - 用于链式调用
-
userAgent
Sets User-Agent header. 设置 User-Agent 头部。- Parameters:
userAgent- the user agent - User-Agent- Returns:
- this for chaining - 用于链式调用
-
iterator
-
toString
-