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