本文介绍了如何在Swift 3中使用Alamofires ServerTrustPolicy.disableEvaluation的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的会话管理器类
import UIKit
import Alamofire
import ObjectMapper
import AlamofireObjectMapper
import Reqres
class Manager: SessionManager {
static let sharedInstance: Manager = Manager()
static let manager: Manager = {
let configuration = Reqres.defaultSessionConfiguration()
let manager = Manager(configuration: configuration)
return manager
}()
func getRequest(url:URL,param:Parameters,withSuccess success:@escaping (_ response: Dictionary<String, Any>) -> ()){
guard Utils.isInternetAvailable() else{
Utils.showAlert(message: "Internet connection lost", action: {
})
return
}
Utils.addHUD()
Alamofire.request(url, method: .get, parameters: param, encoding: URLEncoding.default).responseJSON { (response:
DataResponse<Any>) in
guard response.result.isSuccess else{
Utils.hideHUD()
Utils.showAlert(message: (response.result.error?.localizedDescription)!, action: {
})
return
}
Utils.hideHUD()
print(response.request!) // original URL request
print(response.response!) // HTTP URL response
print(response.data!) // server data
print(response.result) // result of response serialization
success(response.result.value! as! Dictionary<String, Any>)
}
}
}
我必须向服务器网址发出请求,这是一个没有有效ssl证书的https请求。我已应用服务器策略来执行此操作,但仍然收到错误:
此服务器的证书无效
I have to make request to a server url which is an https request without valid ssl certificates.I have applied server policies to do so but still getting the error:"The certificate for this server is invalid"
我尝试过的其他替代方法
other alternatives that i have tried
class Manager: SessionManager {
static let manager: Manager = {
let configuration = Reqres.defaultSessionConfiguration()
let serverTrustPolicies: [String: ServerTrustPolicy] = ["192.168.1.28:8443": .DisableEvaluation]
let manager = Manager(configuration: configuration, serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies))
return manager
}()
推荐答案
尝试一下似乎对我有用的方法-
Try this which seems to be working for me -
extension SessionManager {
static func getManager() -> SessionManager{
let serverTrustPolicies: [String: ServerTrustPolicy] = [
"192.168.1.28:8443": .disableEvaluation
]
let configuration = Reqres.defaultSessionConfiguration()
configuration.httpAdditionalHeaders = Alamofire.SessionManager.defaultHTTPHeaders
return Alamofire.SessionManager(
configuration: configuration,
serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)
)
}
}
要使用此SessionManager,请执行以下操作-
And to use this SessionManager do the following -
var manager: SessionManager?
manager = SessionManager.getManager()
manager?.request// Here is your request call.
这篇关于如何在Swift 3中使用Alamofires ServerTrustPolicy.disableEvaluation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!