ios - URLSessionDelegate、URLSessionDataDelegate 问题

标签 ios swift3 nsurlsession

自从我升级到 Swift 3 后,我就遇到了从后端加载信息的问题。在 Swift 3 之前,我使用的是:

NSURLConnection.sendAsynchronousRequest(request as URLRequest, queue: OperationQueue.main) { (response, data, error) in

     if let responseData = NSString(data: data!, encoding: String.Encoding.utf8.rawValue){
         print("data: \(responseData)")
         self.parseXML(data!)        
     }
}

但是在我更新后,每次运行时我都会得到服务器无效的错误,并且服务器可能会伪装成它。

通过 stackoverflow 查看后,我将代码更改为:

let task = URLSession(configuration: .default, delegate: self, delegateQueue: nil)
task.dataTask(with: request).resume()

请求本身没有改变:

var request = URLRequest(url: URL(string: "urlhere")!)

request.httpMethod = "POST"
request.addValue("**authentication**", forHTTPHeaderField: "Authorization")
request.httpBody = "req=\(xmlRequest)".data(using: String.Encoding.utf8)

我的类正在实现 URLSessionDelegateURLSessionData 委托(delegate)

func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
    print("Data received: \(data)")
}

func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse, completionHandler: @escaping (URLSession.ResponseDisposition) -> Void) {

    print("Response received: \(response)")
}

func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
    print("something went wrong!")
}

func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {

    print("did autherntcationchallenge = \(challenge.protectionSpace.authenticationMethod)")

    if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust  {
        print("send credential Server Trust")
        let credential = URLCredential(trust: challenge.protectionSpace.serverTrust!)
        challenge.sender!.use(credential, for: challenge)

    }else if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodHTTPBasic{
        print("send credential HTTP Basic")
        let defaultCredentials: URLCredential = URLCredential(user: "username", password: "password", persistence:URLCredential.Persistence.forSession)
        challenge.sender!.use(defaultCredentials, for: challenge)

    }else if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodNTLM{
        print("send credential NTLM")

    } else{
        challenge.sender!.performDefaultHandling!(for: challenge)
    }
}

func urlSession(_ session: URLSession, didBecomeInvalidWithError error: Error?) {
    print("there was an error: \(error)")
}

但是,当我现在运行时,执行了 did receive challenge 函数,但没有执行其他函数,我已经使用 print 语句和使用断点对此进行了测试。

这是我的控制台:

did autherntcationchallenge = NSURLAuthenticationMethodServerTrust send credential Server Trust

这是唯一打印的东西,所以没有调用其他函数。这里出了什么问题?

*****编辑*****

所以经过更多测试后,我意识到如果我注释掉接收挑战函数,则会调用 did complete with error 函数,
所以,它们确实被调用了,但是当调用接收挑战时,其他函数都没有运行,因此,我无法从我的服务器获取任何数据。

最佳答案

此代码危险。您通过执行您正在做的事情完全禁用了所有 TLS 验证,这使得您的连接并不比 HTTP 好。永远不要盲目接受服务器提供的证书。

相反,首先检查它以确保它确实是您的,然后执行您上面所做的,或者如果它不执行默认处理,则请求将失败。

此外,您的证书链中的一个证书已过期,您正在通过 IP 地址发出请求,这是您的证书验证失败的原因之一。不要那样做。使用具有真实证书的域名。您可以从任意数量的权威机构获得免费证书,因此没有理由在您的服务器上没有有效证书。

最后,您的服务器使用的是过时的密码 EDH-RSA-DES-CBC3-SHA,它使用 3DES。您应该远离该密码,因为它可能在未来的 iOS 版本中不受支持。

至于为什么请求没有继续执行,我不知道。您升级后的 Swift 语法可能有问题,但这超出了我的专业领域,所以我不会尝试猜测它是什么。但是 IMO,这在很大程度上是没有实际意义的,因为你不应该做你正在做的事情,如果你不试图有效地禁用 TLS,那么该代码将不存在,你的应用程序将运行。 :-)

关于ios - URLSessionDelegate、URLSessionDataDelegate 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39923046/

相关文章:

ios - 在 block 中将 self 作为参数不会导致循环引用,但对 self 的外部引用会导致循环引用

ios - 如何在ios13中禁用pdfkits pdfview中的查找、共享、转发菜单项

ios - 关于 @autoreleasepool block 从 Objective-C 迁移到 Swift

swift - 成功传递数据时应用程序崩溃

swift - 为 IOS 快速制作 CheckBox

ios - 在IOS上使用FFMPEG mux flv和发送rtmp

swift3 - 每周触发通知 Swift 3

ios - 如何使用证书身份验证调用 HTTPS URL

ios - NSURLSessionUploadTask 获取响应数据

swift - 使用未解析的标识符 'response'(在 ""“dataTask.resume()""”之后)