我需要:iOS的窗体身份验证
1)对验证表单验证服务
2)保存关闭饼干饼干与
3)调用Web服务,我有一段时间试图让这个工作。有没有人有这样的运气?这似乎是一个非常常见的操作。
这里是我的代码:
命中验证网址,并获得饼干:
- (IBAction)buttonGetCookieTap:(id)sender {
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:self.cookieURL];
[request setHTTPShouldHandleCookies:YES];
[request setHTTPMethod:@"POST"];
self.urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- (void)connection: (NSURLConnection *)connection didReceiveResponse: (NSURLResponse *)response
{
NSHTTPURLResponse *HTTPResponse = (NSHTTPURLResponse *)response;
NSDictionary *fields = [HTTPResponse allHeaderFields];
self.cookie = [fields valueForKey:@"Set-Cookie"];
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
NSURLCredential *credential = [NSURLCredential credentialWithUser:@"blah"
password:@"blah"
persistence:NSURLCredentialPersistenceForSession];
[[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
}
现在调用验证服务与cookie:
- (IBAction)buttonCallServiceTap:(id)sender {
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:self.dataURL];
[request addValue:self.cookie forHTTPHeaderField:@"Cookie"];
self.urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
编辑:对不起,我有问题是,我得到一个纯粹的cookie,只有会话ID,但在服务器端cookie看起来很好。任何人都可以验证我的Cookie抓取代码没有问题吗?我已经尝试了很多变化,并且有同样的问题。谢谢!