小编Piy*_*ush的帖子

NSURLErrorDomain错误-1012

我需要从受密码保护的URL解析xml文件,我尝试了以下操作

NSURLCredential *credential = [NSURLCredential credentialWithUser:@"admin"  password:@"123456" persistence:NSURLCredentialPersistenceForSession];
NSURLProtectionSpace *protectionSpace = [[NSURLProtectionSpace alloc]
                                         initWithHost:@"xyz.com"
                                         port:80
                                         protocol:@"http"
                                         realm:nil
                                         authenticationMethod:NSURLAuthenticationMethodDefault];  
[[NSURLCredentialStorage sharedCredentialStorage] setCredential:credential
                                             forProtectionSpace:protectionSpace];    
url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"GET"];
[request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
NSURLResponse *response;
NSError *error;
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse: &response error: &error];   
 NSString *dataStr=[[NSString alloc]initWithData:returnData encoding:NSUTF8StringEncoding]; 
 NSLog(@"data == %@\n error in connecting == %@",dataStr,error);
Run Code Online (Sandbox Code Playgroud)

我收到了以下回复

data == <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>401 Authorization Required</title> …
Run Code Online (Sandbox Code Playgroud)

iphone nsurlconnection nsurlrequest nsurlcredential ios

10
推荐指数
1
解决办法
4万
查看次数