【问题标题】:iphone: didReceiveAuthenticationChallenge and canAuthenticateAgainstProtectionSpace are not called for secure restful serviceiphone:没有调用 didReceiveAuthenticationChallenge 和 canAuthenticateAgainstProtectionSpace 以获得安全的休息服务
【发布时间】:2012-07-03 14:26:27
【问题描述】:

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

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

但在我的情况下 didReceiveAuthenticationChallenge 和 canAuthenticateAgainstProtectionSpace 没有被调用。

如果您能解决问题,请提供帮助,或者提供一些很好的例子来调用安全的 restful 服务。

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

【问题讨论】:

    标签: iphone objective-c ios ipad ssl


    【解决方案1】:

    它可能与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];
    }  
    

    【讨论】:

      【解决方案2】:
      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");
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-08
        • 2011-12-16
        • 1970-01-01
        • 1970-01-01
        • 2019-03-12
        • 2013-03-26
        • 1970-01-01
        • 2014-05-26
        相关资源
        最近更新 更多