web-dev-qa-db-ja.com

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);

私は次の応答を得ました

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>

接続エラー==エラードメイン= NSURLErrorDomainコード= -1012 "操作を完了できませんでした。(NSURLErrorDomainエラー-1012。)" UserInfo = 0x6e51b90 {NSErrorFailingURLKey = http://example.com/api/ ID/password/xml /

どんな助けでも大歓迎です!

10
Piyush

NSURLRequestを使用してサーバーにアクセスしている間、NSURLConnectionのデリゲートメソッドは認証に異議を唱えるの場合に通知される方法を提供します。

以下のサンプルは、資格情報を要求するURLを処理する1つのアプローチを示しています。

- (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
Durai Amuthan.H

私の場合問題はすべてのHTTPS呼び出しにクライアント証明書を必要とするファイアウォール/プロキシによって引き起こされました。認証チャレンジを処理し、この証明書を提供するNSURLProtocolサブクラスを提供することで、この問題を解決しました。

[NSURLProtocol registerClass:self];

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

- (void)resolveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge withCredential:(NSURLCredential *)credential;
1
Alex Stone

Xcode 6では、以前のIOSで正常に動作したにもかかわらず、アプリがIOS 8プラットフォームで多数のファイルをダウンロードしませんでした。私のコードは基本的に以下のとおりでしたが、エラーが表示され続けました。認証チャレンジが発生したとき。

コードを次のように変更する必要がありました

 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
cavuco