如何为 NSHTTPURLResponse 获取正确的 Set-Cookie 标头?
我想使用以下代码登录到一个网站,该网站以以下方式返回其 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该行使系统为您处理 cookie。
This line causes the system to handle cookies for you.