2

我是 iphone 互联网编程的新手。我正在编写一个具有登录屏幕的应用程序。

我正在使用 ASIHTTPREQUEST,这是我想要登录的链接 myurl.com/index.php。在 chrome 中,当我使用用户名和密码登录系统时,我从开发者控制台查看了这个 url 发布到 myurl.com/ajax/login.php 的内容。

无论如何,当我在应用程序中输入用户名和密码后单击登录按钮时,它不会登录系统。它会打印FAIL:1 (当我写 myurl.com/ajax/login.php, FAIL:1在浏览器。)

我的问题的详细描述:

“在 myurl.com/index.php 中有一个登录屏幕。在网络浏览器中,如果我写下我的用户名和密码,它会发布到 myurl.com/ajax/login.php(来自开发者控制台)。无论如何,在应用程序我已将 myurl.com/index.php 写为 url 但当我请求它在控制台中打印该页面的 html 元素时。如果我将 myurl.com/ajax/login.php 写为 url,它会打印 FAIL:1在控制台中。(通常在网络浏览器中,我写了 myurl.com/ajax/login.php,它给出了 FAIL:1)“

编辑:我已经解决了问题,“forKey:@"username" 需要等于 "forKey:@"ID" 和 "forKey:@"password" 需要等于 "forKey:@"PWD"。因为在开发者控制台中,用户名和密码作为 ID 和密码返回。感谢您的评论和回答。

这是代码:

-(IBAction)login:(id)sender 
{
    NSURL *url = [[NSURL alloc] initWithString:@"https://myurl.com/index.php"];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request setTimeOutSeconds:120];
    [request setPostValue:studentid.text forKey:@"username"];
    [request setPostValue:starspassword.text forKey:@"password"];
    [request setDelegate:self];
    [request startAsynchronous];
    [url release];
}

-(void) requestFinished:(ASIHTTPRequest *)request 
{
    NSString *responseString = [request responseString];
    UIAlertView *alert= [[[UIAlertView alloc] initWithTitle:@"Success" message:@"http works" 
                                               delegate:self cancelButtonTitle:@"okay" otherButtonTitles:nil] autorelease];

    [alert show];
    NSLog(@"SUCCESS: %@", responseString);
    //Request succeeded
}

-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex         
{
}

-(void) requestFailed:(ASIHTTPRequest *)request 
{
    NSError *error = [request error];
    NSLog(@"ERROR: %@", [error localizedDescription]);

    UIAlertView *alert= [[[UIAlertView alloc] initWithTitle:@"fail" message:@"http fails" delegate:self cancelButtonTitle:@"okay" otherButtonTitles:nil]autorelease];
    [alert show];
    //Request failed
}

对不起我的英语,有一些关于这个话题的例子,但我没有解决这个问题。谢谢你的建议。

4

1 回答 1

0

你试过这个吗?ASI 包括一种进行基本身份验证的方法

[request setUsername:studentid.text];
[request setPassword:starspassword.text];

向下滚动到处理 HTTP 身份验证

http://allseeing-i.com/ASIHTTPRequest/How-to-use

于 2012-10-01T19:14:30.263 回答