给定一url,如何获取访问该url产生的cookie大小、个数? JAVA实现,好的话加分
发布网友
发布时间:2022-05-16 20:27
我来回答
共1个回答
热心网友
时间:2024-03-01 17:36
public static void main(String[] args) {
try{
CookieManager manager=new CookieManager();
manager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
CookieHandler.setDefault(manager);
URLurl=new URL("http://localhost/test.php");
HttpURLConnection conn= (HttpURLConnection) url.openConnection();
conn.getHeaderFields();
CookieStore store = manager.getCookieStore();
List<HttpCookie> lCookies=store.getCookies();
System.out.printf("共%s个cookie\n",lCookies.size());
for (HttpCookie cookie: lCookies) {
System.out.printf("原:%s 名称:%s 解码值:%s\n",
cookie.toString(),
cookie.getName(),
URLDecoder.decode(cookie.getValue(), "UTF8"));
}
}catch (Exception e){
e.printStackTrace();
}
}
===================
输出:
共4个cookie
原:iC4=%E7%AC%AC4%E4%B8%AAcookie 名称:iC4 解码值:第4个cookie
原:iC3=%E7%AC%AC3%E4%B8%AAcookie 名称:iC3 解码值:第3个cookie
原:iC2=%E7%AC%AC2%E4%B8%AAcookie 名称:iC2 解码值:第2个cookie
原:iC1=%E7%AC%AC1%E4%B8%AAcookie 名称:iC1 解码值:第1个cookie
===========
看到这题特别练了下SE的cookie,我自己也有不少收获
加分加分!