使用asihttprequest登录网站

时间:2012-03-29 13:15:30

标签: iphone asihttprequest

我试图登录我制作的简单网页,你有代码 - (IBACTION){}

当我按下按钮时,它应该得到数据

这是我的代码

-(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的头文件,这意味着不需要身份验证?但是该网站上有登录信息。

我输入的用户名和密码我只是没有在这里列出

1 个答案:

答案 0 :(得分:1)

重写代码如下和bingo ....

-(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]);
}

当你提出请求时,你需要等到请求完成,虽然你正在进行异步请求,因为它是登录信息,你需要等到响应来了。