java – URLConnection with Cookies?-ThrowExceptions

Exception or error:

I’m trying to make a URLConnection that supports cookies. According to the documentation I can use:

CookieManager cookieManager = new CookieManager();

I couldn’t get this code to work, then I saw this only works for API 9 (2.3). However, I don’t get an error using CookieManager in an older emulator, CookieManager exists, but can’t be constructed. Is there any way to make this work for earlier versions? I tried:

            URLConnection con = u.openConnection();

            con.setRequestProperty("Cookie", cookieManager.getInstance().getCookie(url););
            String addCookie = con.getHeaderField("Set-Cookie");
            if (addCookie!=null) {
                cookieManager.getInstance().setCookie(url, addCookie);

but this doesn’t work.

How to solve:

I was able to enable cookies using Ian Brown’s CookieManager class:

I renamed it to IansCookieManager, set a class variable _CM = new IansCookieManager, now it’s simple:

            URLConnection conn = u.openConnection();

Leave a Reply

Your email address will not be published. Required fields are marked *