HttpCookie setVersion(int) in Java

setVersion(int): This method is available in the java.net.HttpCookie class of Java.

Syntax:

void java.net.HttpCookie.setVersion(int v)

This method takes one argument. This method 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: One parameter is required for this method.

v: 0 if the cookie should comply with the original Netscape specification; 1 if the cookie should comply with RFC 2965/2109.

Returns: NA

Throws:

1. IllegalArgumentException - if v is neither 0 nor 1.

Approach 1: When no exception

Java

package com.HttpCookie;

import java.net.HttpCookie;

public class HttpCookiesetVersion {
    public static void main(String[] args) {

        HttpCookie httpCookie = new HttpCookie("name", "value");

        int v = 1;
        httpCookie.setVersion(v);
        System.out.println("Successfully setVersion");
    }
}

Output:

Successfully setVersion


Approach 2: IllegalArgumentException

Java

package com.HttpCookie;

import java.net.HttpCookie;

public class HttpCookiesetVersion {
    public static void main(String[] args) {

        HttpCookie httpCookie = new HttpCookie("name", "value");

        int v = 10;
        httpCookie.setVersion(v);
        System.out.println("Successfully setVersion");
    }
}

Output:

Exception in thread "main" java.lang.IllegalArgumentException: cookie version should be 0 or 1 at java.base/java.net.HttpCookie.setVersion(HttpCookie.java:559) at com.HttpCookie.HttpCookiesetVersion.main(HttpCookiesetVersion.java:11)


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