CFNetwork SSLHandshake失败(-9824)NSURLSession / NSURLConnection HTTP加载失败(kCFStreamErrorDomainSSL,-9824)

时间:2015-10-22 08:08:46

标签: ios ios-simulator ios9

我正在使用iOS 9中的以下代码向https服务器

发送帖子请求
[NSURLConnection sendSynchronousRequest:request returningResponse:&requestResponse error:&err];  

但是我收到以下错误

CFNetwork SSLHandshake failed (-9824)
NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9824)

我尝试将异常添加到info.plist中,如下所示:

<key>NSAppTransportSecurity</key>  
<dict>
    <key>NSExceptionDomains</key>
    <dict>
    <key>www.myserver.com</key>
    <dict>
    <key>NSIncludesSubdomains</key>
    <true/>
    <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
    <true/>
    <key>NSTemporaryExceptionMinimumTLSVersion</key>
    <string>TLSv1.1</string>
</dict>

我也试过

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

它适用于真实设备但不适用于模拟器

2 个答案:

答案 0 :(得分:4)

  1.   

    从NSURLConnection到NSURLSession为我工作

  2. 我能够解决如下问题(不推荐使用NSURLConnection,你需要使用NSURLSession):

    NSURL *URL = [NSURL URLWithString:@"http://example.com"];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    
    [NSURLConnection sendAsynchronousRequest:request
                                    queue:[NSOperationQueue mainQueue]
                        completionHandler:^(NSURLResponse *response, NSData  *data, NSError *error) {
     // ... 
    }];
    

    转换为:

    NSURL *URL = [NSURL URLWithString:@"http://example.com"];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request
                                         completionHandler:
     ^(NSData *data, NSURLResponse *response, NSError *error) {
         // ...
      }];
    
    [task resume];
    

    From NSURLConnection to NSURLSession

    1. 还包含在Info.plist中,请参阅文档:
    2. Info.plist reference

      <key>NSAppTransportSecurity</key>
      <dict>
        <key>NSExceptionDomains</key>
        <dict>
        <key>yourdomain.net</key>
        <dict>
        <key>NSIncludesSubdomains</key>
        <true/>
        <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
        <true/>
        <key>NSTemporaryExceptionMinimumTLSVersion</key>
        <string>1.2</string>
        <key>NSTemporaryExceptionRequiresForwardSecrecy</key>
        <false/>
        </dict>
        </dict>
      </dict>
      
      1. 最终
      2.   

        公告:CFNetwork SSLHandshake失败(-9824),同时将登录与Amazon SDK for iOS集成   返回分类返回分类

        CFNetwork SSLHandshake failed (-9824) while integrating Login with Amazon SDK for iOS Back to Category Back to Category

        从api.amazon.com改为yourdomain.net

        希望它有所帮助。

答案 1 :(得分:1)

执行以下操作解决了我的问题:

  1. 在info.plist中添加/编辑
  2. <key>NSAppTransportSecurity</key> <dict> <key>NSExceptionDomains</key> <dict> <key>yourdomain.com</key> <dict> <key>NSIncludesSubdomains</key> <true/> <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key> <true/> <key>NSTemporaryExceptionMinimumTLSVersion</key> <string>1.2</string> <key>NSTemporaryExceptionRequiresForwardSecrecy</key> <false/> </dict> </dict> </dict>

    1. 在您的类中添加以下代码,该代码委派NSURLConnection
    2. - (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpac { return YES; }

      - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
          NSArray *trustedHosts = [NSArray arrayWithObjects:@"mytrustedhost",nil];
      
          if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]){
              if ([trustedHosts containsObject:challenge.protectionSpace.host]) {
                  [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
              }
          }
          [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
      }
      

      希望这会对你有所帮助。