CFNetwork SSL握手失败(-9824) NSURLSession/NSURLConnection HTTP加载失败(kCFStreamErrorDomainSSL,-9824)

7

我正在使用以下代码在iOS 9中向https服务器发送POST请求:

[NSURLConnection sendSynchronousRequest:request returningResponse:&requestResponse error:&err];  

但是我遇到了以下错误
CFNetwork SSLHandshake failed (-9824)
NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9824)

我尝试将以下异常添加到info.plist中:

我尝试将以下异常添加到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个回答

4
  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];

从 NSURLConnection 到 NSURLSession

  1. 在 Info.plist 中也包含了相关内容,具体请参考文档:

Info.plist 参考手册

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

公告:在iOS集成Login with Amazon SDK时,CFNetwork SSLHandshake失败(-9824) 返回类别 返回类别

在iOS集成Login with Amazon SDK时,CFNetwork SSLHandshake失败(-9824) 返回类别 返回类别

只需将api.amazon.com更改为yourdomain.net。

希望它有所帮助。


2
以下操作解决了我的问题:
  1. 在info.plist文件中添加/编辑:

<key>NSAppTransportSecurity</key> <dict> <key>NSExceptionDomains</key> <dict> <key>你的域名.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];
}

希望这能对您有所帮助。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接