菜鸟教程小白 发表于 2022-12-11 20:09:06

ios - URLSession 数据任务返回零响应 - 找不到 404 页面


                                            <p><p>我正在尝试在 Swift 4 中使用 <code>URlSession</code> 调用 https 请求。我没有收到错误消息,但收到了 nil 响应。</p>

<p>我尝试在 plist 中添加 ATS。添加了 TLS 版本、NSIncludesSubdomainskey 等。仍然不起作用。不确定可能是什么问题。有什么帮助吗?</p>

<p>下面是我的代码:</p>

<pre><code> url = URL(string: &#34;https://quickweb.support.qvalent.com/OnlinePaymentServlet3&#34;)

let urlRequest = URLRequest(url: url!, method: method, body: data.toEncodedData(), cachePolicy: .useProtocolCachePolicy)
            let useMainThread = true
            let configuration = URLSessionConfiguration.default
            var queue: OperationQueue? = nil
            if useMainThread {
                queue = OperationQueue.main
            }


let urlSession = URLSession(configuration: configuration, delegate: self, delegateQueue: queue)



urlSession.dataTask(with: urlRequest){ data, resonse, error in
      if let error = error {
            self.errorMessage += Constants.shared.dataErrorMsg+error.localizedDescription
            onComplete(false,nil,self.errorMessage)
      }else if let data = data{
            self.parse(data: data, with: onComplete)
      }


}.resume()
</code></pre>

<p>令人惊讶的是,几年前我在 Objective C 中完成了同样的调用,效果非常好。尝试在 Xcode 9.3 中将其转换为 swift 4 失败。</p>

<p>当我打印响应数据时,它显示“404 page not found”</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>已解决。这与请求或 url 无关。客户端期待一个已就位但未被调用的委托(delegate)调用,因此它返回 404。URLSessionTaskDelegate 是关键。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - URLSession 数据任务返回零响应 - 找不到 404 页面,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/50259931/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/50259931/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - URLSession 数据任务返回零响应 - 找不到 404 页面