Access session cookies using NSHTTPCookieStorage

I access server-side secure information and send a bunch of cookies to the application on request. The problem is that some of the cookies are only sessions, and when I use:

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

it does not return session names named JSESSIONID and causes problems. If I NSLog is a full NSHTTPCookieStorage, it displays the session files so that they are there, I just cannot find a way to retrieve them from the repository. Also, I looked at the plie cookies and the session cookies are not stored there, but I assume this is because they are based on the session.

Any help is appreciated.

Edit: this is a snippet of what I get when I request 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 request cookies based on the URL of the above cookies, the first is returned and the second is not.

* note that you need to remove certain elements, they are as expected and are not relevant

+3
source share
3 answers

I had this problem, the reason is NSHTTPCookieDomain. cookie must have the same domain

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

it should be

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

I just change it and put the same Domain and path and work

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>
+2
source

Please break your problem in pieces, first check

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

, , , . , theCookie.

0

What is the actual URL you are going to -cookiesForURL:? If the URL does not have a path that matches the one specified in your JSESSIONID file entry (for example, http://example.com/path), the method -cookiesForURL:will not return it.

0
source

Source: https://habr.com/ru/post/1754935/


All Articles