NSURLConnection/CFURLConnection HTTP 加载失败 (kCFStreamErrorDomainSSL, -9813) iOS

10

目前我正在iOS中使用块来消费一个SOAP Web服务,我的源代码如下:

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连接存在错误,错误提示为: 错误域=NSURLErrorDomain,错误代码=-1202,“此服务器的证书无效。您可能正在连接到一个假冒成“www.xxxxxxxx.net”的服务器,这可能会危及您的机密信息。” UserInfo=0x10948bbb0 {NSUnderlyingError=0x109470d10 “此服务器的证书无效。您可能正在连接到一个假冒成“www.xxxxxx.net”的服务器,这可能会危及您的机密信息。”,NSErrorFailingURLStringKey=https://www.----------------------------------, NSErrorFailingURLKey=https://-------------------------,NSLocalizedRecoverySuggestion=是否仍要连接到服务器?,NSURLErrorFailingURLPeerTrustErrorKey=,NSLocalizedDescription=此服务器的证书无效。您可能正在连接到一个假冒成“www.xxxxxx.net”的服务器,这可能会危及您的机密信息。}

错误截图


https://dev59.com/-3nZa4cB1Zd3GeqPlRDt - iPatel
@iPatel 如果我正在使用block,该如何进行排序呢? [NSURLConnection sendAsynchronousRequest:<#(NSURLRequest *)#> queue:<#(NSOperationQueue *)#> completionHandler:<#^(NSURLResponse *response, NSData *data, NSError *connectionError)handler#>] - Sumit Patel
4个回答

13

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

这将绕过SSL错误。

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

编辑:

对于iOS 9,上述过程不起作用。请在 info.plist 中添加以下代码片段:

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

惊人!你救了我的一天!! - Joan Cardona
这是非常有用的信息。当我开始为iOS 9编译时,遇到了这个问题,而找出问题的原因非常棘手。就是这个问题。 - DanM
4
这适用于iOS 9.x,但在iOS 10.x上又失败了。有人知道iOS 10的解决方法吗? - billy_comic
无法在iOS 10.x上使其工作。我在网上没有找到任何可行的解决方案。 - FutoRicky

9

我猜测您在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];
}

例如使用initWithRequest:delegate:startImmediately:方法,让代理初始化您的NSURLConnection


0
在iOS 9.0中,将以下内容添加到info.plist文件。
<key>NSAppTransportSecurity</key>  
     <dict>  
          <key>NSAllowsArbitraryLoads</key><true/>  
     </dict>  

7
不经理解后盲目行动是个危险的解决方案。 - Erik Kerber

0

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