1

我们在两种类型的硬件设备上都有类似的服务器,我们需要使用 iOS 应用程序来连接它们。目前我们对所有 API 请求使用 Alamofire 4.9.1。

以前我们在我们的服务器上只支持 http 端点,但最近我们在最新版本的设备上转移到了 https。应用程序需要同时支持这两者。

我们面临的问题是,当我们连接到设备 X(支持 https),然后尝试连接到设备 Y(仅支持 http)时,我们会收到此错误

[Error] GET 'http://172.30.1.1/file.xml' [27.7252 s]:

Error Domain=NSURLErrorDomain Code=-1004 "Could not connect to the server." UserInfo={​​​​​​​NSUnderlyingError=0x281f2fde0 {​​​​​​​Error Domain=kCFErrorDomainCFNetwork Code=-1004 "(null)" UserInfo={​​​​​​​_kCFStreamErrorCodeKey=61, _kCFStreamErrorDomainKey=1}​​​​​​​}​​​​​​​, NSErrorFailingURLStringKey=https://172.30.1.1/file.xml, NSErrorFailingURLKey=https://172.30.1.1/file.xml, _kCFStreamErrorDomainKey=1, _kCFStreamErrorCodeKey=61, NSLocalizedDescription=Could not connect to the server.}​​​​​​​

直接连接到设备 Y(http) 而不先连接到设备 X(https) 可以正常工作,并且在错误之后如果我们终止应用程序,我们可以连接到设备 Y(http)。即使我们在 Alamofire 中使用不同的 SessionManager,也会发生这种情况。

这是因为我们使用了相同的IP地址吗?如果我们检查错误NSErrorFailingURLStringKey,它仍然是 https 虽然我们调用的 url 是 http。

我在这里粘贴相关代码

    private var manager: Alamofire.SessionManager = {
        let configuration = URLSessionConfiguration.default
        let manager = Alamofire.SessionManager(configuration: configuration)
        manager.retrier = RetryHandler()
        return manager
    }()

    private var manager2: Alamofire.SessionManager = {
        var serverTrustPolicies: [String: ServerTrustPolicy] = [
            "172.30.1.1": .disableEvaluation
        ]
        let config = URLSessionConfiguration.ephemeral
       
        //Adding TrustManager to Allow All Certificates
        let manager = Alamofire.SessionManager(configuration: config, serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies))
        manager.adapter = RequestInterceptor()
        manager.retrier = RequestInterceptor()
        
        return manager
    }()

我们已经尝试过的事情:

  • 使用不同的 UrlSessionConfiguration
  • 设置 config.urlCache = nil
4

0 回答 0