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

时间:2014-09-28 17:31:44

标签: ios authentication ssl https swift

在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和浏览器中有效,没有任何错误。我认为Google会拥有有效的SSL证书!有谁知道为什么我会得到这个错误,而且更为重要的是,我怎么能阻止它。

1 个答案:

答案 0 :(得分:0)

基于UIKit导入,您的Playground平台是iOS。我刚刚通过iOS Playground解决了同样的问题。出于沮丧,我创建了一个新的OS X Playground,剪切并粘贴了我的代码,我的请求工作正常。