Je crée une application. J'envoie les informations de connexion en utilisant la méthode HTTP POST et la réponse que je reçois du serveur est au format HTML. Comment puis-je analyser ce HTML et ajouter des méthodes différentes pour la succession ou l'échec? Ce que j'essaye de réaliser est, en cas d'échec de connexion, il devrait montrer le message utilisant UIAlerView et en cas de connexion réussie, l'application devrait changer la vue avec l'animation. :)Analyse de la réponse HTML - Application iPhone
Le code J'utilise en ce moment:
- (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];
}
Voilà une question générale. Tout d'abord, l'analyse HTML n'est probablement pas la meilleure façon d'y parvenir - une chose de type SOAP serait préférable, si vous pouvez le faire, car il serait préférable de découpler le login des aléas du HTML. Deuxièmement, POSTing le mot de passe nu est une mauvaise idée sauf si vous utilisez HTTPS. Troisièmement, voir cette question: http://stackoverflow.com/questions/405749/parsing-html-on-the-iphone – Robert
Merci Robert, pourquoi ne poses-tu pas cela comme une réponse pour que je puisse le sélectionner. :) –