菜鸟教程小白 发表于 2022-12-11 17:46:56

ios - URLSessionDelegate、URLSessionDataDelegate 问题


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

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

   if let responseData = NSString(data: data!, encoding: String.Encoding.utf8.rawValue){
         print(&#34;data: \(responseData)&#34;)
         self.parseXML(data!)      
   }
}
</code></pre>

<p>但是在我更新之后,每次我运行我都会得到服务器无效的错误,并且服务器可能会假装它。 </p>

<p>查看stackoverflow后,我将代码更改为:</p>

<pre><code>let task = URLSession(configuration: .default, delegate: self, delegateQueue: nil)
task.dataTask(with: request).resume()
</code></pre>

<p>请求本身没有改变:</p>

<pre><code>var request = URLRequest(url: URL(string: &#34;urlhere&#34;)!)

request.httpMethod = &#34;POST&#34;
request.addValue(&#34;**authentication**&#34;, forHTTPHeaderField: &#34;Authorization&#34;)
request.httpBody = &#34;req=\(xmlRequest)&#34;.data(using: String.Encoding.utf8)
</code></pre>

<p>我的类(class)正在实现 <code>URLSessionDelegate</code> 和 <code>URLSessionData</code> 委托(delegate)</p>

<pre><code>func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
    print(&#34;Data received: \(data)&#34;)
}

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

    print(&#34;Response received: \(response)&#34;)
}

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

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

    print(&#34;did autherntcationchallenge = \(challenge.protectionSpace.authenticationMethod)&#34;)

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

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

    }else if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodNTLM{
      print(&#34;send credential NTLM&#34;)

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

func urlSession(_ session: URLSession, didBecomeInvalidWithError error: Error?) {
    print(&#34;there was an error: \(error)&#34;)
}
</code></pre>

<p>但是,当我现在运行时,确实执行了接收挑战函数,但其​​他函数均未执行,我已经使用打印语句和断点对此进行了测试。 </p>

<p>这是我的控制台:</p>

<blockquote>
<p>did autherntcationchallenge = NSURLAuthenticationMethodServerTrust
send credential Server Trust</p>
</blockquote>

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

<p>*****编辑*****</p>

<p>所以经过更多测试后,我意识到如果我注释掉接收挑战函数,那么确实会调用带有错误的函数,<br/>因此,它们确实被调用了,但是当调用接收挑战时,没有其他函数被调用运行,因此,我无法从我的服务器获取任何数据。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>此代码<em>危险</em>。您正在通过执行您正在执行的操作完全禁用所有 TLS 验证,这使您的连接不比 HTTP 更好。千万不要盲目接受服务器提供的证书。</p>

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

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

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

<p>至于为什么请求没有继续执行,我不知道。你升级的 Swift 语法可能有问题,但这超出了我的专业领域,所以我不会试图猜测它是什么。但是 IMO,这在很大程度上是没有意义的,因为你不应该做你正在做的事情,如果你不试图有效地禁用 TLS,那么该代码将不存在,并且你的应用程序将工作。 :-)</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - URLSessionDelegate、URLSessionDataDelegate 问题,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/39923046/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/39923046/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - URLSessionDelegate、URLSessionDataDelegate 问题