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

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

当我把这个网址放到浏览器中时,它会要求我输入用户名和密码进行登录.

我想使用NSXMLParser在iphone中解析这个url.

对于解析,我使用下面的代码,但对我来说,它回馈了一个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];

请帮助我相同或建议我做的好结果.

最佳答案 实现此委托功能进行身份验证….

- (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];
 }
}
点赞