Iphone 使用asihttprequest登录网站
当我试图登录到我制作的一个简单网页时,你在一个-(iAction){}中有代码 当我按下按钮时,它应该得到数据 这是我的密码Iphone 使用asihttprequest登录网站,iphone,asihttprequest,Iphone,Asihttprequest,当我试图登录到我制作的一个简单网页时,你在一个-(iAction){}中有代码 当我按下按钮时,它应该得到数据 这是我的密码 -(IBAction)fetchData:(id)sender { NSURL *url = [NSURL URLWithString:@"http://rssit.site90.com/login.php"]; ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; [request
-(IBAction)fetchData:(id)sender {
NSURL *url = [NSURL URLWithString:@"http://rssit.site90.com/login.php"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setRequestMethod:@"POST"];
[request addPostValue:@"" forKey:@"username"];
[request addPostValue:@"" forKey:@"password"];
[request setDelegate:self];
[request startAsynchronous];
NSLog(@"%d, %@", request.responseStatusCode, [request responseString]);
}
当我运行它时,它返回0,(null),我查看了asihttprequest的头文件,这意味着不需要身份验证?但那个网站上有一个登录名
我输入了一个用户名和密码,但我没有在这里列出它重写代码,如下所示,答对了
-(IBAction)fetchData:(id)sender
{
NSURL *url = [NSURL URLWithString:@"http://rssit.site90.com/login.php"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setRequestMethod:@"POST"];
[request addPostValue:@"" forKey:@"username"];
[request addPostValue:@"" forKey:@"password"];
[request setDelegate:self];
[request startAsynchronous];
//Add finish or failed selector
[request setDidFinishSelector:@selector(requestLoginFinished:)];
[request setDidFailSelector:@selector(requestLoginFailed:)];
NSLog(@"%d, %@", request.responseStatusCode, [request responseString]);
}
- (void)requestLoginFinished:(ASIHTTPRequest *)request
{
//Check response of request here and act accordingly
NSString *yourResponse = [request responseString]; //corrected here please change it to responseString
//Parse above response and check it.
}
- (void)requestLoginFailed:(ASIHTTPRequest *)request
{
//some error was there processing request
//Check error
NSError *error = [request error];
NSLog(@"Failed ---> %@",[error localizedDescription]);
}
当您发出请求时,您需要等待直到请求完成,尽管您发出的是异步请求,因为这是登录信息,您需要等待响应到来。您可能需要查找“异步”的含义;-)当我尝试由于未捕获的异常“NSInvalidArgumentException”而终止应用程序时,原因:“-[ASIFormDataRequest response]:未识别的选择器发送到instanceSorry abt,它应该是[request responseString]。我已经更新了我的答案。请再次检查。好的,当我这样做时,它所做的只是显示登录页面的html代码,但它不登录。它在NSLog中打印什么?您收到正确的响应了吗?对于http代码,我收到200,然后我只收到登录页面html代码,这正常吗?既然我有了登录凭据,我该如何使它前进到下一页?