Swift:未调用函数 didReceivechallenge 函数时出现 SSL 错误

问题描述 投票:0回答:1

您好,我正在开发修复 SSL 错误的测试项目。当我尝试从 https://1tamilmv.eu 获取数据时,出现错误

NSLocalizedDescription=发生 SSL 错误并且安全 无法连接到服务器。, NSErrorFailingURLKey=https://www.1tamilmv.eu/, NSUnderlyingError=0x600003f6c090 {错误域=kCFErrorDomainCFNetwork 代码=-1200“(空)” 用户信息={_kCFStreamPropertySSLClientCertificateState=0, _kCFNetworkCFStreamSSLErrorOriginalValue=-9816、_kCFStreamErrorDomainKey=3、_kCFStreamErrorCodeKey=-9816、_NSURLErrorNWPathKey=satisfied(路径满足)、可行,接口:en1、ipv4、ipv6、dns}}、_kCFStreamErrorCodeKey=-9816}

我已经使用命令测试了 cURL 终端中的

curl https://www.1tamilmv.eu
。 cURL 也给出错误

curl: (35) LibreSSL SSL_connect:连接被对等方重置 连接到 www.1tamilmv.eu:443

但是相同的 URL 可以在 Chrome、Firefox 网络浏览器中显示网站,没有任何问题。但在 safari 浏览器中却没有,我注意到

didReceive challenge
函数几乎每次都不会被调用。但如果它被调用,则该网站加载成功。现在刚刚加载了 1 次。我在网络抓取任务的随机网站中遇到相同的错误。

我也尝试过 info.plist

<key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryLoads</key> <true/> <key>NSExceptionDomains</key> <dict> <key>1tamilmv.eu</key> <dict> <key>NSExceptionAllowsInsecureHTTPLoads</key> <true/> <key>NSExceptionMinimumTLSVersion</key> <string>TLSv1.0</string> <key>NSExceptionRequiresForwardSecrecy</key> <false/> <key>NSIncludesSubdomains</key> <true/> </dict> </dict> </dict>

代码:

import Foundation class HTTP: NSObject, URLSessionDelegate, URLSessionTaskDelegate { private(set) var session: URLSession! override init() { let config = URLSessionConfiguration.default //config.tlsMaximumSupportedProtocolVersion = .TLSv13 config.timeoutIntervalForRequest = 30 config.timeoutIntervalForResource = 30 config.httpAdditionalHeaders = [ "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.2 Safari/605.1.15", "Accept-Encoding": "gzip, deflate, br", "Connection": "Keep-Alive" ] super.init() self.session = .init(configuration: config, delegate: self, delegateQueue: .main) } func load() { Task.detached(priority: .userInitiated) { var request = URLRequest(url: URL(string: "https://www.1tamilmv.eu/")!) request.httpMethod = "GET" do { let (data, response) = try await self.session.data(for: request) if let response = response as? HTTPURLResponse { print("-----------") print("Status: \(response.statusCode)") print("Data: \(data.count)") print("-----------") } else { print("Unable to load") } } catch { print("Error: \(error.localizedDescription)") } } } func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge) async -> (URLSession.AuthChallengeDisposition, URLCredential?) { print("Challenging....") if challenge.protectionSpace.serverTrust == nil { return (.useCredential, nil) } let trust: SecTrust = challenge.protectionSpace.serverTrust! let credential = URLCredential(trust: trust) return (.useCredential, credential) } }
大多数情况下,当我点击

HTTP().load()

时,数据不会下载。有没有其他解决方案可以解决这个问题?

ios swift macos nsurlsession urlsession
1个回答
0
投票
这是服务器端问题。我已经像浏览器一样发送了额外的标头,它现在正在工作。

"Accept-Language": "en-GB,en;q=0.9", "Origin": "https://www.1tamilmv.eu", "Sec-Fetch-Mode": "cors", "Sec-Fetch-Site": "cross-site"
    
© www.soinside.com 2019 - 2024. All rights reserved.