Java CookieManager为什么获取不到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);

https://img4.mukewang.com/5b54871e0001312308000547.jpg

小怪兽爱吃肉
浏览 1840回答 1
1回答

胡说叔叔

需要在manager.getCookieStore();之前再做一次openConnection();和getInputStream();也可以考虑用HttpClient获取cookieCookieStore cookieStore = new BasicCookieStore();httpClient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();List<Cookie> cookies = cookieStore.getCookies();
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java