gpt4 book ai didi

ios - 使用 Alamofire 的不可信证书。我已经尝试了所有我能找到的答案

转载 作者:行者123 更新时间:2023-11-28 15:14:50 25 4
gpt4 key购买 nike

这是我的 info.plist

<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>https://chargepoints.dft.gov.uk</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSExceptionRequiresForwardSecrecy</key>
<false/>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>

</dict>
</dict>

这就是我尝试在 alamofire 上设置 session 管理器的方式

private static var Manager: Alamofire.SessionManager = {

// Create the server trust policies
let serverTrustPolicies: [String: ServerTrustPolicy] = [
"https://chargepoints.dft.gov.uk": .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
}()

这是我执行请求的代码

Downloader.Manager.request("https://chargepoints.dft.gov.uk/api/retrieve/registry/format/json").responseJSON { response in
print("Request: \(String(describing: response.request))") // original url request
print("Response: \(String(describing: response.response))") // http url response
print("Result: \(String(describing: response.result))") // response serialization result

print("Error: \(String(describing: response.error))")

if let json = response.result.value {
print("JSON: \(json)") // serialized json response
}

if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) {
print("Data: \(utf8Text)") // original server data as UTF8 string
}
}

哦,使用 iOS 10.3

X代码 8.3.2

swift 3.0

最佳答案

试试这个

var afManager : SessionManager?

afManager!.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 = URLSession.AuthChallengeDisposition.cancelAuthenticationChallenge
}
else
{
credential = self.afManager!.session.configuration.urlCredentialStorage?.defaultCredential(for: challenge.protectionSpace)
if(credential != nil)
{
disposition = URLSession.AuthChallengeDisposition.useCredential
}

}
return (disposition, credential)
}

然后提出你的要求

  afManager?.request("YOUR-URL-HERE", method: .get).responseJSON { response in

switch response.result {
case .success:
print(response.result.value)
break
case .failure(let error):
print(error)
}
}

关于ios - 使用 Alamofire 的不可信证书。我已经尝试了所有我能找到的答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47029633/

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