0

I am trying to get data from a secure restful service. I follow many other posts alongwith

How to use NSURLConnection to connect with SSL for an untrusted cert?

But in my case didReceiveAuthenticationChallenge and canAuthenticateAgainstProtectionSpace are not get called.

please help if you could solve the problem or provide me some good example to call secure restful service.

 NSURL *url = [NSURL URLWithString:@"https://76.69.53.126:8443/jaxrs/tdgateway/getCountries"];


NSError * error = nil;   
NSMutableURLRequest *urlRequest=[NSMutableURLRequest requestWithURL:url];
[ NSURLConnection sendSynchronousRequest: urlRequest returningResponse: nil error: &error ];

NSLog(@"This is %@",url);




- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
    NSLog(@"This is canAuthenticateAgainstProtectionSpace");
    return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
    NSLog(@"This is didReceiveAuthenticationChallenge");
    [[challenge sender] cancelAuthenticationChallenge:challenge];
}  
Community
  • 1
  • 1
Azhar
  • 20,500
  • 38
  • 146
  • 211

2 Answers2

0

It could be different than NSURLAuthenticationMethodServerTrust, so just try this to start:

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
    return YES;
}

You should not cancel it immediately:

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
    NSLog(@"This is didReceiveAuthenticationChallenge");
    [[challenge sender] cancelAuthenticationChallenge:challenge];
}  
Rui Peres
  • 25,741
  • 9
  • 87
  • 137
0
NSString *XAPIKEY = @"myapikey";
NSString *LOGINUSER = @"login username";
NSString *LOGINPASS = @"passwords";

// Setup NSURLConnection
- (void) postRequest:(NSString *) requestFun requestData:(NSDictionary *) sendDataDic{
    NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", @"http://api.domain.com/index.php/api/", requestFun]];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL
                                             cachePolicy:NSURLRequestUseProtocolCachePolicy
                                         timeoutInterval:30.0];
    [request setValue:@"application/x-www-form-urlencoded; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
    [request setValue:XAPIKEY forHTTPHeaderField:@"X-API-KEY"];
    if(sendDataDic != nil){
        NSString *stringData = [self urlEncodedString:sendDataDic];
        [request setHTTPBody:[stringData dataUsingEncoding:NSUTF8StringEncoding]];
        [request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[stringData length]] forHTTPHeaderField:@"Content-Length"];
    }
    [request setHTTPMethod:@"POST"];
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    [connection start];
}

- (void) getRequest:(NSString *) requestFun requestData:(NSDictionary *) sendDataDic{

    NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", @"http://api.domain.com/index.php/api/", requestFun]];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];
    [request setValue:@"application/json; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
    [request setValue:XAPIKEY forHTTPHeaderField:@"X-API-KEY"];

    if(sendDataDic != nil){
        NSString *stringData = [self urlEncodedString:sendDataDic];
        [request setHTTPBody:[stringData dataUsingEncoding:NSUTF8StringEncoding]];
        [request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[stringData length]] forHTTPHeaderField:@"Content-Length"];
    }
    [request setHTTPMethod:@"GET"];
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    [connection start];

}

static NSString *toString(id object) {
    return [NSString stringWithFormat: @"%@", object];
}

static NSString *urlEncode(id object) {
    NSString *string = toString(object);
    return [string stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
}

-(NSString*) urlEncodedString:(NSDictionary*) ObjDic {
    NSMutableArray *parts = [NSMutableArray array];
    for (id key in ObjDic) {
        id value = [ObjDic objectForKey: key];
        NSString *part = [NSString stringWithFormat: @"%@=%@", urlEncode(key), urlEncode(value)];
        [parts addObject: part];
    }
    return [parts componentsJoinedByString: @"&"];
}

// NSURLConnection Delegates
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
    if ([challenge previousFailureCount] == 0) {
        NSLog(@"received authentication challenge");
        NSURLCredential *newCredential = [NSURLCredential credentialWithUser:LOGINUSER
                                                                    password:LOGINPASS
                                                                 persistence:NSURLCredentialPersistenceForSession];
        NSLog(@"credential created");
        [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];
        NSLog(@"responded to authentication challenge");
    } else {
        NSLog(@"previous authentication failure");
    }
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
    NSLog(@"response status code: %ld", (long)[httpResponse statusCode]);
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    NSString *strData = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"%@", strData);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSLog(@"responded to authentication challenge");
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
   NSLog(@"responded to authentication challenge");
}