10

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

Jayprakash Dubey
  • 35,723
  • 18
  • 170
  • 177
Sumit Patel
  • 2,547
  • 8
  • 33
  • 45
  • http://stackoverflow.com/questions/19941939/fetching-an-image-from-a-https-link – iPatel Apr 23 '14 at 10:44
  • @iPatel How can i sort it out if I am using block [NSURLConnection sendAsynchronousRequest:<#(NSURLRequest *)#> queue:<#(NSOperationQueue *)#> completionHandler:<#^(NSURLResponse *response, NSData *data, NSError *connectionError)handler#>] – Sumit Patel Apr 23 '14 at 10:47

4 Answers4

13

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>
rckoenes
  • 69,092
  • 8
  • 134
  • 166
Shanu ji
  • 359
  • 4
  • 5
9

I assume you use https scheme in your serviceURL and your test server has problems with SSL certificate. If so and you trust it, implement next methods in your NSURLConnection delegate's:

- (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];
}

To have a delegate initialize your NSURLConnection for example with initWithRequest:delegate:startImmediately: method.

Axel Guilmin
  • 11,454
  • 9
  • 54
  • 64
user2260054
  • 522
  • 1
  • 3
  • 11
0

Check your delegates are actually being called.

This documentation explains that your delegates may not be getting called in certain circumstances.

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

Important: The URL loading system classes do not call their delegates to handle request challenges unless the server response contains a WWW-Authenticate header. Other authentication types, such as proxy authentication and TLS trust validation do not require this header.

0

In iOS 9.0 add the following to info.plist

<key>NSAppTransportSecurity</key>  
     <dict>  
          <key>NSAllowsArbitraryLoads</key><true/>  
     </dict>  
amir
  • 1,332
  • 8
  • 15