【安卓】Java CookieManager为什么获取不到cookie,我获取响应头的时候能看到set-cookie这个头部呀
题目描述
题目来源及自己的思路
相关代码
// 请把代码文本粘贴到下方(请勿用图片代替代码)
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);
你期待的结果是什么?实际看到的错误信息又是什么?
回答
需要在
manager.getCookieStore();之前再做一次openConnection();和getInputStream();也可以考虑用HttpClient获取cookie
CookieStore cookieStore = new BasicCookieStore();
httpClient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
List<Cookie> cookies = cookieStore.getCookies();
以上是 【安卓】Java CookieManager为什么获取不到cookie,我获取响应头的时候能看到set-cookie这个头部呀 的全部内容, 来源链接: utcz.com/a/103694.html