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

目前我正在使用块在iOS中消耗肥皂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连接中有一个错误,错误是: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”的服务器,这可能会危及您的机密信息。}

错误的屏幕截图

服务器正在抛出SSL证书错误。 为了testing,可以将下面的代码添加到+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString *)host { return YES; } + (BOOL)allowsAnyHTTPSCertificateForHost:(NSString *)host { return YES; }

这将绕过SSL错误

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

编辑:

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

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

我假设你在你的serviceURL中使用httpsscheme,你的testing服务器在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:方法。

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

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

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

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

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

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