这是我的会话管理员课程
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>)
}
}
}
我必须向服务器URL发出请求,这是一个没有有效ssl证书的https请求。我已经应用了服务器策略但仍然收到错误: “此服务器的证书无效”
我尝试过的其他替代方案
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
}()
答案 0 :(得分:2)
尝试这似乎对我有用 -
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,请执行以下操作 -
var manager: SessionManager?
manager = SessionManager.getManager()
manager?.request// Here is your request call.
答案 1 :(得分:0)
如果这里有任何人需要我的网络课程。
import UIKit
import Alamofire
import Reqres
class Manager{
static let sharedInstance: Manager = Manager()
static let manager:SessionManager = {
let configuration = Reqres.defaultSessionConfiguration()
let manager = SessionManager()
return manager
}()
// Call this function before each get/post request only if you have unsigned certificate for https request
func setByPass() {
let delegate: SessionDelegate = Manager.manager.delegate
delegate.sessionDidReceiveChallenge = { session, challenge in
var disposition: URLSession.AuthChallengeDisposition = .performDefaultHandling
var credential: URLCredential?
if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {
disposition = URLSession.AuthChallengeDisposition.useCredential
credential = URLCredential(trust: challenge.protectionSpace.serverTrust!)
} else {
if challenge.previousFailureCount > 0 {
disposition = .cancelAuthenticationChallenge
} else {
credential = Manager.manager.session.configuration.urlCredentialStorage?.defaultCredential(for: challenge.protectionSpace)
if credential != nil {
disposition = .useCredential
}
}
}
return (disposition, credential)
}
}
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
}
self.setByPass()
Utils.addHUD()
Manager.manager.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>)
}
}
func getRequestWithoutParam(url:URL,withSuccess success:@escaping (_ response: Dictionary<String, Any>) -> ()){
guard Utils.isInternetAvailable() else{
Utils.showAlert(message: "Internet connection lost", action: {
})
return
}
self.setByPass()
Utils.addHUD()
let headers = ["Content-Type":"Application/json"]
Manager.manager.request(url, encoding:JSONEncoding.default, headers: headers).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>)
}
}
func postRequest(url:URL,param:Parameters,withSuccess success:@escaping (_ response: Dictionary<String, Any>) -> ()){
guard Utils.isInternetAvailable() else{
Utils.showAlert(message: "Internet connection lost", action: {
})
return
}
self.setByPass()
Utils.addHUD()
let headers = ["Content-Type":"Application/json"]
Manager.manager.request(url, method: .post, parameters: param, encoding:JSONEncoding.default, headers: headers).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>)
}
}
func putRequest(url:URL,param:Parameters,withSuccess success:@escaping (_ response: Dictionary<String, Any>) -> ()){
guard Utils.isInternetAvailable() else{
Utils.showAlert(message: "Internet connection lost", action: {
})
return
}
self.setByPass()
Utils.addHUD()
let headers = ["Content-Type":"Application/json"]
Manager.manager.request(url, method: .put, parameters: param, encoding:JSONEncoding.default, headers: headers).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>)
}
}
}
答案 2 :(得分:0)
如下创建Alamofire会话管理器:
private var manager: Alamofire.SessionManager =
{
// Create the server trust policies
let serverTrustPolicies: [String: ServerTrustPolicy] = [
"URL to Bypass": .disableEvaluation
]
// Create custom manager
let configuration = URLSessionConfiguration.default
configuration.httpAdditionalHeaders = Alamofire.SessionManager.defaultHTTPHeaders
let manager = Alamofire.SessionManager(
configuration: URLSessionConfiguration.default,
serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)
)
return manager
}()
//然后,使用上面创建的会话管理器来请求数据,如下所示:
manager.request(requestBuilder.URLString, method: requestBuilder.method, parameters: requestBuilder.parameters, encoding: requestBuilder.encoding, headers: requestBuilder.headers)
.response(responseSerializer: responseSerializer, completionHandler: { (response) in
// Response is here
}