Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle cookies in httpUrlConnection using cookieManager

I have a server request that returns multiple cookies, like that:

enter image description here

This is how I'm storing these cookies to the cookieManager:

HttpURLConnection connection = ... ; static java.net.CookieManager msCookieManager = new java.net.CookieManager(); msCookieManager.put(COOKIES_URI, connection.getHeaderFields()); 

This is how I'm adding these cookies to the next connection:

connection.setRequestProperty("Cookie",    msCookieManager.getCookieStore().get(COOKIES_URI).toString()); 

Is it the right way to get the cookies from the cookieManager?, I'm quite sure there is a better one...

like image 636
David Avatar asked Apr 22 '13 14:04

David


People also ask

How do I set cookies in Httpurlconnection?

openConnection(); Create a cookie string: String myCookie = "userId=igbrown"; Add the cookie to a request: Using the setRequestProperty(String name, String value); method, we will add a property named "Cookie", passing the cookie string created in the previous step as the property value.

How do I get cookies from Httpurlconnection?

Retrieving CookiesCreate an instance of the CookieManager class and set its CookiePolicy . Set this instance of the CookieManager as the default CookieHandler . Open a URLConnection to the website of your choice. Next, retrieve cookies from the underlying CookieStore by using the getCookies method.

What is Cookie Manager in Java?

CookieManager provides a concrete implementation of CookieHandler , which separates the storage of cookies from the policy surrounding accepting and rejecting cookies.


2 Answers

Ok, the right way to do it is just like that:

Get Cookies from response header and load them into cookieManager:

static final String COOKIES_HEADER = "Set-Cookie"; HttpURLConnection connection = ... ; static java.net.CookieManager msCookieManager = new java.net.CookieManager();  Map<String, List<String>> headerFields = connection.getHeaderFields(); List<String> cookiesHeader = headerFields.get(COOKIES_HEADER);  if (cookiesHeader != null) {     for (String cookie : cookiesHeader) {         msCookieManager.getCookieStore().add(null,HttpCookie.parse(cookie).get(0));     }                } 

Get Cookies from cookieManager and load them into connection:

if (msCookieManager.getCookieStore().getCookies().size() > 0) {     // While joining the Cookies, use ',' or ';' as needed. Most of the servers are using ';'     connection.setRequestProperty("Cookie",     TextUtils.join(";",  msCookieManager.getCookieStore().getCookies()));     } 
like image 125
David Avatar answered Oct 11 '22 09:10

David


I've been searching/trying for days to fix my issue: cannot access protected web resources even after logging in successfully

I created the same app on iOS and didn't have the same problem because NSUrlConnection did the cookie maintenance for us behind the scene. On Android, I tried manually adding cookie

connection.setRequestProperty("Cookie", "PHPSESSID=str_from_server") 

without any luck.

Finally I read this

and added the following 2 lines somewhere in the beginning of my app:

CookieManager cookieManager = new CookieManager(); CookieHandler.setDefault(cookieManager); 

and everything works fine now.

like image 30
Golden Thumb Avatar answered Oct 11 '22 08:10

Golden Thumb