CookieManager setCookiePolicy(CookiePolicy) in Java

setCookiePolicy(CookiePolicy): This method is available in the java.net.CookieManager class of Java.

Syntax:

void java.net.CookieManager.setCookiePolicy(CookiePolicy cookiePolicy)

This method takes one argument. To set the cookie policy of this cookie manager.

A instance of CookieManager will have cookie policy ACCEPT_ORIGINAL_SERVER by default. Users always can call this method to set another cookie policy.

Parameters: One parameter is required for this method.

cookiePolicy: the cookie policy. Can be null, which has no effects on current cookie policy.

Returns: NA

Exceptions: NA

Approach

Java

package com.CookieManager;

import java.net.CookieManager;
import java.net.CookiePolicy;

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

        CookieManager cookieManager = new CookieManager();

        cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
        System.out.println("Successfully sets cookiepolicy");
    }
}

Output:

Successfully sets cookiepolicy


Some other methods of CookieManager

CookieManager()Create a new cookie manager.

CookieManager(CookieStore, CookiePolicy)This method creates a new cookie manager with a specified cookie store and cookie policy.

get(URI, Map)This method gets all the applicable cookies from a cookie cache for the specified uri in the request header.

getCookieStore()To retrieve current cookie store.

put(URI, Map)This method sets all the applicable cookies, examples are response header fields that are named Set-Cookie2, present in the response headers into a cookie cache.

setCookiePolicy(CookiePolicy)To set the cookie policy of this cookie manager.

No comments:

Post a Comment