3
votes

I'm accessing a server's secure information and it sends a bunch of cookies to the App on request. The problem is some of the cookies are session only and when I use:

[NSHTTPCookie requestHeaderFieldsWithCookies:[[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString:theCookie]]

it doesn't return the session ones with the name JSESSIONID and causes issues. If I NSLog the full NSHTTPCookieStorage it displays the session ones so they are there, I just can't find a way to retrieve them out of the storage. Also, I've had a look through the cookie plist and the session cookies aren't stored there but I assume this is just due to them being session based.

Any help is appreciated.

Edit: This is a snippet of what I get when I ask for all cookies:

<NSHTTPCookie version:0 name:@\"TheNameOfTheCookie\" value:@\"A variable number\" expiresDate:@\"(null)\" created:@\"301196844.000000\" sessionOnly:TRUE domain:@\"THE URL\" path:@\"/\" secure:FALSE comment:@\"(null)\" commentURL:@\"(null)\" portList:[]>

<NSHTTPCookie version:0 name:@\"JSESSIONID\" value:@\"A variable number\" expiresDate:@\"(null)\" created:@\"301196866.000000\" sessionOnly:TRUE domain:@\"The Same URL as above\" path:@\"/path\" secure:FALSE comment:@\"(null)\" commentURL:@\"(null)\" portList:[]>

Now when I ask for the cookies based on the URL in the above cookies, first one gets returned, second one doesn't.

*note sorry about having to remove certain items, they are as expected and are not relevant for the question

3
without the actual url's or derived values (for my part tr/a-z/z-a/ them...) we can only guess: could it be a case problem? Some webservers are case-insensitive, while the rest of the world aren't. Could you provide a self contained example which shows the behavior?mvds
Well its not a server problem as the actual Cookie isn't being returned from storage, I think it might be a naming problem as there are multiple entries of JSESSIONID. I even manually entered a cookie with similar details but different url and couldn't retrieve it either. Had the same name though, JSESSIONID.Rudiger

3 Answers

2
votes

I had that problem, the reason is because the NSHTTPCookieDomain. the cookies must have the same Domain

.... domain:@\"THE URL\" path:@\"/\" ....
.... domain:@\"The Same URL as above\" path:@\"/path\" ....

must be

.... domain:@\"NAME-DOMAIN" path:@"/" ....

i just change that, and put the same Domain and path and works

My JSESSIONID:

<NSHTTPCookie version:0 name:"JSESSIONID" value:"7C9B0...........EB5" expiresDate:(null) created:2012-07-06 16:14:26 +0000 (3.63284e+08) sessionOnly:TRUE domain:"FOO" path:"/" isSecure:FALSE>
0
votes

Please break down your problem in pieces, first check if

[[NSHTTPCookieStorage sharedHTTPCookieStorage]
    cookiesForURL:[NSURL URLWithString:theCookie]]

returns what you think it should return. If not, take a closer look at the value of theCookie.

0
votes

What's the actual URL you're passing to -cookiesForURL:? If the URL doesn't have a path matching the one specified in your JSESSIONID cookie entry (e.g., http://example.com/path), the -cookiesForURL: method won't return it.