因此,我尝试使用NSMutableURLRequest登录我的网站,该via通过帖子提供凭据。作为一个noobie,我有一些关于这个方法的功能的问题,以确保我理解它。
NSString *post = [NSString stringWithFormat:@"username=%@&password=%@&TARGET=%@",LoginId,LoginPwd,target];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setHTTPShouldHandleCookies:YES];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://somelogin.mycomp.verify.fcc"]]];
//[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
[request setHTTPBody:postData];
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];现在,这将自动处理任何通过委托方法的重定向,对吗?它还会在一路上收集任何饼干吗?另外,我是否应该使用异步请求呢?谢谢!
更新:所以我推断一些cookie被请求和后续重定向忽略了。有人知道为什么会发生这种情况吗?
发布于 2010-09-09 01:30:33
您可以在同步登录时执行以下操作...代码并不完美,但这是大体思路。
请在此处查找文档
// credentials set here
NSURLCredential *creds = [NSURLCredential credentialWithUser:LoginId password:LoginPwd
persistence:NSURLCredentialPersistenceForSession];
NSURLProtectionSpace *protectionedSpace = [[NSURLProtectionSpace alloc]
initWithHost:@"mycomp.verify.fcc"
port:8443
protocol:@"https"
realm:nil
authenticationMethod:nil];
[[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:creds
forProtectionSpace:protectionedSpace];
// removed credentials from line...
NSString *post = [NSString stringWithFormat:@"TARGET=%@",target];下面是一个异步完成此操作的教程
http://iosdevelopertips.com/networking/handling-url-authentication-challenges-accessing-password-protected-servers.html
https://stackoverflow.com/questions/3670029
复制相似问题