我在将这段代码迁移到Alamofire 5时遇到问题:
init(plugins: [PluginType] = []) {
let serverTrustPolicies: [String: ServerTrustPolicy] = [
"test.com.ph": .pinCertificates(
certificates: ServerTrustPolicy.certificates(),
validateCertificateChain: true,
validateHost: true
)
]
let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest = 120 // default is 60.
let manager = Manager(
configuration: configuration,
serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)
)
super.init(manager: manager, plugins: plugins)
}
我已经修改了我的代码,但是我不知道如何初始化会话管理器:
let pinEvaluator = PinnedCertificatesTrustEvaluator(certificates: Bundle.main.af.certificates,
acceptSelfSignedCertificates: true,
performDefaultValidation: true,
validateHost: true)
let trustManager = ServerTrustManager.init(evaluators: ["test.com.ph": pinEvaluator])
let sessionManager = Session.init(configuration: .default, serverTrustManager: trustManager)
super.init(session: sessionManager,plugins: plugins)
有人可以帮我吗?
由于错误状态,您向未使用ServerTrustEvaluating
值注册的主机发出了请求。也就是说,不是test.com.ph
的请求。您将要确保向正确的主机发出请求,或者已将正确的主机用于证书固定。
如果希望向多个主机发出请求,则可以通过将ServerTrustManager
属性设置为allHostsMustBeEvaluated
来初始化false
,来禁用此行为。