2016-04-21 36 views
0

我無法從服務器獲取有關用戶的信息。基本訪問驗證

它我使用這些:

用戶: 「[email protected]

通: 「1234ABCD」

主機: 「www.host.com」

https://[email protected]:[email protected]/user.xml

NSData * returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:&響應錯誤:& errorServer];

在錯誤我根據維基百科https://en.wikipedia.org/wiki/Basic_access_authentication 網址模板的文章有這樣的

Error Domain=NSURLErrorDomain Code=-1200 "An SSL error has occurred and a secure connection to the server cannot be made." UserInfo={_kCFStreamErrorCodeKey=-9847, NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, NSUnderlyingError=0x7f9c7be02f50 {Error Domain=kCFErrorDomainCFNetwork Code=-1200 "An SSL error has occurred and a secure connection to the server cannot be made." UserInfo={NSErrorFailingURLStringKey=https://[email protected]:[email protected]/user.xml, NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, _kCFNetworkCFStreamSSLErrorOriginalValue=-9847, _kCFStreamPropertySSLClientCertificateState=0, NSLocalizedDescription=An SSL error has occurred and a secure connection to the server cannot be made., _kCFStreamErrorDomainKey=3, NSErrorFailingURLKey=https://[email protected]:[email protected]/user.xml, _kCFStreamErrorCodeKey=-9847}}, NSLocalizedDescription=An SSL error has occurred and a secure connection to the server cannot be made., NSErrorFailingURLKey=https://[email protected]:[email protected]/user.xml, NSErrorFailingURLStringKey=https://[email protected]:[email protected]/user.xml, _kCFStreamErrorDomainKey=3} 

看起來不錯

URL encoding A client may avoid a login prompt when accessing a basic access authentication by prepending username:[email protected] to the hostname in the url. For example, the following would access the page index.html at the web site www.example.com with the secure HTTPS protocol and provide the username Aladdin and the password OpenSesame credentials via basic authorization:

https://Aladdin:[email protected]/index.html

是的,我設置 「應用交通運輸安全設置」

<dict> 
<key>NSAllowsArbitraryLoads</key> 
<true/> 
</dict> 

我做錯了什麼?

+0

你不能編碼在URL中usernam和密碼。在這種情況下,您需要設置基本身份驗證標頭 – Paulw11

+0

服務器給我錯誤響應(默認數據) – Vit

+0

顯示代碼設置標頭的位置 – Paulw11

回答

0

所以,我已經找到了解決方案

我有緩存的衝突。 此之前的用戶呼叫的第二時間信息,我們必須對清潔NSURLCredentialStorage和Cache

- (void)clearCacheAndCookies 

爲此{ 的NSDictionary * credentialsDict = [[NSURLCredentialStorage sharedCredentialStorage] allCredentials];

​​

}

0

試試這個,它可以幫助你 - 在Info.plist文件中添加此

<key>NSAppTransportSecurity</key> 
<dict> 
    <key>NSExceptionDomains</key> 
    <dict> 
     <key>uservoice.com</key> 
     <dict> 
      <key>NSIncludesSubdomains</key> 
      <true/> 
      <key>NSExceptionRequiresForwardSecrecy</key> 
      <false/> 
     </dict> 
    </dict> 
</dict> 
+0

我做到了但沒有幫助我 – Vit

0

你能試試以下兩種方法之一,然後在這裏發表日誌,以便我們進行調查,並幫助你。

- (void)approach1 { 
    NSString *host = @"https://www.host.com"; 
    NSURL *url = [NSURL URLWithString:host]; 

    NSString *userName = @"[email protected]"; 
    NSString *password = @"1234abcd"; 

    NSString *credentialString = [NSString stringWithFormat:@"%@:%@", userName, password]; 
    NSString *basicAuthString = [NSString stringWithFormat:@"Basic %@", [[credentialString dataUsingEncoding:NSUTF8StringEncoding] base64EncodedStringWithOptions:0]]; 

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url]; 
    [request setValue:basicAuthString forHTTPHeaderField:@"Authorization"]; 

    [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { 

    }]; 
} 

- (void)approach2 { 
    NSString *host = @"https://www.host.com"; 
    NSURL *url = [NSURL URLWithString:host]; 

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url]; 

    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:nil]; 
    [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { 

    }]; 
} 

- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge 
completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * __nullable credential))completionHandler { 
    NSString *userName = @"[email protected]"; 
    NSString *password = @"1234abcd"; 
    NSURLCredential *cred = [NSURLCredential credentialWithUser:userName password:password persistence:NSURLCredentialPersistenceNone]; 
    completionHandler(NSURLSessionAuthChallengeUseCredential, cred); 
}