NSURLConnection HTTP 加载失败(kCFStreamErrorDomainSSL,-9813)[重复]

Posted

技术标签:

【中文标题】NSURLConnection HTTP 加载失败(kCFStreamErrorDomainSSL,-9813)[重复]【英文标题】:NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813) [duplicate] 【发布时间】:2016-03-14 10:10:05 【问题描述】:

当我使用http://domain.com 时,我的应用程序运行良好。但是今天,当我将http:// 更改为https:// 时,我遇到了这个问题:NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813)。我还对.plist 进行了更改。这是我的.plist 代码:

<key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
        <key>NSExceptionDomains</key>
        <dict>
            <key>dev.domainName.in</key>
            <dict>
                <key>NSExceptionAllowsInsecureHTTPLoads</key>
                <true/>
                <key>NSIncludesSubdomains</key>
                <true/>
                <key>NSTemporaryExceptionMinimumTLSVersion</key>
                <string>TLSv1.2</string>
            </dict>
        </dict>
    </dict>

但我仍然面临这个问题。请帮帮我。我错过了什么?

【问题讨论】:

您可以使用设备的浏览器调用相同的 URL 吗? 它给了我一个错误:无法验证服务器身份 您是在 Web 视图中打开此 url 还是使用它来使用 NSURLSession 或 NSURLConnection 类进行网络 api 调用?如果可以的话,你介意分享完整的网址吗?您似乎需要处理身份验证挑战,因为您看到的错误是因为证书身份不明。在这种情况下,您必须实现在连接期间调用的某些委托方法并相应地处理它们。 我正在使用 NSURLConnection 类 【参考方案1】:

ios 9 开始,Apple 已强制执行 ATS。 Here 是相关的文档。尝试放松限制,看看它是否有效。

它是否适用于运行 iOS 9 之前的操作系统版本的设备?

【讨论】:

我已阅读此文档并使用了他们提供的代码。但它仍然给我一个错误。 尝试使用此站点检查您的证书是否有任何问题。 sslshopper.com/ssl-checker.html 证书是自签名的。除非手动将证书作为可信证书添加到他们的 Web 浏览器,否则用户在访问此站点时会收到警告。您可以通过购买受信任的 SSL 证书来修复此错误 这就是问题所在。您必须使用自签名证书。您应该从证书颁发机构购买 SSL 证书。您也可以将其添加到设备的根证书列表中。但是,您必须自己管理分发证书。我建议您购买 SSL 证书。 如何将其添加到设备的根证书列表中?【参考方案2】:

根据您的 cmets,您收到无效证书错误并且您正在使用 NSURLConnection,您的解决方案是实现这些委托。看看这是否能解决问题。

- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];

请注意,您实际上忽略了 SSL 错误并允许连接继续进行。因此,如果您要打开应用程序进行黑客攻击。对于测试,您可以使用它,但请确保在生产环境中安装正确的证书。

但是,如果您想正确处理此问题并在证书不正确的情况下警告用户,您可以这样做。

OSStatus err = noErr;
BOOL trusted = NO;
NSURLProtectionSpace *  protectionSpace = challenge.protectionSpace;
SecTrustRef serverTrustRef = protectionSpace.serverTrust;
SecTrustResultType trustResult;

//check if the server trust that we got from the server can be trusted by default
err = SecTrustEvaluate(serverTrustRef, &trustResult);
trusted = (err == noErr) && ((trustResult == kSecTrustResultProceed) || (trustResult == kSecTrustResultUnspecified));

if (trusted) //if the site is trusted then continue with that trust

    [challenge.sender useCredential:[NSURLCredential credentialForTrust:protectionSpace.serverTrust]
          forAuthenticationChallenge:challenge];

else //if not then warn the user about this and let the user make a decision

    //warn the user about the cert problem using a dialog with options to continue or ignore.
   //Continue alert action call : [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]
          forAuthenticationChallenge:challenge];
   //Dont continue action: [challenge.sender continueWithoutCredentialForAuthenticationChallenge:_challenge]; 
   //OR call: [sender cancelAuthenticationChallenge:challenge];

【讨论】:

我应该在哪里写这段代码? 在你的 NSURLConnectionDelegate 中。放一些 NSLog 调用,看看控件是否也来了,供您参考。 没有调用委托。 您必须将委托设置为 NSURLConnection 对象。它可能是您的控制器,也可能是您调用 NSURLConnection 方法的单独对象 在 UIViewController 中,我使用这个代码: [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler: ^(NSURLResponse *response, NSData *data, NSError *connectionError)

以上是关于NSURLConnection HTTP 加载失败(kCFStreamErrorDomainSSL,-9813)[重复]的主要内容,如果未能解决你的问题,请参考以下文章

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

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

尝试使用 Alamofire 下载图像时,NSURLSession/NSURLConnection HTTP 加载失败

NSURLSession/NSURLConnection HTTP 加载失败 (kCFStreamErrorDomainSSL, -9802) Xamarin.Forms IOS

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

ios 9.2 中的 NSURLSession/NSURLConnection HTTP 加载失败 (kCFStreamErrorDomainSSL, -9802)