我试图创建一个应用程序来保存来自特定网站(https://www.airservicesaustralia.com/naips/Account/LogOn)的网页,并且我试图让应用为用户登录,并提供他们在应用中保存的详细信息。我想尝试让它在后台发布登录数据。我一直在尝试使用NSMutableURLRequest,但没有运气......有关如何在后台登录此网站的任何建议吗?
谢谢!
答案 0 :(得分:3)
您应该使用具有某种开发模式的浏览器(例如启用了开发人员模式的Chrome或Safari)并读取登录时发生的POST或GET请求中的变量(在这种情况下,发生时发生的请求)你按Submit
)。
在您自己的请求中使用相同的变量。
答案 1 :(得分:2)
将其放入“登录”按钮操作
NSString *post = [[NSString alloc] initWithFormat:@"uname=%@&pwd=%@",usernameData,passwordData];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSURL *url = [NSURL URLWithString:@"http://www.yourlink.com/chckLogin.php"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
[theRequest setHTTPMethod:@"POST"];
[theRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPBody:postData];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if( theConnection )
{
indicator.hidden = NO;
[indicator startAnimating];
webData = [[NSMutableData data] retain];
}
else
{
NSLog(@"Internet problem maybe...");
}
然后有连接
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[webData setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[webData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
// show error
indicator.hidden = YES;
[indicator stopAnimating];
[connection release];
[webData release];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *loginStatus = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
greetingLabel.text = @"";
NSLog(@"after compareing data is %@", loginStatus);
if ([loginStatus isEqualToString:@"right"]) {
// right login
} else {
// wrong login
greetingLabel.hidden = NO;
greetingLabel.text = @"Incorrect username and/ or password.";
}
[loginStatus release];
[connection release];
[webData release];
indicator.hidden = YES;
}