2012-07-03 59 views
0

我想從安全的寧靜服務獲取數據。我跟許多其他職位非常久遠iphone:didReceiveAuthenticationChallenge和canAuthenticateAgainstProtectionSpace不會被要求安全的寧靜服務

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

但在我的情況下didReceiveAuthenticationChallenge和canAuthenticateAgainstProtectionSpace都不會被調用。

請幫助,如果你能解決問題或給我一個很好的例子來調用安全的寧靜服務。

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

回答

0

這可能是比NSURLAuthenticationMethodServerTrust不同,所以只是嘗試這樣開始:

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

你不應該立即取消:

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge 
{ 
    NSLog(@"This is didReceiveAuthenticationChallenge"); 
    [[challenge sender] cancelAuthenticationChallenge:challenge]; 
} 
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"); 
}