首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >解析HTTPS JSON数据错误此服务器的证书无效

解析HTTPS JSON数据错误此服务器的证书无效
EN

Stack Overflow用户
提问于 2014-09-29 01:31:44
回答 1查看 1.3K关注 0票数 1

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

代码语言:javascript
运行
复制
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)
    }
})

我得到了错误:

代码语言:javascript
运行
复制
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和浏览器中都可以正常工作,没有任何错误。我假设Google应该有有效的SSL证书!有没有人知道我为什么会出现这个错误,更重要的是,我如何防止它。

EN

回答 1

Stack Overflow用户

发布于 2014-11-03 13:27:47

基于UIKit导入,您的游乐场的平台是iOS。我刚刚用iOS游乐场解决了同样的问题。出于沮丧,我做了一个新的OS X Playground,剪切并粘贴了我的代码,我的请求运行得很好。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26087616

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档