Java HttpCookie parse() Method

The parse(String header) method of HttpCookie class is invoked to construct the cookies from set-cookie or set-cookie2 header string.

Syntax

Parameter

The above method requires only ne parameter:

  1. header - which denotes a string which specifies the set-cookie header. The header should start with "set-cookie" or "set-cookie2" token.

Return

The above method is used to return a List of cookies which are parsed from header line string.

Throws

IllegalArgumentException - if the header string breaks the cookie specification?s syntax or the given cookie name contains some illegal characters.

NullPointerException - if the header string is a null.

Example 1

Test it Now

Output:

Exception in thread "main" java.lang.NullPointerException
	at java.net.HttpCookie.guessCookieVersion(Unknown Source)
	at java.net.HttpCookie.parse(Unknown Source)
	at java.net.HttpCookie.parse(Unknown Source)
	at javatpoint.JavaHttpCookieParseExample1.main(JavaHttpCookieParseExample1.java:10)

Example 2

Test it Now

Output:

Exception in thread "main" java.lang.IllegalArgumentException: Invalid cookie name-value pair
	at java.net.HttpCookie.parseInternal(Unknown Source)
	at java.net.HttpCookie.parse(Unknown Source)
	at java.net.HttpCookie.parse(Unknown Source)
	at javatpoint.JavaHttpCookieParseExample2.main(JavaHttpCookieParseExample2.java:8)




Latest Courses