如何为 NSHTTPURLResponse 获取正确的 Set-Cookie 标头?

发布于 2024-08-26 05:37:17 字数 1193 浏览 4 评论 0原文

我想使用以下代码登录到一个网站,该网站以以下方式返回其 cookie 信息:

Set-Cookie: 19231234
Set-Cookie: u2am1342340
Set-Cookie: owwjera

我使用以下代码登录到该网站,但最后的打印语句不输出任何有关“设置cookie”。 在 Snow Leopard 上,库似乎会自动获取该站点的 cookie,并且稍后发送的连接会设置正确的“cookie”标头。但在 leopard 上,它不会那样工作,那么这是“记住某些根 url 的 cookie”行为的触发器吗?

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:uurl]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"keep-live" forHTTPHeaderField:@"Connection"];
[request setValue:@"300" forHTTPHeaderField:@"Keep-Alive"];
[request setHTTPShouldHandleCookies:YES];

[request setHTTPBody:postData];
[request setTimeoutInterval:10.0];

NSData *urlData;
NSHTTPURLResponse *response;
NSError *error;
urlData = [NSURLConnection sendSynchronousRequest:request
                                returningResponse:&response
                                            error:&error];

NSLog(@"response dictionary %@",[response allHeaderFields]);

I want to use the following code to login to a website which returns its cookie information in the following manner:

Set-Cookie: 19231234
Set-Cookie: u2am1342340
Set-Cookie: owwjera

I'm using the following code to log in to the site, but the print statement at the end doesn't output anything about "set-cookie".
On Snow leopard, the library seems to automatically pick up the cookie for this site and later connections sent out is set with correct "cookie" headers. But on leopard, it doesn't work that way, so is that a trigger for this "remember the cookie for certain root url" behavior?

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:uurl]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"keep-live" forHTTPHeaderField:@"Connection"];
[request setValue:@"300" forHTTPHeaderField:@"Keep-Alive"];
[request setHTTPShouldHandleCookies:YES];

[request setHTTPBody:postData];
[request setTimeoutInterval:10.0];

NSData *urlData;
NSHTTPURLResponse *response;
NSError *error;
urlData = [NSURLConnection sendSynchronousRequest:request
                                returningResponse:&response
                                            error:&error];

NSLog(@"response dictionary %@",[response allHeaderFields]);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

樱花落人离去 2024-09-02 05:37:17
[request setHTTPShouldHandleCookies:YES];

该行使系统为您处理 cookie。

[request setHTTPShouldHandleCookies:YES];

This line causes the system to handle cookies for you.

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