2

我正在尝试制作一个应用程序来保存来自特定网站(https://www.airservicesaustralia.com/naips/Account/LogOn)的页面,并且我正在尝试让应用程序为用户登录他们保存的详细信息在应用程序中。我想尝试让它在后台发布登录数据。我一直在尝试使用 NSMutableURLRequest 但没有运气...有关如何在后台登录该网站的任何建议?

谢谢!

4

2 回答 2

3

您应该使用具有某种开发模式的浏览器(例如启用开发者模式的 Chrome 或 Safari)并读取作为您登录时发生的 POST 或 GET 请求的一部分的变量(在这种情况下,当您按下时发生的请求Submit) .

在您自己的请求中使用相同的变量。

于 2013-09-15T12:32:34.993 回答
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;

}
于 2013-09-15T12:40:38.380 回答