0

When using Java in an app server environment, of course, you can read the cookies.

Is it possible to read cookies from a Java Standalone program? Often the next example is shown. When running, no cookies are listed.

public class CookiesReader {
    public static void main(String[] args) {
        CookieManager manager = new CookieManager();
        manager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
        CookieHandler.setDefault(manager);
        try {
            URL url = new URL("https://nu.nl");
            URLConnection connection = url.openConnection();
            connection.getContent();
            CookieStore cookieJar = manager.getCookieStore();
            List<HttpCookie> cookies = cookieJar.getCookies();
            for (HttpCookie cookie : cookies) {
                System.out.println("CookieHandler retrieved cookie: " + cookie);
            }
        } catch ( Exception e) {
            e.printStackTrace();
        }
    }
}

SOLUTION: thanks to Sotirios Delimanolis

The steps are: open connection > get access to the header fields > access the "Set-Cookie" attribute > parse the cookie stuff.

public class CookiesReader {
    static final String COOKIES_HEADER = "Set-Cookie";

    public static void main(String[] args) {
        CookieManager cookieManager = new CookieManager();
        CookieHandler.setDefault(cookieManager);
        try {
            URL url = new URL("https://google.com");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.getContent();
            Map<String, List<String>> headerFields = connection.getHeaderFields();
            List<String> cookiesHeader = headerFields.get(COOKIES_HEADER);
            if (cookiesHeader != null) {
                for (String cookie : cookiesHeader) {
                    try {
                        cookieManager.getCookieStore().add(null, HttpCookie.parse(cookie).get(0));
                    } catch( Exception e) {
                        e.printStackTrace();
                    }
                }
            }
            if (cookieManager.getCookieStore().getCookies().size() > 0) {
                for (HttpCookie ck : cookieManager.getCookieStore().getCookies()) {
                    System.out.println("Cookie name: " + ck.getName());
                    System.out.println("Domain: " + ck.getDomain());
                    System.out.println("Max age: " + ck.getMaxAge());
                    System.out.println("Server path: " + ck.getPath());
                    System.out.println("Is secured: " + ck.getSecure());
                    System.out.println("Cookie value: " + ck.getValue());
                    System.out.println("Cookie protocol version: " + ck.getVersion());
                }
            }
        } catch ( Exception e) {
            e.printStackTrace();
        }
    }
}
tm1701
  • 7,307
  • 17
  • 79
  • 168
  • @Sotirios Delimanolis, can you re-read the question? The answer via the 'duplicate question' does not help. Still no cookies are shown. – tm1701 Jul 06 '19 at 18:14
  • That's because the web page you're hitting doesn't set any cookies. – Sotirios Delimanolis Jul 06 '19 at 18:22
  • Also, please don't completely change your question. If you have to add details or explain your concern with a duplicate, _add_ those things to the existing post. – Sotirios Delimanolis Jul 06 '19 at 18:23
  • OK, I will take notice of that. ** The https://www.geocaching.com site has cookies. I use the in another app. And you can easily see them in your browser. – tm1701 Jul 06 '19 at 18:38
  • Thanx. OK, 1 goolge cookie works ;-) Why cannot I not see the cookies of the other sites? You can see them in the browser. – tm1701 Jul 06 '19 at 19:05
  • 1
    If I navigate to `https://nu.nl` on my browser, there are no `set-cookie` headers in the response. – Sotirios Delimanolis Jul 06 '19 at 19:07
  • Ah - am learning! Are there other cookies I can find? How can I find the other cookies? Please put your info in an answer and I will +1 / V. – tm1701 Jul 06 '19 at 19:10
  • @SotiriosDelimanolis I'm very confused about your roll back. I can understand removing the "not duplicate" declaration in the title, but I don't understand why you undid all the other modifications. – jpmc26 Jul 07 '19 at 12:25
  • @jpmc26 The edit fundamentally changed the question, by removing its original details. That's what I meant by _please don't completely change your question. If you have to add details or explain your concern with a duplicate, **add** those things to the existing post._ – Sotirios Delimanolis Jul 07 '19 at 14:18
  • @tjm1706 Cookies in the response are just `Set-Cookie` headers. You find them when they are present. I don't otherwise understand what you mean by _find the other cookies_. – Sotirios Delimanolis Jul 07 '19 at 14:19
  • Thank you. @Sotirios Delimanolis, please summarize your information as the answer and I can +1/V the answer. – tm1701 Jul 07 '19 at 15:09
  • @SotiriosDelimanolis I disagree that it "completely changed the question." It attempted to incorporate the advice of the duplicate to address the closure, to demonstrate that the result remain unchanged. (Whether that change was successful in invalidating the duplicate or not is another question, but here I am only discussing in terms related to whether rollback was warranted.) Furthermore, the main problem with changing the question is that it can invalidate answers, but there are no answers here to invalidate. I see no harm done by them and the potential for them to represent an improvement. – jpmc26 Jul 07 '19 at 16:42

0 Answers0