gpt4 book ai didi

Swift 客户端和根 SSL 证书身份验证

转载 作者:搜寻专家 更新时间:2023-11-01 07:01:11 25 4
gpt4 key购买 nike

QLIK servertoken api 与 ssl 证书集成,但在执行任务后我收到这样的错误 -

"finished with error - code: -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)
}

请检查一下

最佳答案

创建 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
}
}
}

希望对你有所帮助。

关于Swift 客户端和根 SSL 证书身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51173275/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com