gpt4 book ai didi

ios - Swift:如何使用服务器 SSL 证书发出 Https 请求

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

您好,我想在 Swift 中创建 Https 请求。目前我通过 ip 地址访问本地服务器。本地服务器有一个 SSL 证书,通过访问证书想要向服务器发出请求,我正在这样做。

  Alamofire.request(.GET, https://ipaddress//, parameters: [param], headers: headers)
.responseJSON { response in
print(response.request) // original URL request
print(response.response) // URL response
print(response.data) // server data
print(response.result) // result of response serialization

if let JSON = response.result.value {
print("JSON: \(JSON)")


}
}

我在 plist 中使用了上面的代码来发出请求

 <key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>192.168.2.223:1021(my local ip)</key>
<dict>
Include to allow subdomains-->
<key>NSIncludesSubdomains</key>
<true/>
<!--Include to allow insecure HTTP requests-->
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
<!--Include to specify minimum TLS version-->
<key>NSTemporaryExceptionMinimumTLSVersion</key>
<string>TLSv1.1</string>
</dict>
</dict>
</dict>

在 plist 中,我已经给出了这样的,但我仍然收到类似的错误

NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813)

最佳答案

重要提示:请勿在生产中使用它。基本上,使用 Alamofire,您可以绕过应用程序开发和测试目的的身份验证。确保在应用程序在 App Store 上或投入生产之前将其删除:-

func bypassURLAuthentication() {
let manager = Alamofire.Manager.sharedInstance
manager.delegate.sessionDidReceiveChallenge = { session, challenge in
var disposition: NSURLSessionAuthChallengeDisposition = .PerformDefaultHandling
var credential: NSURLCredential?
if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {
disposition = NSURLSessionAuthChallengeDisposition.UseCredential
credential = NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!)
} else {
if challenge.previousFailureCount > 0 {
disposition = .CancelAuthenticationChallenge
} else {
credential = manager.session.configuration.URLCredentialStorage?.defaultCredentialForProtectionSpace(challenge.protectionSpace)
if credential != nil {
disposition = .UseCredential
}
}
}
return (disposition, credential)
}
}

谢谢!让我知道这是否有帮助。 :)

关于ios - Swift:如何使用服务器 SSL 证书发出 Https 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36098485/

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