iOS Development

ios – NSURLSessionDownloadTask leads to 404 standing when server points a redirect

Spread the love

I’m making an attempt to obtain a picture in Goal C from a CDN that serves pictures. This works tremendous if the picture URL is similar because the URL that was requested. However some pictures will lead to a redirect to a special URL (even a special area). For instance, this is perhaps the request URL however then the CDN will redirect to the second URL.

If viewing this in a browser or different context, it really works tremendous (the URL will get up to date, however the picture is proven appropriately). However when making an attempt to obtain it utilizing an NSURLSessionDownloadTask, it leads to a 404 standing code.

Right here is the code I’m utilizing to carry out the obtain. The code does work if the picture shouldn’t be redirected (the URL of the picture that’s downloaded is similar because the picture URL that was requested). However in any other case, if the server does carry out any redirection, this leads to a 404 standing.

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration ephemeralSessionConfiguration];
configuration.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
configuration.URLCache = nil;
configuration.timeoutIntervalForRequest = 5 * 60;
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:nil delegateQueue:nil];

NSURLRequest *request = [NSURLRequest requestWithURL:key.url];
NSURLSessionDownloadTask *activity = [session downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
    NSInteger statusCode = 0;
    NSHTTPURLResponse *httpResponse = nil;
    if ([response isKindOfClass:NSHTTPURLResponse.class]) {
        httpResponse = (NSHTTPURLResponse *)response;
        statusCode = httpResponse.statusCode;
    } else {
        statusCode = error.code;

    // the standing code is 404 when the server points a redirect

[task resume];

Does anybody know what I might do to get this to work? If I request a picture from URL1 however it’s served again at URL2, then I’d simply love to do the obtain anyway. It does not matter if the server redirects to a special URL or not, I’d nonetheless prefer to get the picture downloaded.

Leave a Reply

Your email address will not be published. Required fields are marked *