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

时间:2015-07-27 19:57:42

标签: ios ios9

我正在更新我的应用以适应Apple的新ATS。在没有对Plist-Info进行任何更改的情况下,以下代码在vanilla`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];

错误:

  

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

有关这个问题可能背后的想法吗?

Ps :我了解NSURLConnection已被弃用。但是,如果我在AllowArbitraryLoads中添加Plist,则会发现此调用有效。

6 个答案:

答案 0 :(得分:13)

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

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

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

答案 1 :(得分:9)

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

Add a new row in your plist file

答案 2 :(得分:6)

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

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

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

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

答案 3 :(得分:1)

将以下内容添加到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>

答案 4 :(得分:0)

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

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

// test
describe('CodeHandler', function() {
  var req = {
    query: {
      code: 'test-code',
      key: 'test-state'
    }
  };

  var res = {
    redirect: function() {}
  };

  var expectedUrl = 'https://test.tes';
  var ch;

  beforeEach(function() {
    sinon.stub(UserManager, 'saveCode').returns(
      new RSVP.Promise(function(resolve, reject){
        resolve();
      })
    );

    sinon.stub(res, 'redirect');

    ch = CodeHandler(UserManager);
  });

  afterEach(function() {
    UserManager.saveCode.restore();
    res.redirect.restore();
  });

  it('redirects to the expected URL', function(){
    ch.oAuthCallback(req, res);
    assert(res.redirect.calledWith(expectedUrl));
  })
});

答案 5 :(得分:0)

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


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