【问题标题】:CFNetwork SSLHandshake failed (-9824) NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9824)CFNetwork SSLHandshake 失败 (-9824) NSURLSession/NSURLConnection HTTP 加载失败 (kCFStreamErrorDomainSSL, -9824)
【发布时间】:2015-10-22 08:08:45
【问题描述】:

我正在使用 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>

它可以在真实设备上运行,但不能在模拟器上运行

【问题讨论】:

    标签: ios ios-simulator ios9


    【解决方案1】:
    1. 从 NSURLConnection 到 NSURLSession 对我有用

    我能够解决如下(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 中,请参阅文档:

    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. 最终

    公告:CFNetwork SSLHandshake 在将登录与适用于 iOS 的 Amazon SDK 集成时失败 (-9824) 返回分类 返回分类

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

    只需从 api.amazon.com 更改为 yourdomain.net

    希望对你有帮助。

    【讨论】:

      【解决方案2】:

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

      1. 在 info.plist 中添加/编辑

      <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

      - (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];
      }
      

      希望这会对你有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-12-23
        • 2015-09-04
        • 1970-01-01
        • 2016-10-12
        • 2016-01-31
        • 2016-10-19
        • 2016-01-03
        • 1970-01-01
        相关资源
        最近更新 更多