【问题标题】:NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813) iOSNSURLConnection/CFURLConnection HTTP 加载失败 (kCFStreamErrorDomainSSL, -9813) iOS
【发布时间】:2014-04-23 10:40:52
【问题描述】:

目前我正在使用 ios 中的块使用肥皂网络服务,我的源代码如下

NSString *xml = requestXMLToSent;

NSString *msgLength = [NSString stringWithFormat:@"%lu", (unsigned long)[xml length]];
NSURL *serviceURL = [NSURL URLWithString: url];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:serviceURL];

[urlRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[urlRequest addValue: serviceURL forHTTPHeaderField:@"SOAPAction"];
[urlRequest addValue:msgLength  forHTTPHeaderField:@"Content-Length"];
[urlRequest setHTTPBody: [xml dataUsingEncoding:NSUTF8StringEncoding]];
[urlRequest setHTTPMethod:@"POST"];

[NSURLConnection sendAsynchronousRequest:urlRequest queue:[[NSOperationQueue alloc]init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {


    if (connectionError == NULL) {

        NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse *) response;
        NSInteger statuscode = httpResponse.statusCode;
        if (statuscode == 200) {

            NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            NSLog(@"response String  : %@",responseString);


        }else{
            NSLog(@"%@",response);

        }




    }else{

        NSLog(@"There is an error in URL connection and the Error is : %@",connectionError);
    }

我收到以下错误@控制台

NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813)

URL 连接出错,错误为:Error Domain=NSURLErrorDomain Code=-1202“此服务器的证书无效。您可能正在连接一个伪装成“www.xxxxxxxx.net”的服务器” 这可能会使您的机密信息面临风险。” UserInfo=0x10948bbb0 {NSUnderlyingError=0x109470d10“此服务器的证书无效。您可能正在连接到伪装成“www.xxxxxx.net”的服务器,这可能会使您的机密信息面临风险。”,NSErrorFailingURLStringKey=https: // www.----------------------------------, NSErrorFailingURLKey=https: // ----- -------------------- NSLocalizedRecoverySuggestion=您是否仍要连接到服务器?, NSURLErrorFailingURLPeerTrustErrorKey=, NSLocalizedDescription=此服务器的证书无效。您可能正在连接到伪装成“www.xxxxxx.net”的服务器,这可能会使您的机密信息面临风险。}

【问题讨论】:

标签: ios ssl https ssl-certificate


【解决方案1】:

服务器抛出 SSL 证书错误。 为了测试,您可以在 appDelegate 中添加以下代码: + (BOOL)allowsAnyHTTPSCertificateForHost:(NSString *)host { return YES; }

这将绕过 SSL 错误

注意:适用于 NSURLConnection 和 UIWebView,但不适用于 WKWebView

已编辑:

对于 iOS 9,上述过程不起作用。在 info.plist 中添加如下 sn -p:

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

【讨论】:

  • 太棒了!你拯救了我的一天!
  • 这是非常有用的信息。当我开始为 iOS 9 编译时遇到了这个问题,要找出问题所在非常棘手。就是这样。
  • 这适用于 iOS 9.x,但在 iOS 10.x 上再次失败。有人知道 iOS 10 的分辨率吗?
  • 无法使其在 iOS 10.x 上运行。我没有在网上找到任何可行的解决方案。
【解决方案2】:

我假设您在 serviceURL 中使用 https 方案,并且您的测试服务器存在 SSL 证书问题。如果是这样并且您信任它,请在您的 NSURLConnection 委托中实现下一个方法:

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
    return YES;
}

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
    if([challenge.protectionSpace.host isEqualToString:@"127.0.0.1"] /*check if this is host you trust: */ )
       [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
}

让委托初始化您的NSURLConnection,例如使用initWithRequest:delegate:startImmediately: 方法。

【讨论】:

    【解决方案3】:

    检查您的代表是否实际被调用。

    本文档说明在某些情况下您的代表可能不会被调用。

    https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/URLLoadingSystem/Articles/AuthenticationChallenges.html

    重要提示:除非服务器响应包含 WWW-Authenticate 标头,否则 URL 加载系统类不会调用其委托来处理请求质询。其他身份验证类型,例如代理身份验证和 TLS 信任验证,不需要此标头。

    【讨论】:

      【解决方案4】:

      在 iOS 9.0 中将以下内容添加到 info.plist

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

      【讨论】:

      • 盲目地这样做而不了解其含义是一种危险的解决方案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-06
      • 1970-01-01
      • 2014-01-27
      • 2016-04-24
      • 2016-10-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多