jQuery Cordova 应用程序中的 HTTP 加载失败(kCFStreamErrorDomainSSL,-9813)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21025622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 01:24:02  来源:igfitidea点击:

HTTP load failed (kCFStreamErrorDomainSSL, -9813) in Cordova app

jqueryiosiphonexcodecordova

提问by Martin Cassidy

I've created an iOS app using Cordova 3.2 and jquery mobile 1.3. I use jquery ajax to send requests to a RESTfull service to retrieve/update information.

我使用 Cordova 3.2 和 jquery mobile 1.3 创建了一个 iOS 应用程序。我使用 jquery ajax 向 RESTfull 服务发送请求以检索/更新信息。

I have a number of test devices, various iPhones, with various different iOS versions. On an iPhone 4S, running iOS 7 I receive the following error when any ajax request is sent:

我有许多测试设备,各种 iPhone,以及各种不同的 iOS 版本。在运行 iOS 7 的 iPhone 4S 上,当发送任何 ajax 请求时,我收到以下错误:

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

This only happens on this device, on another iPhone 4S, also running iOS 7, it works absolutely fine, as it does on all of the other test devices I'm using.

这只发生在这个设备上,在另一台运行 iOS 7 的 iPhone 4S 上,它工作得非常好,就像在我使用的所有其他测试设备上一样。

I've read that this indicates the certificate is invalid in someway, that being the case, can this be ignored and why would affect on one device?

我读过这表明证书在某种程度上无效,在这种情况下,可以忽略这一点,为什么会影响一台设备?

Can anyone suggest why this might be happening and what could be different about the device that would prevent it from working only on one of my devices. Thanks

任何人都可以提出为什么会发生这种情况以及设备可能会阻止它仅在我的一台设备上工作的不同之处。谢谢

采纳答案by Arkaaito

If it's working on some devices and not others, there's some difference in the certificate validation parameters for those devices. Here are some things to look at:

如果它适用于某些设备而不是其他设备,则这些设备的证书验证参数会有所不同。以下是一些需要注意的事项:

  1. Do the happy devices have some root certificate that the unhappy devices don't? Particularly if they are running different iOS versions, they may have different lists of trusted certificates as well. Try going to Settings > General > Aboutand compare the numbers for "Trust Store" at the very bottom. See more info here.
  2. Did you manually accept some intermediate certificate (used to sign this certificate) on the happy devices at some point in the past?*
  3. Is the date/time wrong on the unhappy device? If it's set to a time after the certificate expiry date or before the certificate issue date, it will reject it. I'm always shooting myself in the foot with this one!
  1. 快乐的设备是否有一些不快乐的设备没有的根证书?特别是如果他们运行不同的 iOS 版本,他们可能也有不同的受信任证书列表。尝试转到“设置”>“常规”>“关于”并比较最底部“信任商店”的数字。在此处查看更多信息。
  2. 您是否在过去的某个时候手动接受了一些快乐设备上的中间证书(用于签署此证书)?*
  3. 不满意的设备上的日期/时间是否错误?如果设置为证书到期日期之后或证书颁发日期之前的时间,它将拒绝它。我总是用这个射自己的脚!

If you access your endpoint URL in the browser on the unhappy device, what error does it give you? (Note: obviously your server will give you some error as well because you aren't requesting with the right headers and params. I'm talking about Safari itself, though - it should give a more detailed message about what (it thinks) is wrong with the certificate.)

如果您在不满意的设备上的浏览器中访问您的端点 URL,它会给您带来什么错误?(注意:显然您的服务器也会给您一些错误,因为您没有使用正确的标头和参数进行请求。不过,我在谈论 Safari 本身 - 它应该提供有关(它认为)是什么的更详细信息证书有误。)

*Edited to correct something I misremembered: you do nothave the option to accept the leaf certificate if you go to the URL in Safari, you only have the option to accept intermediate certificates if you attempt to load them directly.

*编辑以更正我记错的内容:如果您转到 Safari 中的 URL,则无法选择接受叶证书,如果您尝试直接加载它们,则只能选择接受中间证书。

回答by Markiv

I had a similar issue, but in my case, across all devices this used to happen and only during an ajax call with https.

我有一个类似的问题,但在我的情况下,这曾经发生在所有设备上,并且仅在使用 https 的 ajax 调用期间发生。

If that is your case, adding the below code at the end of appDelegate.m file will mitigate your issue.

如果是这种情况,在 appDelegate.m 文件末尾添加以下代码将缓解您的问题。

@implementation NSURLRequest(DataController) 
+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString *)host 
{ 
return YES; 
} 
@end 

But i do doubt that if it is working in other devices, the solution i have given, might not be effective. But do give it a try.

但我怀疑如果它在其他设备上工作,我给出的解决方案可能不会有效。但一定要试一试。

回答by Chris

The Swift version of Markivs answer(taken from here):

Markivs 答案的 Swift 版本(取自此处):

extension NSURLRequest {
    static func allowsAnyHTTPSCertificateForHost(host: String) -> Bool {
        return true
    }
}

回答by Brewster

Xcode 7.3 Swift 2.2

Xcode 7.3 斯威夫特 2.2

To get this to work I had to do 3 things:

为了让它发挥作用,我必须做三件事:

  1. I had to add Chris's answer^ to the bottom of the AppDelegate.swift after the closing } for the class
  2. then modify Info.plist using this answer https://stackoverflow.com/a/32701693
  3. Make sure you set a domain name for the server instead of an IP Address
  1. 在关闭 } 之后,我必须将 Chris 的答案^ 添加到 AppDelegate.swift 的底部
  2. 然后使用此答案修改 Info.plist https://stackoverflow.com/a/32701693
  3. 确保为服务器设置域名而不是 IP 地址

回答by user3540599

I got the same issue while running the application in simulator .the reason behind this is there is no trusted certificate in the simulator.

我在模拟器中运行应用程序时遇到了同样的问题。这背后的原因是模拟器中没有受信任的证书。

simply drag & drop your .cer Files into your running Simulator window. You'll see Safari flashing and then the import dialog for your Certificate (or Certificate Authority)...

只需将您的 .cer 文件拖放到正在运行的模拟器窗口中即可。您将看到 Safari 闪烁,然后是您的证书(或证书颁发机构)的导入对话框...