为了账号安全,请及时绑定邮箱和手机立即绑定

Java CookieManager为什么获取不到cookie

Java CookieManager为什么获取不到cookie

小怪兽爱吃肉 2018-07-20 13:21:34
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();


查看完整回答
反对 回复 2018-07-22
  • 1 回答
  • 0 关注
  • 1839 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信