【问题标题】:NSURLErrorDomain error -1012NSURLErrorDomain 错误 -1012
【发布时间】:2012-07-23 06:05:04
【问题描述】:

我需要从受密码保护的 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 (e.g., 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>

连接错误 == Error Domain=NSURLErrorDomain Code=-1012 "操作无法完成。(NSURLErrorDomain error -1012.)" UserInfo=0x6e51b90 {NSErrorFailingURLKey=http://example.com/api/ID/password/xml/,

感谢任何帮助!

【问题讨论】:

  • 我认为您正在寻找http身份验证:检查此链接stackoverflow.com/questions/1973325/…
  • 我在验证凭据时也面临同样的问题。但是,这仅发生在第二次验证,第一次登录有效并且注销和登录失败。
  • 如果你解决了请发布解决方案
  • 检查你从服务器获取的 NSURLProtectionSpace 是否和你提供给 NSURLCredentialStorage 的一样。服务器可能为领域设置了一些东西。
  • 您使用的是代理吗?我对代理也有同样的问题

标签: iphone ios nsurlconnection nsurlrequest nsurlcredential


【解决方案1】:

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

以下示例将展示一种处理要求凭据的 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");

}

希望对你有帮助

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      使用 Xcode 6,我的应用程序不会在 IOS 8 平台上下载大量文件,尽管它在早期的 IOS 上运行良好。我的代码基本上与您在下面的代码一样,但是当身份验证挑战到来时,错误一直出现。

      我不得不将代码更改为

       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 上一样工作

      【讨论】:

        猜你喜欢
        • 2016-02-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-05
        • 1970-01-01
        相关资源
        最近更新 更多