HttpURLConnection中如何使用Cookie呢?

书欣 Java经验 发布时间:2022-12-29 11:02:29 阅读数:8686 1
下文笔者讲述使用HttpURLConnection中使用Cookie的简介说明,如下所示
实现思路:
    使用CookieManager和CookieHandler
	即可实现HttpURLConnection对Cookie的操作
例:
 
CookieManager manager = new CookieManager();
CookieHandler.setDefault(manager);
//以上代码,就可将Cookie信息进行存储
//下次访问网站时,就会将Cookie附上

CookieManager设置CookiePolicy

 
CookieManager manager = new CookieManager();
//设置cookie策略,只接受与你对话服务器的cookie,而不接收Internet上其它服务器发送的cookie
manager.setCookiePolicy(CookiePolicy.ACCEPT_ORIGINAL_SERVER);

CookieHandler、CookieManager 、CookieStore、 HttpCookie

public class CookieManagerDemo {

    //打印cookie信息
    public static void printCookie(CookieStore cookieStore){
        list<HttpCookie> listCookie = cookieStore.getCookies();
        listCookie.forEach(httpCookie -> {
            System.out.println("--------------------------------------");
            System.out.println("class      : "+httpCookie.getClass());
            System.out.println("comment    : "+httpCookie.getComment());
            System.out.println("commentURL : "+httpCookie.getCommentURL());
            System.out.println("discard    : "+httpCookie.getDiscard());
            System.out.println("domain     : "+httpCookie.getDomain());
            System.out.println("maxAge     : "+httpCookie.getMaxAge());
            System.out.println("name       : "+httpCookie.getName());
            System.out.println("path       : "+httpCookie.getPath());
            System.out.println("portlist   : "+httpCookie.getPortlist());
            System.out.println("secure     : "+httpCookie.getSecure());
            System.out.println("value      : "+httpCookie.getValue());
            System.out.println("version    : "+httpCookie.getVersion());
            System.out.println("httpCookie : "+httpCookie);
        });
    }

    public static void requestURL() throws Exception{
        URL url = new URL("http://192.168.8.8/index.jsp");
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        String basic = Base64.getEncoder().encodeToString("webSiteName:java265.com".getBytes());
        conn.setRequestProperty("Proxy-authorization", "Basic " + basic);
        BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line = null;
        while((line=br.readLine())!=null){
            System.out.println(line);
        }
        br.close();
    }

    public static void main(String[] args) throws Exception {
        
        CookieManager manager = new CookieManager();
        //设置cookie策略,只接受与你对话服务器的cookie,而不接收Internet上其它服务器发送的cookie
        manager.setCookiePolicy(CookiePolicy.ACCEPT_ORIGINAL_SERVER);
        CookieHandler.setDefault(manager);
        
        printCookie(manager.getCookieStore());
        //第一次请求
        requestURL();

        printCookie(manager.getCookieStore());
        //第二次请求
        requestURL();
    }
    
}
以上代码就是实现第二次访问时
将Cookie信息自动携带
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaJingYan/202212/16722842965224.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者