gaurav
gaurav

Reputation: 347

XML parsing in iphone with authenticate url

I am doing xml parsing while my url is authenticate url with username and password.

When I put this url into a browser it asks me for username and password for login.

I want to parse this url in iphone using NSXMLParser.

For parsing, I'm using the below code but for me it is giving back a parseErrorOccurred error.

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];

And for parsing:

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

Please help me out for the same or advise what i do for good results.

Upvotes: 4

Views: 773

Answers (1)

Ankit Srivastava
Ankit Srivastava

Reputation: 12405

Implement this delegate function for authentication....

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

Upvotes: 2

Related Questions