Swift 3 NSURLSession / NSURLConnection当连接到具有身份validation的站点时,HTTP加载失败

我以前见过这个错误,似乎补救办法是将信息添加到.plist文件。 我试过了我见过的每一个:

如何在iOS 9中启用启用App Transport Security的HTTP URL?

在iOS 9上NSURLSession / NSURLConnection HTTP加载失败

NSURLSession / NSURLConnection HTTP加载失败(kCFStreamErrorDomainSSL,-9802)子域?

仍然我的程序导致相同的错误抛出:

2017-05-08 08:29:37.781075-0400 xxxx [3256:408961] [] nw_coretls_callback_handshake_message_block_invoke_3 tls_handshake_continue:[-9812] 2017-05-08 08:29:37.781 xxxx [3256:408982] NSURLSession / NSURLConnection HTTP加载失败kCFStreamErrorDomainSSL,-9813)

我的相关代码在这里:

let username = "xxxx" let password = "xxxx" let loginString = String(format: "%@:%@", username, password) let loginData = loginString.data(using: String.Encoding.utf8)! let base64LoginString = loginData.base64EncodedString() let baseUrl = "https://xxxx" var request = URLRequest(url: URL(string: baseUrl)! as URL) let baseUrl = "server i want.xml" let request = NSMutableURLRequest(url: NSURL(string: baseUrl)! as URL) request.httpMethod = "POST" request.setValue("Basic \(base64LoginString)", forHTTPHeaderField: "Authorization") let session = URLSession.shared var err: NSError? let task = session.dataTask(with: request as URLRequest) { (data, response, error) in if data == nil { print("dataTaskWithRequest error: \(err)") return } let xml = SWXMLHash.parse(data!) let serialNumFromXML = xml["mobile_devices"]["mobile_device"]["serial_number"].element?.text 

 func urlSession( _ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) { completionHandler( .useCredential, URLCredential(trust: challenge.protectionSpace.serverTrust!)) } 

我读过它可能与证书有关,但我不能更改服务器,因为我不负责它,所以我无法获得适当的证书。 除了更换plist还有更多吗?

我的plist文件有允许任意负载和我的XML文件所在的域的exception域。

我可以得到一个testing文件的内容,比如https://www.w3schools.com/xml/note.xml ,但是我需要的文件是在一个用户名/密码“Authentication Required”的popup框后面。

我也尝试过Alamofire库,发生同样的事情:

nw_coretls_callback_handshake_message_block_invoke_3 tls_handshake_continue:[-9807] 2017-05-08 09:42:08.452 xxx [6128:888398] NSURLSession / NSURLConnection HTTP加载失败(kCFStreamErrorDomainSSL,-9802)

好吧,我不知道如何解决这个问题,但是我发现Alamofire可以用来做我所需要的。 这是一个库存pipe理的本地项目,所以我不关心绕过证书的安全性。 我的代码在这里:

  Manager.delegate.sessionDidReceiveChallenge = { session, challenge in var disposition: URLSession.AuthChallengeDisposition = .performDefaultHandling var credential: URLCredential? if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust, let trust = challenge.protectionSpace.serverTrust { disposition = URLSession.AuthChallengeDisposition.useCredential credential = URLCredential(trust: trust) } else { if challenge.previousFailureCount > 0 { disposition = .cancelAuthenticationChallenge } else { credential = Manager.session.configuration.urlCredentialStorage?.defaultCredential(for: challenge.protectionSpace) if credential != nil { disposition = .useCredential } } } return (disposition, credential) } 

 private var Manager: Alamofire.SessionManager = { // Create the server trust policies let serverTrustPolicies: [String: ServerTrustPolicy] = [ "https://SERVER:PORT": .disableEvaluation ] // Create custom manager let configuration = URLSessionConfiguration.default configuration.httpAdditionalHeaders = Alamofire.SessionManager.defaultHTTPHeaders let manager = Alamofire.SessionManager( configuration: URLSessionConfiguration.default, serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies) ) let sessionManager = SessionManager( serverTrustPolicyManager: ServerTrustPolicyManager( policies: ["https://SERVER:PORT": .disableEvaluation] ) ) return manager }() 

 open class MyServerTrustPolicyManager: ServerTrustPolicyManager { // Override this function in order to trust any self-signed https open override func serverTrustPolicy(forHost host: String) -> ServerTrustPolicy? { return ServerTrustPolicy.disableEvaluation } }