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

Sum*_*tel 10 ssl https ssl-certificate ios

目前我在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);
    }
Run Code Online (Sandbox Code Playgroud)

我收到以下错误@ console

NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813)
Run Code Online (Sandbox Code Playgroud)

URL连接出错,错误是:错误域= NSURLErrorDomain代码= -1202"此服务器的证书无效.您可能正在连接到假装为"www.xxxxxxxx.net"的服务器,这可能是将您的机密信息置于危险之中." UserInfo = 0x10948bbb0 {NSUnderlyingError = 0x109470d10"此服务器的证书无效.您可能连接到假冒"www.xxxxxx.net"的服务器,这可能会使您的机密信息面临风险.",NSErrorFailingURLStringKey = https: // www .----------------------------------,NSErrorFailingURLKey = https:// ----- -------------------- NSLocalizedRecoverySuggestion =您是否还要连接到服务器?,NSURLErrorFailingURLPeerTrustErrorKey =,NSLocalizedDescription =此服务器的证书无效.您可能正在连接到假装为"www.xxxxxx.net"的服务器,这可能会使您的机密信息面临风险.}

错误的屏幕截图

小智 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>
Run Code Online (Sandbox Code Playgroud)

  • 这适用于iOS 9.x,但在iOS 10.x上它再次失败.有谁知道iOS 10的分辨率? (4认同)

小智 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];
}
Run Code Online (Sandbox Code Playgroud)

让代理人NSURLConnection使用initWithRequest:delegate:startImmediately:方法初始化您的示例.