使用 NSHTTPCookieStorage 访问会话 cookie

发布于 2024-09-09 20:18:13 字数 1118 浏览 5 评论 0原文

我正在访问服务器的安全信息,它会根据请求向应用程序发送一堆 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你与清晨阳光 2024-09-16 20:18:13

我遇到了这个问题,原因是 NSHTTPCookieDomain。
cookie 必须具有相同的域

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

必须是

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

我只是更改它,并放置相同的域和路径并且有效

我的 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>

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>
寄离 2024-09-16 20:18:13

请将您的问题分解,首先检查是否

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

返回您认为应该返回的内容。如果没有,请仔细查看 theCookie 的值。

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.

乖乖哒 2024-09-16 20:18:13

您传递给 -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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文