zoukankan      html  css  js  c++  java
  • HttpCookie类

    HttpCookie

    所有已实现的接口:Cloneable

    public final class HttpCookie
    extends Object
    implements Cloneable
     
    An HttpCookie object represents an HTTP cookie, which carries state information between server and user agent.
    一个HttpCookie对象表示一个HTTP cookie,它在服务器和用户代理之间传递状态信息。
     
    Cookie is widely adopted to create stateful sessions. 
    Cookie被广泛用于创建有状态会话。
     

    There are 3 HTTP cookie specifications:

    有3个HTTP cookie 规范

    Netscape draft
    RFC 2109 - http://www.ietf.org/rfc/rfc2109.txt
    RFC 2965 - http://www.ietf.org/rfc/rfc2965.txt

    HttpCookie class can accept all these 3 forms of syntax.

    HttpCookie 类 可以接受所有这三种形式的语法

    Since:
    1.6 

     

     

    Constructor Summary构造函数的总结

     

    Constructors 构造函数

    Constructor and Description

    构造函数和描述

    HttpCookie(String name, String value)
    Constructs a cookie with a specified name and value.
    构造具有指定名称和值的cookie。


    Method Summary

     

    Modifier and TypeMethod and Description
    Object clone()
    Create and return a copy of this object.
    创建并返回此对象的副本。
    static boolean domainMatches(String domain, String host)
    The utility method to check whether a host name is in a domain or not.
    这个通用的程序方法检查主机名是否在域中。
    boolean equals(Object obj)
    Test the equality of two HTTP cookies.
    测试两个HTTP cookie的相等。
    String getComment()
    Returns the comment describing the purpose of this cookie, or null if the cookie has no comment.
    返回描述此cookie用途的注释,如果该cookie没有描述,则返回null。
    String getCommentURL()
    Returns the comment URL describing the purpose of this cookie, or null if the cookie has no comment URL.
    返回描述此cookie用途的注释URL,如果该cookie没有注释URL,则返回null。
    boolean getDiscard()
    Returns the discard attribute of the cookie
    返回cookie的丢弃属性
    String getDomain()
    Returns the domain name set for this cookie.
    返回此cookie的域名集。
    long getMaxAge()
    Returns the maximum age of the cookie, specified in seconds.
    返回cookie的最大生命周期,以秒为单位指定。
    String getName()
    Returns the name of the cookie.
    返回cookie的名称。
    String getPath()
    Returns the path on the server to which the browser returns this cookie.
    返回浏览器返回此cookie的服务器上的路径。
    String getPortlist()
    Returns the port list attribute of the cookie.
    返回cookie的端口列表属性。
    boolean getSecure()
    Returns true if sending this cookie should be restricted to a secure protocol, or false if the it can be sent using any protocol.
    如果发送此cookie应该被限制在一个安全协议中,则返回true;如果可以使用任何协议发送此cookie,则返回false。
    String getValue()
    Returns the value of the cookie.
    返回cookie的值。
    int getVersion()
    Returns the version of the protocol this cookie complies with.
    返回此cookie遵守的协议的版本。
    boolean hasExpired()
    Reports whether this HTTP cookie has expired or not.
    报告此HTTP cookie是否已过期。
    int hashCode()
    Returns the hash code of this HTTP cookie.
    返回此HTTP cookie的哈希码。
    boolean isHttpOnly()
    Returns true if this cookie contains the HttpOnly attribute.
    如果此cookie包含HttpOnly属性,则返回true。
    static List<HttpCookie> parse(String header)
    Constructs cookies from set-cookie or set-cookie2 header string.
    从set-cookie或set-cookie2头字符串构造cookie。
    void setComment(String purpose)
    Specifies a comment that describes a cookie's purpose.
    指定描述cookie用途的注释。
    void setCommentURL(String purpose)
    Specifies a comment URL that describes a cookie's purpose.
    指定描述cookie用途的注释URL
    void setDiscard(boolean discard)
    Specify whether user agent should discard the cookie unconditionally.
    指定用户代理是否应该无条件地丢弃cookie。
    void setDomain(String pattern)
    Specifies the domain within which this cookie should be presented.
    指定应该在其中显示此cookie的域。
    void setHttpOnly(boolean httpOnly)
    Indicates whether the cookie should be considered HTTP Only.
    指示是否应该只考虑HTTP cookie。
    void setMaxAge(long expiry)
    Sets the maximum age of the cookie in seconds.
    设置cookie的最大生命周期(以秒为单位)。
    void setPath(String uri)
    Specifies a path for the cookie to which the client should return the cookie.
    为客户端应该返回的cookie指定一个路径。
    void setPortlist(String ports)
    Specify the portlist of the cookie, which restricts the port(s) to which a cookie may be sent back in a Cookie header.
    指定cookie的端口列表,它限制cookie在cookie头中可能被发送回的端口。
    void setSecure(boolean flag)
    Indicates whether the cookie should only be sent using a secure protocol, such as HTTPS or SSL.
    指示是否仅应使用安全协议(如HTTPS或SSL)发送cookie。
    void setValue(String newValue)
    Assigns a new value to a cookie after the cookie is created.
    在创建cookie之后,为cookie分配一个新值。
    void setVersion(int v)
    Sets the version of the cookie protocol this cookie complies with.
    设置此cookie遵守的cookie协议的版本。
    String toString()
    Constructs a cookie header string representation of this cookie, which is in the format defined by corresponding cookie specification, but without the leading "Cookie:" token.
    构造此cookie的cookie标头字符串表示形式,其格式由相应的cookie规范定义,但没有前导“Cookie:”标志。

     

     

    Methods inherited from class java.lang.Object

    以下方法继承自java.lang.Object类

    finalize, getClass, notify, notifyAll, wait, wait, wait

     


    Constructor Detail

    构造函数详情

    • HttpCookie

      public HttpCookie(String name,
                        String value)
      Constructs a cookie with a specified name and value.

      The name must conform to RFC 2965. That means it can contain only ASCII alphanumeric characters and cannot contain commas, semicolons, or white space or begin with a $ character. The cookie's name cannot be changed after creation.

      The value can be anything the server chooses to send. Its value is probably of interest only to the server. The cookie's value can be changed after creation with the setValue method.

      By default, cookies are created according to the RFC 2965 cookie specification. The version can be changed with the setVersion method.

      Parameters:
      name - a String specifying the name of the cookie
      value - a String specifying the value of the cookie
      Throws:
      IllegalArgumentException - if the cookie name contains illegal characters
      NullPointerException - if name is null
      See Also:
      setValue(java.lang.String), setVersion(int) 

    Method Detail

    方法详情

    • parse

      public static List<HttpCookie> parse(String header)
      Constructs cookies from set-cookie or set-cookie2 header string. RFC 2965 section 3.2.2 set-cookie2 syntax indicates that one header line may contain more than one cookie definitions, so this is a static utility method instead of another constructor.
      Parameters:
      header - a String specifying the set-cookie header. The header should start with "set-cookie", or "set-cookie2" token; or it should have no leading token at all.
      Returns:
      a List of cookie parsed from header line string
      Throws:
      IllegalArgumentException - if header string violates the cookie specification's syntax or the cookie name contains illegal characters.
      NullPointerException - if the header string is null
    • hasExpired

      public boolean hasExpired()
      Reports whether this HTTP cookie has expired or not.
      Returns:
      true to indicate this HTTP cookie has expired; otherwise, false
    • setComment

      public void setComment(String purpose)
      Specifies a comment that describes a cookie's purpose. The comment is useful if the browser presents the cookie to the user. Comments are not supported by Netscape Version 0 cookies.
      Parameters:
      purpose - a String specifying the comment to display to the user
      See Also:
      getComment()
    • getComment

      public String getComment()
      Returns the comment describing the purpose of this cookie, or null if the cookie has no comment.
      Returns:
      a String containing the comment, or null if none
      See Also:
      setComment(java.lang.String)
    • setCommentURL

      public void setCommentURL(String purpose)
      Specifies a comment URL that describes a cookie's purpose. The comment URL is useful if the browser presents the cookie to the user. Comment URL is RFC 2965 only.
      Parameters:
      purpose - a String specifying the comment URL to display to the user
      See Also:
      getCommentURL()
    • getCommentURL

      public String getCommentURL()
      Returns the comment URL describing the purpose of this cookie, or null if the cookie has no comment URL.
      Returns:
      a String containing the comment URL, or null if none
      See Also:
      setCommentURL(java.lang.String)
    • setDiscard

      public void setDiscard(boolean discard)
      Specify whether user agent should discard the cookie unconditionally. This is RFC 2965 only attribute.
      Parameters:
      discard - true indicates to discard cookie unconditionally
      See Also:
      getDiscard()
    • getDiscard

      public boolean getDiscard()
      Returns the discard attribute of the cookie
      Returns:
      a boolean to represent this cookie's discard attribute
      See Also:
      setDiscard(boolean)
    • setPortlist

      public void setPortlist(String ports)
      Specify the portlist of the cookie, which restricts the port(s) to which a cookie may be sent back in a Cookie header.
      Parameters:
      ports - a String specify the port list, which is comma separated series of digits
      See Also:
      getPortlist()
    • getPortlist

      public String getPortlist()
      Returns the port list attribute of the cookie
      Returns:
      a String contains the port list or null if none
      See Also:
      setPortlist(java.lang.String)
    • setDomain

      public void setDomain(String pattern)
      Specifies the domain within which this cookie should be presented.

      The form of the domain name is specified by RFC 2965. A domain name begins with a dot (.foo.com) and means that the cookie is visible to servers in a specified Domain Name System (DNS) zone (for example, www.foo.com, but not a.b.foo.com). By default, cookies are only returned to the server that sent them.

      Parameters:
      pattern - a String containing the domain name within which this cookie is visible; form is according to RFC 2965
      See Also:
      getDomain()
    • getDomain

      public String getDomain()
      Returns the domain name set for this cookie. The form of the domain name is set by RFC 2965.
      Returns:
      a String containing the domain name
      See Also:
      setDomain(java.lang.String)
    • setMaxAge

      public void setMaxAge(long expiry)
      Sets the maximum age of the cookie in seconds.

      A positive value indicates that the cookie will expire after that many seconds have passed. Note that the value is the maximum age when the cookie will expire, not the cookie's current age.

      A negative value means that the cookie is not stored persistently and will be deleted when the Web browser exits. A zero value causes the cookie to be deleted.

      Parameters:
      expiry - an integer specifying the maximum age of the cookie in seconds; if zero, the cookie should be discarded immediately; otherwise, the cookie's max age is unspecified.
      See Also:
      getMaxAge()
    • getMaxAge

      public long getMaxAge()
      Returns the maximum age of the cookie, specified in seconds. By default, -1 indicating the cookie will persist until browser shutdown.
      Returns:
      an integer specifying the maximum age of the cookie in seconds
      See Also:
      setMaxAge(long)
    • setPath

      public void setPath(String uri)
      Specifies a path for the cookie to which the client should return the cookie.

      The cookie is visible to all the pages in the directory you specify, and all the pages in that directory's subdirectories. A cookie's path must include the servlet that set the cookie, for example, /catalog, which makes the cookie visible to all directories on the server under /catalog.

      Consult RFC 2965 (available on the Internet) for more information on setting path names for cookies.

      Parameters:
      uri - a String specifying a path
      See Also:
      getPath()
    • getPath

      public String getPath()
      Returns the path on the server to which the browser returns this cookie. The cookie is visible to all subpaths on the server.
      Returns:
      a String specifying a path that contains a servlet name, for example, /catalog
      See Also:
      setPath(java.lang.String)
    • setSecure

      public void setSecure(boolean flag)
      Indicates whether the cookie should only be sent using a secure protocol, such as HTTPS or SSL.

      The default value is false.

      Parameters:
      flag - If true, the cookie can only be sent over a secure protocol like HTTPS. If false, it can be sent over any protocol.
      See Also:
      getSecure()
    • getSecure

      public boolean getSecure()
      Returns true if sending this cookie should be restricted to a secure protocol, or false if the it can be sent using any protocol.
      Returns:
      false if the cookie can be sent over any standard protocol; otherwise, true
      See Also:
      setSecure(boolean)
    • getName

      public String getName()
      Returns the name of the cookie. The name cannot be changed after creation.
      Returns:
      a String specifying the cookie's name 
    • setValue

      public void setValue(String newValue)
      Assigns a new value to a cookie after the cookie is created. If you use a binary value, you may want to use BASE64 encoding.

      With Version 0 cookies, values should not contain white space, brackets, parentheses, equals signs, commas, double quotes, slashes, question marks, at signs, colons, and semicolons. Empty values may not behave the same way on all browsers.

      Parameters:
      newValue - a String specifying the new value
      See Also:
      getValue()
    • getValue

      public String getValue()
      Returns the value of the cookie.
      Returns:
      a String containing the cookie's present value
      See Also:
      setValue(java.lang.String)
    • getVersion

      public int getVersion()
      Returns the version of the protocol this cookie complies with. Version 1 complies with RFC 2965/2109, and version 0 complies with the original cookie specification drafted by Netscape. Cookies provided by a browser use and identify the browser's cookie version.
      Returns:
      0 if the cookie complies with the original Netscape specification; 1 if the cookie complies with RFC 2965/2109
      See Also:
      setVersion(int)
    • setVersion

      public void setVersion(int v)
      Sets the version of the cookie protocol this cookie complies with. Version 0 complies with the original Netscape cookie specification. Version 1 complies with RFC 2965/2109.
      Parameters:
      v - 0 if the cookie should comply with the original Netscape specification; 1 if the cookie should comply with RFC 2965/2109
      Throws:
      IllegalArgumentException - if v is neither 0 nor 1
      See Also:
      getVersion()
    • isHttpOnly

      public boolean isHttpOnly()
      Returns true if this cookie contains the HttpOnly attribute. This means that the cookie should not be accessible to scripting engines, like javascript.
      Returns:
      true if this cookie should be considered HTTPOnly
      See Also:
      setHttpOnly(boolean)
    • setHttpOnly

      public void setHttpOnly(boolean httpOnly)
      Indicates whether the cookie should be considered HTTP Only. If set to true it means the cookie should not be accessible to scripting engines like javascript.
      Parameters:
      httpOnly - if true make the cookie HTTP only, i.e. only visible as part of an HTTP request.
      See Also:
      isHttpOnly()
    • domainMatches

        public static boolean domainMatches(String domain,String host)
        The utility method to check whether a host name is in a domain or not.

        This concept is described in the cookie specification. To understand the concept, some terminologies need to be defined first:

    effective host name = hostname if host name contains dot
                      or = hostname.local if not

        Host A's name domain-matches host B's if:

    • 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.)

        A host isn't in a domain (RFC 2965 sec. 3.3.2) if:

    • 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.

        Examples:

    • 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.
        Parameters:
        domain - the domain name to check host name with
        host - the host name in question
        Returns:
        true if they domain-matches; false if not 

     

    • toString

      public String toString()
      Constructs a cookie header string representation of this cookie, which is in the format defined by corresponding cookie specification, but without the leading "Cookie:" token.
      Overrides:
      toString in class Object
      Returns:
      a string form of the cookie. The string has the defined format 
    • equals

      public boolean equals(Object obj)
      Test the equality of two HTTP cookies.

      The result is true only if two cookies come from same domain (case-insensitive), have same name (case-insensitive), and have same path (case-sensitive).

      Overrides:
      equals in class Object
      Parameters:
      obj - the reference object with which to compare.
      Returns:
      true if two HTTP cookies equal to each other; otherwise, false
      See Also:
      Object.hashCode(), HashMap
    • hashCode

      public int hashCode()
      Returns the hash code of this HTTP cookie. The result is the sum of hash code value of three significant components of this cookie: name, domain, and path. That is, the hash code is the value of the expression:
      getName().toLowerCase().hashCode()
      + getDomain().toLowerCase().hashCode()
      + getPath().hashCode()
      Overrides:
      hashCode in class Object
      Returns:
      this HTTP cookie's hash code
      See Also:
      Object.equals(java.lang.Object), System.identityHashCode(java.lang.Object)
    • clone

      public Object clone()
      Create and return a copy of this object.
      Overrides:
      clone in class Object
      Returns:
      a clone of this HTTP cookie
      See Also:
      Cloneable

     

  • 相关阅读:
    java笔记 chapter3 对象,抽象,package,import,权限修饰符,属性,方法,构造方法
    javass 视频笔记三 switch语句 for循环,while循环,do-while循环,break和continue
    java笔记 chapter1 java是什么,能干什么,有什么,特点,开发环境
    在用SSH框架中的碰见的一些问题
    这几天写MFC时候碰到的一些问题!
    2_1.8_点击按钮__改变背景颜色
    1_1.7_hello_android
    phpstudy客户端的使用
    navicat
    iptables防火墙
  • 原文地址:https://www.cnblogs.com/LinQingYang/p/12444581.html
Copyright © 2011-2022 走看看