iOS 9 中的 HTTPS 请求:NSURLSession/NSURLConnection HTTP 加载失败(kCFStreamErrorDomainSSL,-9802)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31662134/
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-31 07:00:44  来源:igfitidea点击:

HTTPS request in iOS 9 : NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802)

iosios9

提问by Vignesh Murugesan

I'm updating my app to accommodate Apple's new ATS. Without any changes to the Plist-Info,the following code throws an error at sendSynchronousRequest()in a vanilla `iOS 9 simulator.

我正在更新我的应用程序以适应 Apple 的新ATS。没有对 Plist-Info 进行任何更改,以下代码sendSynchronousRequest()在 vanilla `iOS 9 模拟器中抛出错误。

NSURL *url  =[NSURL URLWithString:@"https://Google.com"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
[request setHTTPMethod:@"GET"];
[request setURL:url];

NSURLResponse *urlResponse = nil;
NSError *error = nil;    
NSData *reponse = [NSURLConnection sendSynchronousRequest:request
                                        returningResponse:&urlResponse
                                                    error:&error];

Error:

错误:

NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802)

NSURLSession/NSURLConnection HTTP 加载失败(kCFStreamErrorDomainSSL,-9802)

Any thoughts as to what might be behind this issue?

关于这个问题背后可能有什么想法?

Ps: I understand that NSURLConnectionis deprecated. But this invocations works find if I add AllowArbitraryLoadsin Plist.

Ps:我知道这NSURLConnection已被弃用。但这种调用作品发现,如果我加AllowArbitraryLoadsPlist

采纳答案by Vignesh Murugesan

NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802) corresponds to the server not supporting "Forward Secrecy".

NSURLSession/NSURLConnection HTTP 加载失败 (kCFStreamErrorDomainSSL, -9802) 对应于不支持“Forward Secrecy”的服务器。

To work around this, add a domain exception to .plist file as follows:

要解决此问题,请向 .plist 文件添加域异常,如下所示:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>test.testdomain.com</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSTemporaryExceptionRequiresForwardSecrecy</key>
            <false/>
        </dict>
    </dict>
</dict>

回答by pkc456

Add a new row in your plist file.

在 plist 文件中添加一个新行。

Add a new row in your plist file

在 plist 文件中添加新行

回答by delarcomarta

I added this code in the info.plist to allow any request http:

我在 info.plist 中添加了此代码以允许任何请求 http:

 <key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

This article lists all the changes made by Apple for iOS 9 and their implementations:

本文列出了 Apple 对 iOS 9 所做的所有更改及其实现:

http://ste.vn/2015/06/10/configuring-app-transport-security-ios-9-osx-10-11/

http://ste.vn/2015/06/10/configuring-app-transport-security-ios-9-osx-10-11/

回答by Lazy

Add the following to the info.plist file. And replace 'My_Base_Url.com' with your web service link's base url. This should do the trick.

将以下内容添加到 info.plist 文件中。并将“My_Base_Url.com”替换为您的网络服务链接的基本网址。这应该可以解决问题。

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>My_Base_Url.com</key>
        <dict>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSExceptionMinimumTLSVersion</key>
            <string>TLSv1.2</string>
            <key>NSExceptionRequiresForwardSecrecy</key>
            <true/>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSRequiresCertificateTransparency</key>
            <false/>
            <key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
            <false/>
            <key>NSThirdPartyExceptionMinimumTLSVersion</key>
            <string>TLSv1.2</string>
            <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
            <true/>
        </dict>
    </dict>
</dict>

回答by scorpiozj

If your app includes H5 page, sometimes it also will have this error.
It doesn't only require to turn on Allow Arbitrary Loadsto fix it, but also require to add code below in your appDelegate.m:

如果您的应用包含 H5 页面,有时也会出现此错误。
它不仅需要打开Allow Arbitrary Loads才能修复它,还需要在您的 appDelegate.m 中添加以下代码:


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

回答by spirographer

According to this: https://forums.developer.apple.com/message/36842#36842

根据这个:https: //forums.developer.apple.com/message/36842#36842

The correct exception to fix HTTP load failed (kCFStreamErrorDomainSSL, -9802) is:

修复 HTTP 加载失败的正确异常 (kCFStreamErrorDomainSSL, -9802) 是:

NSExceptionAllowsInsecureHTTPLoads