使用authenticate url在iphone中进行XML解析

时间:2011-11-11 12:00:13

标签: iphone nsxmlparser

我正在进行xml解析,而我的网址是用户名和密码验证网址。

当我将此网址放入浏览器时,它会要求我输入用户名和密码。

我想使用 NSXMLParser 在iphone中解析此网址。

对于解析,我使用下面的代码,但对我来说,它回复了 parseErrorOccurred 错误。

NSString *UploadCardDesign=kLOGIN_URL;
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:UploadCardDesign]] autorelease];

[request setHTTPMethod: @"GET"];
[request setHTTPBody:nil]; 

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
//NSString *strRequ=[[NSString alloc] initWithData:returnData encoding:nsen]
NSString *data=[[NSString alloc]initWithData:returnData encoding:NSUTF8StringEncoding];

用于解析:

xmlParser = [[NSXMLParser alloc] initWithData:returnData];
[xmlParser setDelegate:self];
[xmlParser parse];

请帮助我做同样的事情,或建议我做些什么来取得好成绩。

1 个答案:

答案 0 :(得分:2)

实现此委托功能以进行身份​​验证....

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
 if ([challenge previousFailureCount] == 0)
 {
  NSLog(@"received authentication challenge");

  NSURLCredential *newCredential;
  newCredential=[NSURLCredential credentialWithUser:@"root"
             password:@"rootpassword"
             persistence:NSURLCredentialPersistenceForSession];


  NSLog(@"credential created");

  [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];

  NSLog(@"responded to authentication challenge");

 }
 else
 {
  NSLog(@"previous authentication failure");

  UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Authentication Failure" 
              message:@"Website did not accept the username and password."
                delegate:nil
             cancelButtonTitle:@"OK" 
             otherButtonTitles:nil];
  [alert show];
  [alert release];
 }
}