URL url = new URL("HTTP://km.oa.com"); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setInstanceFollowRedirects(false); CookieManager manager = new CookieManager(); manager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); CookieHandler.setDefault(manager); con.getInputStream(); System.out.println(con.getResponseCode()); CookieStore store = manager.getCookieStore(); List<HttpCookie> cookie = store.getCookies(); System.out.println(cookie); for (HttpCookie coo : cookie) { System.out.println(coo); } String str = con.getHeaderField("set-cookie"); System.out.println(str);
1 回答
胡说叔叔
TA贡献1804条经验 获得超8个赞
需要在
manager.getCookieStore();之前再做一次openConnection();和getInputStream();
也可以考虑用HttpClient获取cookie
CookieStore cookieStore = new BasicCookieStore();
httpClient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
List<Cookie> cookies = cookieStore.getCookies();
添加回答
举报
0/150
提交
取消