首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSURLErrorDomain错误-1012

NSURLErrorDomain错误-1012
EN

Stack Overflow用户
提问于 2012-07-23 14:05:05
回答 3查看 38.5K关注 0票数 10

我需要从一个受密码保护的URL解析xml文件,我尝试了以下方法

代码语言:javascript
复制
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);

我得到了以下响应

代码语言:javascript
复制
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>

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

如有任何帮助,我们不胜感激!

EN

回答 3

Stack Overflow用户

发布于 2013-07-29 17:24:00

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

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

代码语言:javascript
复制
- (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");

}

希望这能有所帮助

票数 6
EN

Stack Overflow用户

发布于 2016-03-03 23:30:47

在我的例子中,问题是由防火墙/代理服务器导致的,所有的HTTPS调用都需要客户端证书。我通过提供一个NSURLProtocol子类来处理身份验证挑战并提供此证书,从而解决了这个问题。

代码语言:javascript
复制
[NSURLProtocol registerClass:self];

//implement these methods
- (void)didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;

- (void)resolveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge withCredential:(NSURLCredential *)credential;
票数 1
EN

Stack Overflow用户

发布于 2014-10-01 00:01:47

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

我不得不把代码改成

代码语言:javascript
复制
 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上工作

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11607214

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档