CookieManager put(URI, Map) in Java

put(URI, Map): This method is available in the java.net.CookieManager class of Java.

Syntax:

void java.net.CookieManager.put(URI uri, Map<String, List<String>> responseHeaders) throws IOException

This method takes two arguments. 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.

Parameters: Two parameters are required for this method.

uri: a URI where the cookies come from.

responseHeaders: an immutable map from field names to lists of field values representing the response header fields returned.

Returns: NA

Throws:

1. IOException - if an I/O error occurs

Approach

Java

package com.CookieManager;

import java.io.IOException;
import java.net.CookieManager;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CookieManagerput {
    public static void main(String[] args)
throws URISyntaxException, IOException {

        CookieManager cookieManager = new CookieManager();

        URI uri = new URI("/hello");
        Map<String, List<String>> map = new HashMap<>();
        List<String> value = new ArrayList<>();
        value.add("java");
        map.put("hello", value);
        cookieManager.put(uri, map);
        System.out.println("Successfully put");
    }
}

Output:

Successfully put


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