java cookie

// Java Program to illustrate getCookieStore()method of
// java.net.CookieManager Class
// java.net package is imported
// for network related things
import java.net.*;
// Importing List class from
// java.util package
import java.util.List;
// Class
// To set CookiePolicy in JavaCookieManager Class
public class GFG {
    // Main driver method
    public static void main(String args[]) throws Exception
    {
        // Random URI as input
        String uri = "https://www.geeksforgeeks.org/";
        // Creating an object of Cookie Manager Class
        // Creating an object of CookieManager Class
        CookieManager cookieManager = new CookieManager();
        //  Setting and unsetting system wide Cookie Handler
        // using the setDefault() method
        CookieHandler.setDefault(cookieManager);
        // Pre-defined policy that accepts cookies
        // from original server.
        CookiePolicy cookiePolicy
            = CookiePolicy.ACCEPT_ORIGINAL_SERVER;
        // Setting the cookie policy of the cookieManager
        // class using the setCookiePolicy() method
        cookieManager.setCookiePolicy(cookiePolicy);
        // Now, creating an object of URL class
        URL url = new URL(uri);
        // Establishing the connection over the object
        // of URLConnection class
        URLConnection connection = url.openConnection();
        // Receiving the response
        connection.getContent();
        // Lastly, creating object of CookieStore to
        // retrieve current cookie store instance
        CookieStore cookieStore
            = cookieManager.getCookieStore();
        // For this, creating an List of object type
        // HttpCookie
        List<HttpCookie> cookieList
            = cookieStore.getCookies();
        // Iterating over the List of objects
        // using the for each loop
        for (HttpCookie cookie : cookieList) {
            // Print the Domain name and Cookie name using
            // using getName() and getDomain() method
            System.out.println("Domain name is: "
                               + cookie.getDomain());
            System.out.println("Cookie name is: "
                               + cookie.getName());
        }
    }
}
输出:

The Domain is: www.geeksforgeeks.org
java.net.InMemoryCookieStore@4590c9c3

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值