gpt4 book ai didi

ios - 解析 HTTPS JSON 数据错误 The Certificate for this server is invalid

转载 作者:行者123 更新时间:2023-11-28 09:19:21 24 4
gpt4 key购买 nike

在 Xcode Playground 中尝试一个非常简单的 RESTful API GET 请求,但收到证书错误。这是我的代码:

import UIKit
import XCPlayground

XCPSetExecutionShouldContinueIndefinitely()

let url = NSURL(string:"https://www.googleapis.com/books/v1/volumes?q=programming")
let urlReq = NSURLRequest(URL: url)

let queue = NSOperationQueue()

NSURLConnection.sendAsynchronousRequest(urlReq, queue: queue, completionHandler: { (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in
if (error != nil) {
println("API error: \(error), \(error.userInfo)")
}
var jsonError:NSError?
var json:NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &jsonError) as NSDictionary
if (jsonError != nil) {
println("Error parsing json: \(jsonError)")
}
else {
println(json)
}
})

我得到错误:

2014-09-28 18:27:39.075 JSON Parse[7009:1766181] NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9807)
API error: Error Domain=NSURLErrorDomain Code=-1202 "The certificate for this server is invalid. You might be connecting to a server that is pretending to be “www.googleapis.com” which could put your confidential information at risk." UserInfo=0x7f9e73a04fe0 {NSURLErrorFailingURLPeerTrustErrorKey=<SecTrustRef: 0x7f9e714391a0>, NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, _kCFStreamErrorDomainKey=3, _kCFStreamErrorCodeKey=-9807, NSErrorPeerCertificateChainKey=<CFArray 0x7f9e71700070 [0x103794970]>{type = immutable, count = 3, values = (
0 : <cert(0x7f9e71437710) s: *.googleapis.com i: Google Internet Authority G2>
1 : <cert(0x7f9e71437e50) s: Google Internet Authority G2 i: GeoTrust Global CA>
2 : <cert(0x7f9e71438810) s: GeoTrust Global CA i: Equifax Secure Certificate Authority>
)}, NSUnderlyingError=0x7f9e73805310 "The certificate for this server is invalid. You might be connecting to a server that is pretending to be “www.googleapis.com” which could put your confidential information at risk.", NSLocalizedDescription=The certificate for this server is invalid. You might be connecting to a server that is pretending to be “www.googleapis.com” which could put your confidential information at risk., NSErrorFailingURLKey=https://www.googleapis.com/books/v1/volumes?q=programming, NSErrorFailingURLStringKey=https://www.googleapis.com/books/v1/volumes?q=programming, NSErrorClientCertificateStateKey=0}, Optional([NSURLErrorFailingURLPeerTrustErrorKey: <SecTrustRef: 0x7f9e714391a0>, NSLocalizedRecoverySuggestion: Would you like to connect to the server anyway?, NSErrorFailingURLKey: https://www.googleapis.com/books/v1/volumes?q=programming, NSErrorFailingURLStringKey: https://www.googleapis.com/books/v1/volumes?q=programming, NSErrorPeerCertificateChainKey: <__NSCFArray 0x7f9e71700070>(
<cert(0x7f9e71437710) s: *.googleapis.com i: Google Internet Authority G2>,
<cert(0x7f9e71437e50) s: Google Internet Authority G2 i: GeoTrust Global CA>,
<cert(0x7f9e71438810) s: GeoTrust Global CA i: Equifax Secure Certificate Authority>
)
, NSErrorClientCertificateStateKey: 0, NSLocalizedDescription: The certificate for this server is invalid. You might be connecting to a server that is pretending to be “www.googleapis.com” which could put your confidential information at risk., _kCFStreamErrorDomainKey: 3, NSUnderlyingError: Error Domain=kCFErrorDomainCFNetwork Code=-1202 "The certificate for this server is invalid. You might be connecting to a server that is pretending to be “www.googleapis.com” which could put your confidential information at risk." UserInfo=0x7f9e738055a0 {NSErrorFailingURLStringKey=https://www.googleapis.com/books/v1/volumes?q=programming, NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, _kCFNetworkCFStreamSSLErrorOriginalValue=-9807, kCFStreamPropertySSLPeerCertificates=<CFArray 0x7f9e71700070 [0x103794970]>{type = immutable, count = 3, values = (
0 : <cert(0x7f9e71437710) s: *.googleapis.com i: Google Internet Authority G2>
1 : <cert(0x7f9e71437e50) s: Google Internet Authority G2 i: GeoTrust Global CA>
2 : <cert(0x7f9e71438810) s: GeoTrust Global CA i: Equifax Secure Certificate Authority>
)}, _kCFStreamPropertySSLClientCertificateState=0, kCFStreamPropertySSLPeerTrust=<SecTrustRef: 0x7f9e714391a0>, NSLocalizedDescription=The certificate for this server is invalid. You might be connecting to a server that is pretending to be “www.googleapis.com” which could put your confidential information at risk., _kCFStreamErrorDomainKey=3, NSErrorFailingURLKey=https://www.googleapis.com/books/v1/volumes?q=programming, _kCFStreamErrorCodeKey=-9807}, _kCFStreamErrorCodeKey: -9807])
fatal error: unexpectedly found nil while unwrapping an Optional value

API 调用在 PostMan 和浏览器中运行,没有任何错误。我假设谷歌会有有效的 SSL 证书!有谁知道我为什么会收到此错误,更重要的是,我该如何防止它。

最佳答案

基于 UIKit 导入,您的 Playground 平台是 iOS。我刚刚处理了与 iOS Playground 相同的问题。出于沮丧,我制作了一个新的 OS X Playground,剪切并粘贴了我的代码,我的请求运行良好。

关于ios - 解析 HTTPS JSON 数据错误 The Certificate for this server is invalid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26087616/

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