在 iPhone 上管理 HTTP Cookie

发布于 2024-08-17 07:00:57 字数 200 浏览 1 评论 0原文

我想为 iPhone 移植一个使用 mechanize 的 python 应用程序。此应用程序需要登录网页并使用站点 cookie 转到该站点上的其他页面以获取一些数据。

在我的 python 应用程序中,我使用 mechanize 进行自动 cookie 管理。 Objective C 有类似的东西可以移植到 iPhone 上吗?

感谢您的任何帮助。

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.

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?

Thanks for any help.

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

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

发布评论

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

评论(3

小帐篷 2024-08-24 07:00:57

NSURLConnection 为您免费提供 cookie 管理。来自 URL 加载系统编程指南

URL加载系统自动发送任何适合NSURLRequest的存储cookie。除非请求指定不发送cookie。同样,根据当前的 cookie 接受策略接受 NSURLResponse 返回的 cookie。

NSURLConnection gives you cookie management for free. From the URL Loading System Programming Guide:

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.

从来不烧饼 2024-08-24 07:00:57

您可以使用 NSURLConnection 类执行 HTTP 请求来登录网站并检索 cookie。要执行请求,只需创建一个 NSURLConnection 实例并为其分配一个委托对象。

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com/"]];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];

然后,实现委托方法。

- (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
}

保留或复制 cookie 字符串。当您想要执行另一个请求时,请将其添加到 NSURLRequest 实例的 HTTP 标头中。

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com/"]];
[request addValue:cookie forHTTPHeaderField:@"Cookie"];

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.

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.

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com/"]];
[request addValue:cookie forHTTPHeaderField:@"Cookie"];
风筝在阴天搁浅。 2024-08-24 07:00:57

@zonble 的答案仅适用于我的本地主机;
如果服务器不在本地运行,则 NSDictionary *fields = [HTTPResponse allHeaderFields] 中缺少“Set-Cookie”标头;

最后我发现@benzado的解决方案工作正常。
另请参阅:iphone nsurlconnection 读取 cookies @Tal Bereznitskey 的回答。

@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];

Finally I found @benzado's solution works fine.
Also see: iphone nsurlconnection read cookies @Tal Bereznitskey's answer.

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