Alamofire HTTPS 请求失败
Posted
技术标签:
【中文标题】Alamofire HTTPS 请求失败【英文标题】:Alamofire HTTPS request failing 【发布时间】:2016-04-16 07:43:46 【问题描述】:我想在我的 swift 应用程序中使用 Alamofire,从服务器获取数据。但是当我通过 Alamofire 发送请求时,它总是抛出一个错误。但是,如果我使用默认配置的 NSUrlSession,它就可以工作。
我尝试了this 解决方案,但仍然无法正常工作
我正在使用的代码
let headers = [
"Authorization": validationHeader
]
let completeUrl = kReserveBaseURL + strMyResURL
Alamofire.request(.GET, completeUrl, headers: headers)
.response request, response, data, error in
debugPrint(error)
debugPrint(response)
以下是我遇到的问题
Some : Error Domain=NSURLErrorDomain Code=-1202 "The certificate for this server is invalid. You might be connecting to a server that is pretending to be “abc.xyz.com” which could put your confidential information at risk." UserInfo=NSURLErrorFailingURLPeerTrustErrorKey=<SecTrustRef: 0x7ff0c22052d0>, NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, _kCFStreamErrorDomainKey=3, _kCFStreamErrorCodeKey=-9813, NSErrorPeerCertificateChainKey=<CFArray 0x7ff0c044b840 [0x10996ba40]>type = immutable, count = 2, values = (
0 : <cert(0x7ff0c22044c0) s: abc.xyz.com i: Verizon Public SureServer EV SSL CA G14-SHA2>
1 : <cert(0x7ff0c2204770) s: Verizon Public SureServer EV SSL CA G14-SHA2 i: Cybertrust Global Root>
), NSUnderlyingError=0x7ff0c2302070 Error Domain=kCFErrorDomainCFNetwork Code=-1202 "(null)" UserInfo=_kCFStreamPropertySSLClientCertificateState=0, kCFStreamPropertySSLPeerTrust=<SecTrustRef: 0x7ff0c22052d0>, _kCFNetworkCFStreamSSLErrorOriginalValue=-9813, _kCFStreamErrorDomainKey=3, _kCFStreamErrorCodeKey=-9813, kCFStreamPropertySSLPeerCertificates=<CFArray 0x7ff0c044b840 [0x10996ba40]>type = immutable, count = 2, values = (
0 : <cert(0x7ff0c22044c0) s: abc.xyz.com i: Verizon Public SureServer EV SSL CA G14-SHA2>
1 : <cert(0x7ff0c2204770) s: Verizon Public SureServer EV SSL CA G14-SHA2 i: Cybertrust Global Root>
), NSLocalizedDescription=The certificate for this server is invalid. You might be connecting to a server that is pretending to be “abc.xyz.com” which could put your confidential information at risk., NSErrorFailingURLKey=https://abc.xyz.com/Api/CheckIn/GetData?userId=0&BookingDate=2016-04-16, NSErrorFailingURLStringKey=https://abc.xyz.com/Api/CheckIn/GetData?userId=0&BookingDate=2016-04-16, NSErrorClientCertificateStateKey=0
【问题讨论】:
我可以看到你用来点击请求的代码 sn-p 吗? 【参考方案1】:由于 ios 安全限制(SSL 级别的 Untruster 服务器证书),您似乎收到此错误。我建议通过在您的应用程序的 info.plist 中添加安全异常来解决此问题:
<key>NSAppTransportSecurity</key>
<dict>
<!--Include to allow all connections (DANGER)-->
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
来源链接: https://ste.vn/2015/06/10/configuring-app-transport-security-ios-9-osx-10-11/
添加后您应该不会再观察到错误了。
如果有帮助,您可以使用以下方法找出 depper:
https://developer.apple.com/library/ios/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html?hl=sw
与您特别相关的部分是:“使用 nscurl 工具诊断 ATS 连接问题”
【讨论】:
【参考方案2】:这是因为安全限制。在info.plist中添加这两个cmets,
【讨论】:
以上是关于Alamofire HTTPS 请求失败的主要内容,如果未能解决你的问题,请参考以下文章
iOS/IBM Cloud/Swift:使用 AlamoFire 发布到 Watson API
在 swift 3 中下载文件时,alamo fire 中的进度视图不会更新