NSURLErrorDomain错误-1012

我需要parsing密码保护的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); 

我得到了以下回应

 data == <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>401 Authorization Required</title> </head><body> <h1>Authorization Required</h1> <p>This server could not verify that you are authorized to access the document requested. Either you supplied the wrong credentials (eg, bad password), or your browser doesn't understand how to supply the credentials required.</p> <p>Additionally, a 401 Authorization Required error was encountered while trying to use an ErrorDocument to handle the request.</p> </body></html> 

连接错误==错误域= NSURLErrorDomain代码= -1012“操作无法完成(NSURLErrorDomain错误-1012。)”UserInfo = 0x6e51b90 {NSErrorFailingURLKey = http://example.com/api/ID/password/ xml / ,

任何帮助表示赞赏!

在使用NSURLRequest访问服务器时, NSURLConnection的委托方法提供了一种身份validation受到挑战得到通知方法

以下示例将显示一种处理要求凭据的URL的方法。

 - (void)CallPasswordProtectedWebService { NSURL *url = [NSURL URLWithString:@"your url"]; NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url]; urlConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self]; } //when server asks the credentials this event will fire - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { if ([challenge previousFailureCount] == 0) { NSURLCredential *newCredential; newCredential=[NSURLCredential credentialWithUser:@"username" password:@"password" persistence:NSURLCredentialPersistenceNone]; [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge]; } else { [[challenge sender] cancelAuthenticationChallenge:challenge]; } } //when connection succeeds this event fires - (void)connectionDidFinishLoading:(NSURLConnection *)connection { NSLog(@"Successfuly connected "); } //when connection fails the below event fires - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { NSLog(@"connection failed"); } 

希望这可以帮助

在我的情况下, 问题是由所有HTTPS调用需要客户端证书的防火墙/代理引起的 。 我通过提供NSURLProtocol子类来解决此问题,以处理身份validation挑战并提供此证书。

 [NSURLProtocol registerClass:self]; //implement these methods - (void)didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge; - (void)resolveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge withCredential:(NSURLCredential *)credential; 

使用Xcode 6,我的应用程序不会在IOS 8平台上下载大量的文件,尽pipe它在早期的IOS上运行的很好。 我的代码本质上就像下面的代码一样,但是当身份validation挑战来临时,错误一直在出现。

我不得不改变代码

  if ([challenge previousFailureCount] == 0) { NSURLCredential *newCredential; newCredential=[NSURLCredential credentialWithUser:@"username" password:@"password" persistence:NSURLCredentialPersistenceForSession]; [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge]; } else { NSURLCredential *newCredential; newCredential=[NSURLCredential credentialWithUser:@"username" password:@"password" persistence:NSURLCredentialPersistenceForSession]; [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge]; } 

这当然会使IF语句变得多余(可以删除)。 我没有任何解释,但即使在持续的会话中也不回应用户名和密码的挑战,导致错误(-1012)。 现在它和早期的IOS一样工作