1
我正在创建一个应用程序。我使用HTTP POST方法发送登录信息,并且我从服务器获得的回复是HTML格式。我如何解析HTML并添加不同的继承或失败方法?我试图实现的是,在登录失败时,它应该使用UIAlerView显示消息,并且在成功登录后,应用程序应该使用动画更改视图。 :)解析HTML响应 - iPhone App
我现在使用的代码:
- (IBAction) loginButton: (id) sender {
indicator.hidden = NO;
[indicator startAnimating];
loginbutton.enabled = NO;
// Create the username and password string.
// username and password are the username and password to login with
NSString *postString = [[NSString alloc] initWithFormat:@"username=%@&password=%@",userName, password];
// Package the string in an NSData object
NSData *requestData = [postString dataUsingEncoding:NSASCIIStringEncoding];
// Create the URL request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString:@"http://localhost/dologin.php"]]; // create the URL request
[request setHTTPMethod: @"POST"]; // you're sending POST data
[request setHTTPBody: requestData]; // apply the post data to be sent
// Call the URL
NSURLResponse *response; // holds the response from the server
NSError *error; // holds any errors
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse:&response error:&error]; // call the URL
/* If the response from the server is a web page, dataReturned will hold the string of the HTML returned. */
NSString *dataReturned = [[NSString alloc] initWithData:returnData encoding:NSASCIIStringEncoding];
alertWithOkButton = [[UIAlertView alloc] initWithTitle:@"Status..." message:[NSString stringWithFormat:@"%@",dataReturned] delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil];
[alertWithOkButton show];
[alertWithOkButton release];
}
这是一个广泛的问题。首先,解析HTML可能不是这里的一种方式 - 如果你能做到这一点,SOAP类型的东西会更可取,因为你最好将登录与HTML的变幻莫测分开。其次,除非您使用HTTPS,否则发布纯密码不是好主意。第三,看到这个问题:http://stackoverflow.com/questions/405749/parsing-html-on-the-iphone – Robert 2010-12-08 08:17:24