Skip to content

NSURLErrorDomain Code=403 #1228

Closed
Closed
@Chren

Description

@Chren

When using SDWebImage to download an jpg image from web, I got error below:
NSURLErrorDomain Code=403 "The operation couldn’t be completed
this is image url:
http://img5.imgtn.bdimg.com/it/u=3017210771,879699792&fm=11&gp=0.jpg
It shows ok in browser.

Activity

qingmu213

qingmu213 commented on Jul 29, 2015

@qingmu213

because is need User-Agent

in SDWebImageDownloader.m method init replace by

- (id)init {
    if ((self = [super init])) {
        _operationClass = [SDWebImageDownloaderOperation class];
        _shouldDecompressImages = YES;
        _executionOrder = SDWebImageDownloaderFIFOExecutionOrder;
        _downloadQueue = [NSOperationQueue new];
        _downloadQueue.maxConcurrentOperationCount = 6;
        _URLCallbacks = [NSMutableDictionary new];

        NSString *userAgent = @"";
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wgnu"
#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED)
        // User-Agent Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.43
        userAgent = [NSString stringWithFormat:@"%@/%@ (%@; iOS %@; Scale/%0.2f)", [[[NSBundle mainBundle] infoDictionary] objectForKey:(__bridge NSString *)kCFBundleExecutableKey] ?: [[[NSBundle mainBundle] infoDictionary] objectForKey:(__bridge NSString *)kCFBundleIdentifierKey], [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"] ?: [[[NSBundle mainBundle] infoDictionary] objectForKey:(__bridge NSString *)kCFBundleVersionKey], [[UIDevice currentDevice] model], [[UIDevice currentDevice] systemVersion], [[UIScreen mainScreen] scale]];
#elif defined(__MAC_OS_X_VERSION_MIN_REQUIRED)
        userAgent = [NSString stringWithFormat:@"%@/%@ (Mac OS X %@)", [[[NSBundle mainBundle] infoDictionary] objectForKey:(__bridge NSString *)kCFBundleExecutableKey] ?: [[[NSBundle mainBundle] infoDictionary] objectForKey:(__bridge NSString *)kCFBundleIdentifierKey], [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"] ?: [[[NSBundle mainBundle] infoDictionary] objectForKey:(__bridge NSString *)kCFBundleVersionKey], [[NSProcessInfo processInfo] operatingSystemVersionString]];
#endif
#pragma clang diagnostic pop
        if (userAgent) {
            if (![userAgent canBeConvertedToEncoding:NSASCIIStringEncoding]) {
                NSMutableString *mutableUserAgent = [userAgent mutableCopy];
                if (CFStringTransform((__bridge CFMutableStringRef)(mutableUserAgent), NULL, (__bridge CFStringRef)@"Any-Latin; Latin-ASCII; [:^ASCII:] Remove", false)) {
                    userAgent = mutableUserAgent;
                }
            }
            [self setValue:userAgent forHTTPHeaderField:@"User-Agent"];
        }


#ifdef SD_WEBP
        _HTTPHeaders = [@{@"Accept": @"image/webp,image/*;q=0.8",@"User-Agent":userAgent} mutableCopy];
#else
        _HTTPHeaders = [@{@"Accept": @"image/*;q=0.8",@"User-Agent":userAgent} mutableCopy];
#endif
        _barrierQueue = dispatch_queue_create("com.hackemist.SDWebImageDownloaderBarrierQueue", DISPATCH_QUEUE_CONCURRENT);
        _downloadTimeout = 15.0;
    }
    return self;
}
bpoplauschi

bpoplauschi commented on Aug 5, 2015

@bpoplauschi
Member

Indeed, it seems this webserver requires a User-Agent before giving access to the image.
You don't need to modify the SDWebImageDownloader code.

Before downloading the image, do:
[[SDWebImageDownloader sharedDownloader] setValue:userAgent forHTTPHeaderField:@"User-Agent"]; using an userAgent computed like in the example above.

jiangyancong

jiangyancong commented on Jul 5, 2016

@jiangyancong

Excuse me, the solution works very well, but is there any plan to fix it in the branch? Thanks.

AndreaVitale

AndreaVitale commented on Sep 19, 2016

@AndreaVitale
jiangyancong

jiangyancong commented on Sep 20, 2016

@jiangyancong

@AndreaVitale Have you tried qingmu213's solution above?

AndreaVitale

AndreaVitale commented on Sep 20, 2016

@AndreaVitale

Yep, I tried setting the userAgent. It was loaded only for the first, second time. After that the server always return 403.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @bpoplauschi@Chren@AndreaVitale@qingmu213@jiangyancong

        Issue actions

          NSURLErrorDomain Code=403 · Issue #1228 · SDWebImage/SDWebImage