Iphone 如何获取cookie并将其用于POST(iOS)等其他请求?
我的问题是,我每次都必须登录才能进行web服务,如发布链接或上传图片。Philipe回答说,对于每个请求,我必须使用cookies,而不是登录过程。我找到了获取cookie的方法:Iphone 如何获取cookie并将其用于POST(iOS)等其他请求?,iphone,ios,cookies,Iphone,Ios,Cookies,我的问题是,我每次都必须登录才能进行web服务,如发布链接或上传图片。Philipe回答说,对于每个请求,我必须使用cookies,而不是登录过程。我找到了获取cookie的方法: - (void)getCookies { NSHTTPURLResponse * response; NSError * error; NSMutableURLRequest *request; request = [[NSMutableURLRequest alloc] init
- (void)getCookies {
NSHTTPURLResponse * response;
NSError * error;
NSMutableURLRequest *request;
request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://MyWebsite.com/login.php"]
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:120];
NSData * data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]);
NSArray * all = [NSHTTPCookie cookiesWithResponseHeaderFields:[response allHeaderFields] forURL:[NSURL URLWithString:@"http://MyWebsite.com/login.php"]];
NSLog(@"%d", all.count);
for (NSHTTPCookie *cookie in all) {
NSLog(@"Name: %@ : Value: %@", cookie.name, cookie.value);
NSLog(@"Comment: %@ : CommentURL: %@", cookie.comment, cookie.commentURL);
NSLog(@"Domain: %@ : ExpiresDate: %@", cookie.domain, cookie.expiresDate);
NSLog(@"isHTTPOnly: %c : isSecure: %c", cookie.isHTTPOnly, cookie.isSecure);
NSLog(@"isSessionOnly: %c : path: %@", cookie.isSessionOnly, cookie.path);
NSLog(@"portList: %@ : properties: %@", cookie.portList, cookie.properties);
NSLog(@"version: %u", cookie.version);
}
}
我还发现了使用这些cookie的代码,但我不确定如何使用它:
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookies];
以下是我的发布方法,我使用的是RestKit API:
- (IBAction)addLinkPressed:(UIButton *)sender {
[RKClient clientWithBaseURLString:@"http://MyWebsite.com"];
NSDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:
self.linkField.text, @"url",
self.linkTitleField.text, @"title",
self.linkSummaryField.text, @"summary",
nil];
RKRequest *request = [[RKClient sharedClient] post:@"/send_link.php" params:params delegate:self];
[request setUserData:@"sendLink"];
}
问题:我应该存储cookie的哪个属性以将其用于登录信息,以及我应该将其放在代码中的什么位置
[request setHTTPMethod:@"POST"];