objective-c 在 iPhone 上管理 HTTP Cookie
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2053568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Managing HTTP Cookies on iPhone
提问by dan
I want to port a python app that uses mechanize for the iPhone. This app needs to login to a webpage and using the site cookie to go to other pages on that site to get some data.
我想移植一个在 iPhone 上使用 mechanize 的 python 应用程序。此应用程序需要登录网页并使用站点 cookie 转到该站点上的其他页面以获取一些数据。
With my python app I was using mechanize for automatic cookie management. Is there something similar for Objective C that is portable to the iPhone?
使用我的 python 应用程序,我使用 mechanize 进行自动 cookie 管理。可移植到 iPhone 的 Objective C 是否有类似的东西?
Thanks for any help.
谢谢你的帮助。
回答by Steve Madsen
NSURLConnectiongives you cookie management for free. From the URL Loading System Programming Guide:
NSURLConnection免费为您提供 cookie 管理。来自URL 加载系统编程指南:
The URL loading system automatically sends any stored cookies appropriate for an NSURLRequest. unless the request specifies not to send cookies. Likewise, cookies returned in an NSURLResponse are accepted in accordance with the current cookie acceptance policy.
URL 加载系统会自动发送任何适合 NSURLRequest 的存储 cookie。除非请求指定不发送 cookie。同样,根据当前的 cookie 接受策略接受在 NSURLResponse 中返回的 cookie。
回答by zonble
You can use the NSURLConnection class to perform a HTTP request to login the website, and retrieve the cookie. To perform a request, just create an instance of NSURLConnection and assign a delegate object to it.
您可以使用 NSURLConnection 类执行 HTTP 请求以登录网站,并检索 cookie。要执行请求,只需创建一个 NSURLConnection 实例并为其分配一个委托对象。
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com/"]];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
Then, implement a delegate method.
然后,实现一个委托方法。
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
NSDictionary *fields = [httpResponse allHeaderFields];
NSString *cookie = [fields valueForKey:@"Set-Cookie"]; // It is your cookie
}
Retain or copy the cookie string. When you want to perform another request, add it to your HTTP header of your NSURLRequest instance.
保留或复制 cookie 字符串。当您想执行另一个请求时,请将其添加到您的 NSURLRequest 实例的 HTTP 标头中。
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com/"]];
[request addValue:cookie forHTTPHeaderField:@"Cookie"];
回答by Ranger Way
@zonble's answer only works in my localhost; If the server is not running locally, the "Set-Cookie" header is missing from the NSDictionary *fields = [HTTPResponse allHeaderFields];
@zonble 的答案仅适用于我的本地主机;如果服务器不在本地运行,则 NSDictionary *fields = [HTTPResponse allHeaderFields] 中缺少“Set-Cookie”标头;
Finally I found @benzado's solution works fine. Also see: iphone nsurlconnection read cookies@Tal Bereznitskey's answer.
最后我发现@benzado 的解决方案工作正常。另请参阅:iphone nsurlconnection read cookies@Tal Bereznitskey 的回答。

![objective-c 使用来自 [[NSBundle mainBundle] resourcePath] 的路径](/res/img/loading.gif)