ios NSURLErrorDomain 错误 -1012
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11607214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NSURLErrorDomain error -1012
提问by Piyush
I need to parse a xml file from a password protected URL I tried the following
我需要从受密码保护的 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);
I got the following response
我收到以下回复
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 in connecting == Error Domain=NSURLErrorDomain Code=-1012 "The operation couldn't be completed. (NSURLErrorDomain error -1012.)" UserInfo=0x6e51b90 {NSErrorFailingURLKey=http://example.com/api/ID/password/xml/,
连接错误 == Error Domain=NSURLErrorDomain Code=-1012 “操作无法完成。(NSURLErrorDomain 错误 -1012。)” UserInfo=0x6e51b90 {NSErrorFailingURLKey= http://example.com/api/ID/password/ xml/,
Any help is appreciated !
任何帮助表示赞赏!
回答by Durai Amuthan.H
While using a NSURLRequest
to access a server, the delegate methods of NSURLConnection
provides a way to be notifiedwhen an authentication is challenged.
在使用NSURLRequest
访问服务器时, 的委托方法NSURLConnection
提供了一种在身份验证受到挑战时收到通知的方法。
This below sample will show one approach to handle URLs that asks credentials.
下面的示例将展示一种处理要求凭据的 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");
}
Hope this helps
希望这可以帮助
回答by Alex Stone
In my case the issue was caused by a firewall/proxy requiring a client certificate for all HTTPS calls. I resolved this issue by providing a NSURLProtocol subclass to handle authentication challenges and provide this certificate.
就我而言,该问题是由防火墙/代理要求所有 HTTPS 调用的客户端证书引起的。我通过提供 NSURLProtocol 子类来处理身份验证挑战并提供此证书来解决此问题。
[NSURLProtocol registerClass:self];
//implement these methods
- (void)didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;
- (void)resolveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge withCredential:(NSURLCredential *)credential;
回答by cavuco
With Xcode 6, my app would not download a large number of files on IOS 8 platforms eventhough it worked fine on earlier IOS. My code was essentially as you have it below, but the error kept appearing when an authentication challenge came.
使用 Xcode 6,我的应用程序不会在 IOS 8 平台上下载大量文件,即使它在早期的 IOS 上运行良好。我的代码本质上与您在下面的代码一样,但是当出现身份验证挑战时错误不断出现。
I had to change the code to
我不得不将代码更改为
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];
}
Which of course makes the IF statement superfluous (it can be removed). I have no explanation but without responding to the challenge with username and password even on a persistent session causes an error (-1012). Now it works as on the earlier IOS
这当然使 IF 语句变得多余(它可以被删除)。我没有任何解释,但即使在持久会话中也没有响应用户名和密码的挑战会导致错误(-1012)。现在它可以像早期的 IOS 一样工作