我是iphone网络编程的新手。我正在编写一个具有登录屏幕的应用程序。
我正在使用ASIHTTPREQUEST,这是我想要登录的链接myurl.com/index.php。在chrome中,当我使用用户名和密码登录系统时,我从开发者控制台查看了此url帖子到myurl.com/ajax/login.php。
无论如何,当我在应用程序中输入用户名和密码后单击登录按钮时,它不会登录系统。它会打印失败:1(当我写myurl.com/ajax/login.php时,失败:1 在浏览器中看到。)
我的问题的详细说明:
“myurl.com/index.php中有一个登录界面。在网络浏览器中,如果我写了我的用户名和密码,它会发布到myurl.com/ajax/login.php(来自开发者控制台)。无论如何,在应用程序中我写了myurl.com/index.php作为网址,但当我请求它在控制台中打印该页面的html元素。如果我写myurl.com/ajax/login.php作为网址,它打印失败:1在控制台中。(通常在网络浏览器中我写了myurl.com/ajax/login.php,它给出了失败:1)“
编辑:我已经解决了问题,“forKey:@”用户名“需要等于”forKey:@“ID”和“forKey:@”密码“需要等于”forKey:@“PWD” 。因为在开发人员控制台中用户名和密码作为ID和PWD返回。感谢您的评论和回答。
这是代码:
-(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
}
对不起我的英文,有一些关于这个主题的例子,但我没有解决这个问题。谢谢你的建议。
答案 0 :(得分:0)
你试过这个吗? ASI包括一种进行基本身份验证的方法
[request setUsername:studentid.text];
[request setPassword:starspassword.text];
向下滚动到处理HTTP身份验证