HttpCookie.parse(String): This method is available in the java.net.HttpCookie class of Java.
Syntax:
List<HttpCookie> java.net.HttpCookie.parse(String header)
This method takes one argument. This method constructs cookies from the set-cookie or set-cookie2 header string.
Parameters: One parameter is required for this method.
header: a String specifying the set-cookie header. The header should start with a "set-cookie", or "set-cookie2" token, or it should have no leading token at all.
Returns: a List of cookies parsed from the header line string.
Throws:
1. IllegalArgumentException - if the header string violates the cookie specification's syntax or the cookie name contains illegal characters.
2. NullPointerException - if the header string is null
Approach 1: When no exception
Java
package com.HttpCookie;import java.net.HttpCookie;public class HttpCookieparse {public static void main(String[] args) {String header = "name=value";System.out.println(HttpCookie.parse(header));}}
Output:
[name=value]
Approach 2: IllegalArgumentException
Java
package com.HttpCookie;import java.net.HttpCookie;public class HttpCookieparse {public static void main(String[] args) {String header = "name-value";System.out.println(HttpCookie.parse(header));}}
Output:
Exception in thread "main" java.lang.IllegalArgumentException: Invalid cookie name-value pair at java.base/java.net.HttpCookie.parseInternal(HttpCookie.java:836) at java.base/java.net.HttpCookie.parse(HttpCookie.java:210) at java.base/java.net.HttpCookie.parse(HttpCookie.java:186) at com.HttpCookie.HttpCookieparse.main(HttpCookieparse.java:10)
Approach 3: NullPointerException
Java
package com.HttpCookie;import java.net.HttpCookie;public class HttpCookieparse {public static void main(String[] args) {String header = null;System.out.println(HttpCookie.parse(header));}}
Output:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.toLowerCase()" because "header" is null at java.base/java.net.HttpCookie.guessCookieVersion(HttpCookie.java:1081) at java.base/java.net.HttpCookie.parse(HttpCookie.java:195) at java.base/java.net.HttpCookie.parse(HttpCookie.java:186) at com.HttpCookie.HttpCookieparse.main(HttpCookieparse.java:10)
Some other methods of HttpCookie class
HttpCookie(String, String): This method constructs a cookie with a specified name and value.
clone(): This method creates and returns a copy of this object.
HttpCookie.domainMatches(String, String): The utility method to check whether a hostname is in a domain or not.
equals(Object): This method tests the equality of two HTTP cookies.
getComment(): This method returns the comment describing the purpose of this cookie, or null if the cookie has no comment.
getCommentURL(): This method returns the comment URL describing the purpose of this cookie, or null if the cookie has no comment URL.
getDiscard(): This method returns the discard attribute of the cookie.
getDomain(): This method returns the domain name set for this cookie. The form of the domain name is set by RFC 2965.
getMaxAge(): This method returns the maximum age of the cookie, specified in seconds.
getName(): This method returns the name of the cookie.
getPath(): This method returns the path on the server to which the browser returns this cookie. The cookie is visible to all subpaths on the server.
getPortlist(): This method returns the port list attribute of the cookie.
getSecure(): This method returns true if sending this cookie should be restricted to a secure protocol, or false if it can be sent using any protocol.
getValue(): This method returns the value of the cookie.
getVersion(): This method returns the version of the protocol this cookie complies with.
hasExpired(): This method reports whether this HTTP cookie has expired or not.
hashCode(): This method returns the hash code of this HTTP cookie.
isHttpOnly(): This method returns true if this cookie contains the HttpOnly attribute.
HttpCookie.parse(String): This method constructs cookies from the set-cookie or set-cookie2 header string.
setComment(String): This method specifies a comment that describes a cookie's purpose.
setCommentURL(String): This method specifies a comment URL that describes a cookie's purpose.
setDiscard(boolean): This method specifies whether the user agent should discard the cookie unconditionally.
setDomain(String): This method specifies the domain within which this cookie should be presented.
setHttpOnly(boolean): This method 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.
setMaxAge(long): This method sets the maximum age of the cookie in seconds.
setPath(String): This method specifies a path for the cookie to which the client should return the cookie.
setPortlist(String): This method specifies the port list of the cookie, which restricts the port(s) to which a cookie may be sent back in a Cookie header.
setSecure(boolean): This method indicates whether the cookie should only be sent using a secure protocol, such as HTTPS or SSL.
setValue(String): This method 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.
setVersion(int): This method sets the version of the cookie protocol this cookie complies with.
toString(): This method constructs a cookie header string representation of this cookie, which is in the format defined by the corresponding cookie specification, but without the leading "Cookie:" token.
No comments:
Post a Comment