首页 > 解决方案 > how to handle "too many http redirects" error in objective c

问题描述

i'm currently working on an itunes search app. i'm following the iTunes API documentation (https://affiliate.itunes.apple.com/resources/documentation/itunes-store-web-service-search-api/) and AFNetworking sample here (https://github.com/AFNetworking/AFNetworking). But i'm getting an error of "too many http redirects". Here's my code:

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

NSURL *URL = [NSURL URLWithString:@"https://itunes.apple.com/search?term=jack+johnson&limit=25"];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];

NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request uploadProgress:nil downloadProgress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
        if (error) {
            NSLog(@"Error: %@", error);
        } else {
            NSLog(@"%@ %@", response, responseObject);
        }
    }];
    [dataTask resume];

I've also tried:

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

NSURL *URL = [NSURL URLWithString:@"https://itunes.apple.com/search?term=jack+johnson&limit=25"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
        NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
        return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
    } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
        if (error) {
            NSLog(@"Error: %@", error.localizedDescription);
        } else {
            NSLog(@"File downloaded to: %@", filePath);
            NSDictionary *result;
            NSData *jSONData = [NSData dataWithContentsOfURL:filePath];  // will block if url is not local!

            if (jSONData) {
                result = [NSJSONSerialization JSONObjectWithData:jSONData options:0 error:NULL];
                NSLog(@"%@", result);
            }
        }
    }];
    [downloadTask resume];

Thanks in advance for answering. ps. im new here :3

标签: objective-cafnetworking

解决方案


推荐阅读