2015-10-20 64 views
2

我有這樣的代碼:應用傳輸安全:NSURLSession/NSURLConnection的HTTP加載失敗

func loginWithBasicAuth() -> Void { 

    let usernameText = username.text 
    let passwordText = password.text 

    let credentialData = "\(usernameText):\(passwordText)".dataUsingEncoding(NSUTF8StringEncoding) 
    let base64Credentials = credentialData?.base64EncodedStringWithOptions([]) 
    let headers = ["Authorization": "HTTP Basic \(base64Credentials)"] 

    Alamofire.request(.GET, "https://api.domainname.com/v0.1/", headers: headers).responseString { aResponse in 

     if let receivedString = aResponse.result.value { 

      print(receivedString) 

     } else { 

      print("Login failed.") 

     } 
    } 
} 

在它打印控制檯:

NSURLSession/NSURLConnection的HTTP加載失敗 (kCFStreamErrorDomainSSL,-9802) 登錄失敗。

這裏是在info.plist文件中的配置: enter image description here

什麼是錯

+0

在您的異常域中嘗試'NSTemporaryExceptionRequiresForwardSecrecy = NO' –

+0

不,沒有任何更改。 –

+0

將此關鍵字'NSIncludesSubdomains'添加到api.domainname.com dictionary.And將其設置爲YES以包含子域名 – Leo

回答

1

讓NSAllowsArbitraryLoads爲YES禁用ATS?

+0

我試過了,但問題依然存在。 –

2

我不確定NSThirdPartyExceptionAllowsInsecureHTTPLoads是通過HTTPS連接時使用的正確密鑰。您應該使用NSThirdPartyExceptionMinimumTLSVersionNSThirdPartyExceptionRequiresForwardSecrecy

我強烈建議您閱讀App Transport Security Technote文檔,特別是診斷連接問題部分。

有一個工具,可以幫助您診斷您的域名:

/usr/bin/nscurl --ats-diagnostics [--verbose] URL 
0

添加NSIncludesSubdomains你指的子域,並希望擁有它也可以作爲例外。 See this answer

相關問題