我正在做xml解析,而我的url是认证url与用户名和密码。
当我把这个url输入浏览器时,它会问我登录的用户名和密码。
我想在iphone中使用NSXMLParser解析这个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];
}
}