我刚刚开始 iOS 编程,但无法理解这一点。我正在尝试连接到一个有登录名的网站。我正在使用NSURLConnection
和 POST,并发送我的登录凭据。
凭据是正确的并且connectionDidFinishLoading:
被调用但它没有完全登录到站点..登录部分完成..我在查看响应时发现了这一点..根据我的理解,“connectionDidFinishLoading”仅在之后调用连接完成..我肯定在这里遗漏了一些东西..我的代码在下面..请指教..
- (IBAction)connect:(id)sender {
// Login to the website
urlRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.mywebsite.com/login"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
NSString *post = @"username=myname&password=mypassword";
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
[urlRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[urlRequest setHTTPBody:postData];
// Start the connection request
urlConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
}
在这里,当我记录响应时,它是不完整的..它登陆在最终成功页面之间的页面..所以身份验证部分完成..我需要包括任何延迟吗?请帮忙..
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"Connection success.");
NSURLResponse *responseTest;
NSError *error;
NSData *responseDataTest = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&responseTest error:&error];
NSString *responsestring = [[NSString alloc] initWithData:responseDataTest encoding:NSASCIIStringEncoding];
NSLog(@"Recieved response.. %@",responsestring);
}
提前致谢..