【问题标题】:Parsing HTML Response - iPhone App解析 HTML 响应 - iPhone 应用程序
【发布时间】:2010-12-08 07:51:35
【问题描述】:

我正在创建一个应用程序。我正在使用 HTTP POST 方法发送登录信息,而我从服务器获得的回复是 HTML 格式。如何解析该 HTML 并添加不同的继承或失败方法?我想要实现的是,在登录失败时,它应该使用 UIAlerView 显示消息,并且在成功登录后,应用程序应该用动画更改视图。 :)

我现在使用的代码:

- (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,否则发布裸密码是个坏主意。第三,看到这个问题:stackoverflow.com/questions/405749/parsing-html-on-the-iphone
  • 谢谢罗伯特,您为什么不将此作为答案发布,以便我选择它。 :)

标签: iphone cocoa-touch login httpwebrequest


【解决方案1】:

我所做的正是我使用了 HTMLparser 类。如果您以 HTML 格式获得响应,则此类非常有用。

【讨论】:

    【解决方案2】:
    -(void)startParsingForLogin:(NSString *)userIdStr Password:(NSString *)passwordStr 
    {
    
    NSString *urlString = [NSString stringWithFormat:@"http://www.example.com/loginxml.php?username=%@&password=%@",userIdStr,passwordStr];
    ////////NSLog(@"urlString : %@",urlString);
    NSURL *xmlURL = [NSURL URLWithString:urlString];
    
    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] initWithURL:xmlURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0]autorelease];
    
    NSURLResponse *returnedResponse = nil;
    NSError *returnedError = nil;
    NSData *itemData  = [NSURLConnection sendSynchronousRequest:request returningResponse:&returnedResponse error:&returnedError];
    //NSString *itemString = [[[NSString alloc] initWithBytes:[itemData bytes] length:[itemData length] encoding:NSUTF8StringEncoding]autorelease];
    
    //////NSLog(@"itemString : %@",itemString);
    
    
    xmlParser = [[NSXMLParser alloc] initWithData:itemData];        
    [xmlParser setDelegate:self];
    
    [xmlParser parse];
    
    }
    - (void)parserDidStartDocument:(NSXMLParser *)parser
    {
    ////////NSLog(@"parserDidStartDocument");
    }
    
    - (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
    {
    ////////NSLog(@"parseErrorOccurred");
    NSString * errorString = [NSString stringWithFormat:@"Error (Error code %i )", [parseError code]];
    UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"Error loading data" message:errorString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [errorAlert show];
    [errorAlert release];
    }
    
    - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:    (NSDictionary *)attributeDict
    {
    ////NSLog(@"didStartElement");
    ////NSLog(@"elementName : %@",elementName);
    ////NSLog(@"namespaceURI : %@",namespaceURI);
    ////NSLog(@"qualifiedName : %@",qualifiedName);
    ////NSLog(@"attributeDict : %@",attributeDict);
    [registerNewArr addObject:attributeDict];
    
    }
    - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
    {
    ////NSLog(@"foundCharacters");
    }
    
    - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
    {
    /////NSLog(@"didEndElement");   
    }
    - (void)parserDidEndDocument:(NSXMLParser *)parser
    {
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-06
      • 2011-12-22
      • 2011-11-16
      • 1970-01-01
      • 1970-01-01
      • 2016-10-12
      • 2011-04-12
      • 2012-06-06
      相关资源
      最近更新 更多