简体   繁体   English

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

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

I'm updating my app to accommodate Apple's new ATS . 我正在更新我的应用程序以适应Apple的新ATS Without any changes to the Plist-Info,the following code throws an error at sendSynchronousRequest() in a vanilla `iOS 9 simulator. 在未对Plist-Info进行任何更改的情况下,以下代码在sendSynchronousRequest() iOS 9模拟器中的sendSynchronousRequest()处引发了错误。

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 NSURLConnection is deprecated. PS:据我所知, NSURLConnection被弃用。 But this invocations works find if I add AllowArbitraryLoads in Plist . 但这种调用作品发现如果我添加AllowArbitraryLoadsPlist

NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802) corresponds to the server not supporting "Forward Secrecy". NSURLSession / NSURLConnection HTTP加载失败(kCFStreamErrorDomainSSL,-9802)对应于不支持“转发保密性”的服务器。

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>

Add a new row in your plist file. 在您的plist文件中添加新行。

在您的plist文件中添加新行

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/

Add the following to the info.plist file. 将以下内容添加到info.plist文件中。 And replace 'My_Base_Url.com' with your web service link's base url. 并用您的Web服务链接的基本URL替换“ My_Base_Url.com”。 This should do the trick. 这应该可以解决问题。

<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>

If your app includes H5 page, sometimes it also will have this error. 如果您的应用程序包含H5页面,则有时也会出现此错误。
It doesn't only require to turn on Allow Arbitrary Loads to fix it, but also require to add code below in your appDelegate.m: 它不仅需要打开“ Allow Arbitrary Loads来修复它,还需要在appDelegate.m中添加以下代码:


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

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 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在iOS 9中NSURLSession / NSURLConnection HTTP加载失败(kCFStreamErrorDomainSSL,-9802) - NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802) in ios 9 NSURLSession / NSURLConnection HTTP加载失败(kCFStreamErrorDomainSSL,-9802)错误 - The NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802) error 查找NSURLSession / NSURLConnection的URL HTTP加载失败(kCFStreamErrorDomainSSL,-9802)错误 - Find URL for NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802) error 子域上的NSURLSession / NSURLConnection HTTP加载失败(kCFStreamErrorDomainSSL,-9802)? - NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802) on a subdomain? 警告:NSURLSession / NSURLConnection HTTP加载失败(kCFStreamErrorDomainSSL,-9802) - Warning: NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802) Xcode 7.3:NSURLSession/NSURLConnection HTTP 加载失败(kCFStreamErrorDomainSSL,-9802) - Xcode 7.3: NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802) NSURLSession / NSURLConnection HTTP加载失败(kCFStreamErrorDomainSSL,-9802)Xamarin.Forms IOS - NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802) Xamarin.Forms IOS NSURLSession/NSURLConnection HTTP 加载失败 (kCFStreamErrorDomainSSL, -9802 in iOS10, 2017 - NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802 in iOS10, 2017 ios9 HTTP加载失败kCFStreamErrorDomainSSL(-9802) - ios9 HTTP load failed kCFStreamErrorDomainSSL (-9802) 使用Xcode 7.1的iOS 9中的NSURLSession / NSURLConnection HTTP加载失败(kCFStreamErrorDomainSSL,-9813) - NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813) in iOS 9 with Xcode 7.1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM