• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - URLSessionDelegate、URLSessionDataDelegate 问题

[复制链接]
菜鸟教程小白 发表于 2022-12-11 17:46:56 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

自从我升级到 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 = "OST"
request.addValue("**authentication**", forHTTPHeaderField: "Authorization")
request.httpBody = "req=\(xmlRequest)".data(using: String.Encoding.utf8)

我的类(class)正在实现 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 autherntcationchallenge = NSURLAuthenticationMethodServerTrust send credential Server Trust

这是唯一打印的内容,因此没有调用其他函数。这里出了什么问题?

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

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



Best Answer-推荐答案


此代码危险。您正在通过执行您正在执行的操作完全禁用所有 TLS 验证,这使您的连接不比 HTTP 更好。千万不要盲目接受服务器提供的证书。

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

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

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

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

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

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap