将token
的get QLIK server
api与ssl
证书集成在一起,但是执行任务后,我得到了这样的错误-
“ 完成错误-代码:-999 ”
// Your hostname and endpoint
let hostname = "YOUR_HOST_NAME"
let endpoint = "YOUR_ENDPOINT"
let cert = "YOUR_CERT" // e.g. for cert.der, this should just be "cert"
// Set up certificates
let pathToCert = Bundle.main.path(forResource: cert, ofType: "der")
let localCertificate = NSData(contentsOfFile: pathToCert!)
let certificates = [SecCertificateCreateWithData(nil, localCertificate!)!]
// Configure the trust policy manager
let serverTrustPolicy = ServerTrustPolicy.pinCertificates(
certificates: certificates,
validateCertificateChain: true,
validateHost: true
)
let serverTrustPolicies = [hostname: serverTrustPolicy]
let serverTrustPolicyManager = ServerTrustPolicyManager(policies: serverTrustPolicies)
// Configure session manager with trust policy
afManager = SessionManager(
configuration: URLSessionConfiguration.default,
serverTrustPolicyManager: serverTrustPolicyManager
)
qlikManager.request(strURL, method: .post, parameters: params, encoding: JSONEncoding.default, headers: headers).responseJSON { (responseObject) -> Void in
print(responseObject)
}
请检查
答案 0 :(得分:2)
创建CustomServerTrustPolicyManager.swift
并使用以下代码
import UIKit
import Alamofire
class CustomServerTrustPolicyManager: ServerTrustPolicyManager {
override func serverTrustPolicy(forHost host: String) -> ServerTrustPolicy? {
// Check if we have a policy already defined, otherwise just kill the connection
if let policy = super.serverTrustPolicy(forHost: host) {
return policy
} else {
return .customEvaluation({ (_, _) -> Bool in
return false
})
}
}
}
然后创建NetworkManager.swift
并使用以下代码:
import UIKit
import Alamofire
class NetworkManager {
static let sharedInstance = NetworkManager()
let manager: Alamofire.SessionManager = {
//For using Cerificates Pinning
let serverTrustPolicies: [String: ServerTrustPolicy] = [
"host_url": .pinCertificates(
certificates: ServerTrustPolicy.certificates(),
validateCertificateChain: true,
validateHost: true
)
]
/*
//For Using with Public Key
let serverTrustPolicies: [String: ServerTrustPolicy] = [
"host_url": .pinPublicKeys(
publicKeys: ServerTrustPolicy.publicKeys(),
validateCertificateChain: true,
validateHost: true
)
]
*/
let configuration = URLSessionConfiguration.default
configuration.httpAdditionalHeaders = Alamofire.SessionManager.defaultHTTPHeaders
return Alamofire.SessionManager(
configuration: configuration,
serverTrustPolicyManager: CustomServerTrustPolicyManager(policies: serverTrustPolicies)
)
}()
}
之后,请按照以下步骤测试您的连接。
func testSSLConnection() {
NetworkManager.sharedInstance.manager.request("your_url_to_test").responseJSON { response in
print("Request: \(String(describing: response.request))") // original url request
print("Response: \(String(describing: response.response))") // http url response
print("Result: \(response.result)") // response serialization result
if let json = response.result.value {
print("JSON: \(json)") // serialized json response
}
}
}
希望它会对您有所帮助。