使用 NSHTTPCookieStorage 访问会话 cookie
我正在访问服务器的安全信息,它会根据请求向应用程序发送一堆 cookie。问题是有些 cookie 仅限会话,当我使用时:
[NSHTTPCookie requestHeaderFieldsWithCookies:[[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString:theCookie]]
它不会返回名称为 JSESSIONID 的会话 cookie 并导致问题。如果我 NSLog 完整的 NSHTTPCookieStorage 它会显示会话会话,因此它们就在那里,我只是找不到从存储中检索它们的方法。另外,我查看了 cookie plist,会话 cookie 没有存储在那里,但我认为这只是因为它们是基于会话的。
任何帮助表示赞赏。
编辑:这是我请求所有 cookie 时得到的片段:
<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:[]>
现在,当我根据上述 cookie 中的 URL 请求 cookie 时,第一个会返回,第二个不会返回。
*请注意,对于必须删除某些项目感到抱歉,它们符合预期并且与问题无关
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我遇到了这个问题,原因是 NSHTTPCookieDomain。
cookie 必须具有相同的域
必须是
我只是更改它,并放置相同的域和路径并且有效
我的 JSESSIONID:
I had that problem, the reason is because the NSHTTPCookieDomain.
the cookies must have the same Domain
must be
i just change that, and put the same Domain and path and works
My JSESSIONID:
请将您的问题分解,首先检查是否
返回您认为应该返回的内容。如果没有,请仔细查看
theCookie
的值。Please break down your problem in pieces, first check if
returns what you think it should return. If not, take a closer look at the value of
theCookie
.您传递给
-cookiesForURL:
的实际 URL 是什么?如果 URL 没有与 JSESSIONID cookie 条目中指定的路径匹配的路径(例如,http://example.com/path
),则-cookiesForURL:
方法不会返回它。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.