Java HttpCookie 类

2025年3月22日 | 阅读 3 分钟

方法

方法描述
clone()创建并返回给定对象的副本。
domainMatches(String domain, String host)检查给定的主机名是否属于该域。
equals(Object obj)测试两个 HTTP Cookie 的相等性。
getComment()返回描述 Cookie 目的的注释。如果 Cookie 没有注释,则返回 null。
getCommentURL()返回描述 Cookie 目的的注释 URL。如果 Cookie 没有注释 URL,则返回 null。
getDiscard()返回 Cookie 的 discard 属性。
getDomain()返回为给定 Cookie 设置的域(domain)名。
getMaxAge()返回 Cookie 在秒为单位的最大生存期。
getName()返回 Cookie 的名称。
getPath()返回浏览器将 Cookie 返回到服务器上的路径。
getPortList()返回 Cookie 的端口列表(port list)属性。
getSecure()如果发送 Cookie 被限制为安全协议,则返回 true。如果使用任何协议发送,则返回 false。
getValue()返回 Cookie 的值。
getVersion()返回 Cookie 所遵循协议的版本。
hasExpired()报告 HTTP Cookie 是否已过期。
hasCode()返回给定 HTTP Cookie 的哈希码。
isHttpOnly()如果 Cookie 包含 HttpOnly 属性,则返回 true。
parse(String header)从 set-cookie 或 set-cookie2 头字符串构建 Cookie。
setComment(String purpose)指定一个描述 Cookie 目的的注释。
setCommentURL(String purpose)指定一个描述 Cookie 目的的注释 URL。
setDiscard(boolean discard)指定用户代理是否可以无条件丢弃 Cookie。
setDomain(String pattern)指定 Cookie 可以呈现的域。
setHttpOnly(boolean httpOnly)指示 Cookie 是否可以被视为 HTTP Only。
setMaxAge(long expiry)设置 Cookie 在秒为单位的最大生存期。
setPath(String uri)为 Cookie 指定一个客户端可以返回 Cookie 的路径。
setPortlist(String ports)指定 Cookie 的端口列表,限制 Cookie 可以通过哪些端口在 Cookie 头中返回。
setSecure(boolean flag)指示 Cookie 是否只能通过安全协议(如 HTTPS 或 SSL)发送。
setValue(String newValue)在 Cookie 创建后为其分配新值。
setVersion(int v)设置 Cookie 协议的版本。
toString()为给定的 Cookie 构建 Cookie 头字符串表示,该格式由相应的 Cookie 规范定义。

示例 1

输出

Comment: This is for tutorial purpose.
The required domain is: javatpoint.com
The maximum age is given as: 2000
Name for the cookie is given as: Java
The hash code for the given cookie is given as: -357741581 

示例 2

输出

The discard attribute is given as: true
The version is given as: 1
The value is given as: Hello
Check whether cookie has expired or not: false
Check whether the cookie contains the HttpOnly attribute or not: true
下一个主题clone() 方法