2014-01-06 65 views
6

從安全URL下載安全牆後的圖像。一個URL,如https://foo.com/bar.pngNSURLSession變得間歇性SSLHandshake失敗(-9810)錯誤

有時我得到一個SSLHandshake錯誤。有時錯誤發生一次,但有時錯誤是不變的,我無法下載文件。

DownloadImageApp[2914] <Warning>: CHALLENGE!!! (NSURLAuthenticationMethodServerTrust) 
DownloadImageApp[2914] <Warning>: server = foo.com 
DownloadImageApp[2914] <Warning>: CFNetwork SSLHandshake failed (-9810) 
DownloadImageApp[2914] <Warning>: CHALLENGE!!! (NSURLAuthenticationMethodServerTrust) 
DownloadImageApp[2914] <Warning>: server = foo.com 
DownloadImageApp[2914] <Warning>: CHALLENGE!!! (NSURLAuthenticationMethodNTLM) 
DownloadImageApp[2914] <Warning>: NSURLAuthenticationMethodNTLM 

我用下面的代碼來處理的挑戰

- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *))completionHandler { 
    NSLog(@"CHALLENGE!!! (%@)", challenge.protectionSpace.authenticationMethod); 

    if (challenge.error) 
     NSLog(@" -- error: %@", challenge.error.description); 
    if (challenge.previousFailureCount > 0) 
     NSLog(@" -- previous failure count = %d", challenge.previousFailureCount); 
    if (challenge.proposedCredential) 
     NSLog(@" -- proposed credential user: %@", challenge.proposedCredential.user); 

    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) { 
     NSLog(@" server = %@", challenge.protectionSpace.host); 
     completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]); 
    } else if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodNTLM]) { 
     NSLog(@" NSURLAuthenticationMethodNTLM"); 
     NSURLCredential *credential = [NSURLCredential credentialWithUser:@"username" password:@"passwordIsSecretShhh" persistence:NSURLCredentialPersistenceForSession]; 
     completionHandler(NSURLSessionAuthChallengeUseCredential, credential); 
    } else { 
     NSLog(@" ????"); 
    } 
} 

即使它完全失敗,並不會加載我仍然可以彈出到Safari瀏覽器,在URL類型,有它加載。所以我正在尋找可能會導致此問題的想法,而不是網絡問題或託管Web服務器出現問題。

+0

你有沒有得到任何解決這個握手錯誤? – Ekra

+0

沒有。不是件事。我注意到它有時會重試並且工作正常,但偶爾會連續失敗3次,導致完全連接失敗。 – DBD

回答

4

我終於解決了這個問題。它與我的代碼無關。

問題原因是服務器上安裝的趨勢SSL監控軟件。一旦軟件被禁用,錯誤即刻消失。

這不是一個好的答案,但希望它能幫助別人。