0

我正在尝试从安全的宁静服务中获取数据。我关注了许多其他帖子

如何使用 NSURLConnection 与 SSL 连接以获得不受信任的证书?

但在我的情况下 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];
}  
4

2 回答 2

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];
}  
于 2012-07-03T14:56:59.987 回答
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");
}
于 2018-02-02T13:06:39.650 回答