Bạn có thể sử dụng lớp NSURLConnection để thực hiện yêu cầu HTTP để đăng nhập trang web và truy xuất cookie. Để thực hiện một yêu cầu, chỉ cần tạo một thể hiện của NSURLConnection và gán một đối tượng delegate cho nó.
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com/"]];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
Sau đó, triển khai phương thức đại biểu.
- (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
}
Giữ lại hoặc sao chép chuỗi cookie. Khi bạn muốn thực hiện một yêu cầu khác, hãy thêm nó vào tiêu đề HTTP của cá thể NSURLRequest của bạn.
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com/"]];
[request addValue:cookie forHTTPHeaderField:@"Cookie"];
Ứng dụng A * Python *? 'WWW :: Mechanize' là một thư viện Perl. –
Ngoài ra còn có một cổng python. http://wwwsearch.sourceforge.net/mechanize/ – dan