public final class HttpCookie extends Object implements Cloneable
有3个HTTP Cookie规格:
Netscape draft
RFC 2109 - http://www.ietf.org/rfc/rfc2109.txt
RFC 2965 - http://www.ietf.org/rfc/rfc2965.txt
httpcookie类可以接受所有这3种形式的语法。
Constructor and Description |
---|
HttpCookie(String name, String value)
用指定名称和值构造一个具有指定名称和值的。
|
Modifier and Type | Method and Description |
---|---|
Object |
clone()
创建并返回此对象的副本。
|
static boolean |
domainMatches(String domain, String host)
检查主机名是否在域中的实用方法。
|
boolean |
equals(Object obj)
测试两个HTTP Cookie的平等。
|
String |
getComment()
返回描述这个cookie的目的的评论,或
null 如果cookie没有评论。
|
String |
getCommentURL()
返回描述这个cookie的目的评论的URL,或
null 如果cookie没有评论的URL。
|
boolean |
getDiscard()
返回饼干的丢弃属性
|
String |
getDomain()
返回此“饼干”的域名设置。
|
long |
getMaxAge()
返回的最大年龄的饼干,在几秒钟内指定。
|
String |
getName()
返回饼干的名称。
|
String |
getPath()
返回浏览器返回此“饼干”的服务器上的路径。
|
String |
getPortlist()
返回饼干的端口列表属性
|
boolean |
getSecure()
返回
true 如果发送该Cookie应限制在一个安全的协议,或
false 如果可以使用任何协议发送。
|
String |
getValue()
返回饼干的值。
|
int |
getVersion()
返回此“饼干”符合的协议的版本。
|
boolean |
hasExpired()
报告是否HTTP cookie已经过期或不。
|
int |
hashCode()
返回此HTTP cookie的哈希代码。
|
boolean |
isHttpOnly()
返回
true 如果这个cookie包含类属性。
|
static List<HttpCookie> |
parse(String header)
结构设置cookie或set-cookie2字符串饼干。
|
void |
setComment(String purpose)
指定描述了一个用于描述一个饼干的目的。
|
void |
setCommentURL(String purpose)
指定描述了一个饼干的目的的注释网址。
|
void |
setDiscard(boolean discard)
指定是否应无条件地丢弃用户代理。
|
void |
setDomain(String pattern)
指定要提交该饼干的域。
|
void |
setHttpOnly(boolean httpOnly)
指示是否应考虑HTTP cookie只。
|
void |
setMaxAge(long expiry)
设置在几秒钟内的最大年龄的饼干。
|
void |
setPath(String uri)
指定要返回的“小”的“要返回”的“小”的路径。
|
void |
setPortlist(String ports)
指定cookie的portlist,制约了港口(S),Cookie可以在cookie头送回来。
|
void |
setSecure(boolean flag)
表明cookie是否只能使用一个安全协议发送,如HTTPS和SSL。
|
void |
setValue(String newValue)
创建一个新值后,创建一个新的值到一个。
|
void |
setVersion(int v)
设置此“饼干”符合的“饼干”协议的版本。
|
String |
toString()
构建了一个这个饼干的饼干标题字符串表示,这是由相应的饼干规范定义的格式,但没有领先的“饼干:”令牌。
|
public HttpCookie(String name, String value)
名称必须符合RFC 2965。这意味着它只能包含ASCII字符,并且不能包含逗号,分号或空格或以$开头的字符串。创建后无法更改该名称的名称。
该值可以是服务器选择发送的任何东西。它的值可能只对服务器感兴趣。cookie的值可以改变后的setValue
方法创新。
默认情况下,饼干是根据RFC 2965规范创建Cookie。该版本可与setVersion
方法改变。
name
-
String
指定cookie的名字
value
-
String
指定的cookie的值
IllegalArgumentException
-如果cookie名称包含非法字符
null
name
NullPointerException
setValue(java.lang.String)
,
setVersion(int)
public static List<HttpCookie> parse(String header)
header
-
String
指定的Set-Cookie标头。标题应该从“饼干”,或“set-cookie2”标记;或者它应该没有领导的令牌。
IllegalArgumentException
如果字符串违反规范的语法或Cookie cookie名称包含非法字符。
NullPointerException
如果字符串是
null
public boolean hasExpired()
true
表明这个HTTP cookie过期了;否则,
false
public void setComment(String purpose)
purpose
-
String
指定要显示给用户的评论
getComment()
public String getComment()
null
如果cookie没有评论。
String
包含注释,或
null
如果没有
setComment(java.lang.String)
public void setCommentURL(String purpose)
purpose
-
String
指定要显示给用户的评论的URL
getCommentURL()
public String getCommentURL()
null
如果cookie没有评论的URL。
String
包含注释的URL,或
null
如果没有
setCommentURL(java.lang.String)
public void setDiscard(boolean discard)
discard
-
true
表示无条件地丢弃的饼干
getDiscard()
public boolean getDiscard()
boolean
代表该Cookie的丢弃属性
setDiscard(boolean)
public void setPortlist(String ports)
ports
-
String
指定端口列表中,并以逗号分隔的一系列数字
getPortlist()
public String getPortlist()
String
包含端口列表或
null
如果没有
setPortlist(java.lang.String)
public void setDomain(String pattern)
域名的形式是由RFC 2965规定。一个域名从一个点(.foo.com
)意味着饼干是指定的域名系统(DNS)服务器,可见区(例如,www.foo.com
,但不a.b.foo.com
)。默认情况下,只返回发送给它们的服务器的“饼干”。
pattern
-
String
含内这个饼干是可见的域名;形式是根据RFC 2965
getDomain()
public String getDomain()
String
包含域名
setDomain(java.lang.String)
public void setMaxAge(long expiry)
一个积极的价值表明,该饼干将过期后,许多秒已经过去了。请注意,价值是最大限度cookie有效期满,没有cookie的时代。
负值意味着无法持久存储,当Web浏览器退出时将被删除。一零值导致要删除的小饼干。
expiry
指定秒Cookie的最大年龄的整数;如果是零,饼干应立即丢弃;否则,cookie的最大年龄不详。
getMaxAge()
public long getMaxAge()
-1
指示cookie将持续直到浏览器关闭。
setMaxAge(long)
public void setPath(String uri)
Cookie是在您指定的目录中的所有页可见,在该目录的子目录的所有页面。一个cookie的路径必须包括servlet,设置cookie,比如/目录,使饼干可见所有服务器上的目录下目录。
参考RFC 2965(可在互联网上设置Cookie)路径名称的更多信息。
uri
-
String
指定路径
getPath()
public String getPath()
String
指定路径中包含一个servlet的名字,例如,/目录
setPath(java.lang.String)
public void setSecure(boolean flag)
默认值是false
。
flag
-如果
true
,cookie只能在协议如HTTPS安全送。如果
false
,它可以在任何协议发送。
getSecure()
public boolean getSecure()
true
如果发送该Cookie应限制在一个安全的协议,或
false
如果可以使用任何协议发送。
false
如果cookie可以在任何标准的协议发送;否则,
true
setSecure(boolean)
public String getName()
String
指定cookie的名字
public void setValue(String newValue)
0版本的饼干,值不能包含空格,括号,括号、等号、逗号、双引号、斜杠,问号,冒号和分号的迹象,。空值可能不会在所有浏览器上都表现相同的方式。
newValue
-
String
指定新的值
getValue()
public String getValue()
String
包含Cookie的现值
setValue(java.lang.String)
public int getVersion()
setVersion(int)
public void setVersion(int v)
v
0如果cookie应符合原Netscape规范;1如果cookie应符合RFC 2965 / 2109
IllegalArgumentException
-如果
v
既不是0也不是1
getVersion()
public boolean isHttpOnly()
true
如果这个cookie包含类属性。这意味着饼干不应该是脚本引擎的访问,如JavaScript。
true
应考虑类
setHttpOnly(boolean)
public void setHttpOnly(boolean httpOnly)
true
意味着饼干不应该像JavaScript脚本引擎的访问。
httpOnly
-如果
true
使Cookie HTTP,即可见作为HTTP请求的一部分。
isHttpOnly()
public static boolean domainMatches(String domain, String host)
这个概念描述在饼干规范。要理解这个概念,需要先定义一些术语:
effective host name = hostname if host name contains dot
or = hostname.local if not
主机名称域匹配主机B的如果:
- their host name strings string-compare equal; or
- A is a HDN string and has the form NB, where N is a non-empty name string, B has the form .B', and B' is a HDN string. (So, x.y.com domain-matches .Y.com but not Y.com.)
域中的主机并不(RFC 2965秒。3.3.2)如果:
- The value for the Domain attribute contains no embedded dots, and the value is not .local.
- The effective host name that derives from the request-host does not domain-match the Domain attribute.
- The request-host is a HDN (not IP address) and has the form HD, where D is the value of the Domain attribute, and H is a string that contains one or more dots.
实例:
- A Set-Cookie2 from request-host y.x.foo.com for Domain=.foo.com would be rejected, because H is y.x and contains a dot.
- A Set-Cookie2 from request-host x.foo.com for Domain=.foo.com would be accepted.
- A Set-Cookie2 with Domain=.com or Domain=.com., will always be rejected, because there is no embedded dot.
- A Set-Cookie2 from request-host example for Domain=.local will be accepted, because the effective host name for the request- host is example.local, and example.local domain-matches .local.
domain
-域名检查主机名
host
-问题的主机名
true
如果域名匹配;如果不
false
public String toString()
public boolean equals(Object obj)
其结果是true
只有两饼干来自同一个域(不区分大小写),具有相同的名称(不区分大小写),并具有相同的路径(区分大小写)。
equals
方法重写,继承类
Object
obj
-参考对象的比较。
true
HTTP Cookie相等;否则,
false
Object.hashCode()
,
HashMap
public int hashCode()
getName().toLowerCase().hashCode()
+ getDomain().toLowerCase().hashCode()
+ getPath().hashCode()
hashCode
方法重写,继承类
Object
Object.equals(java.lang.Object)
,
System.identityHashCode(java.lang.Object)
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.