Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
556 views
in Technique[技术] by (71.8m points)

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

Currently I am consuming a soap web service using block in ios my source code is as follows

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);
    }

I am getting the following error @ console

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

There is an error in URL connection and the Error is : Error Domain=NSURLErrorDomain Code=-1202 "The certificate for this server is invalid. You might be connecting to a server that is pretending to be “www.xxxxxxxx.net” which could put your confidential information at risk." UserInfo=0x10948bbb0 {NSUnderlyingError=0x109470d10 "The certificate for this server is invalid. You might be connecting to a server that is pretending to be “www.xxxxxx.net” which could put your confidential information at risk.", NSErrorFailingURLStringKey=https: // www.----------------------------------, NSErrorFailingURLKey=https: // ------------------------- NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, NSURLErrorFailingURLPeerTrustErrorKey=, NSLocalizedDescription=The certificate for this server is invalid. You might be connecting to a server that is pretending to be “www.xxxxxx.net” which could put your confidential information at risk.}

Screenshot of error

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Server is throwing SSL certificate error. For the sake of testing, you can add the following code to the appDelegate: + (BOOL)allowsAnyHTTPSCertificateForHost:(NSString *)host { return YES; }

This will bypass the SSL error

Note: works for NSURLConnection & UIWebView, but not for WKWebView

Edited:

For iOS 9, above procedure don't work. Add the following snippet in info.plist:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...