Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NSURLErrorDomain Code=403 #1228

Closed
Chren opened this issue Jul 27, 2015 · 6 comments
Closed

NSURLErrorDomain Code=403 #1228

Chren opened this issue Jul 27, 2015 · 6 comments

Comments

@Chren
Copy link

Chren commented Jul 27, 2015

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.

@qingmu213
Copy link

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
Copy link
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
Copy link

jiangyancong commented Jul 5, 2016

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

@AndreaVitale
Copy link

Same problem with this sample url: https://static.pexels.com/photos/1475/food-vegetables-italian-restaurant-large.jpg.

Any helps?

@jiangyancong
Copy link

@AndreaVitale Have you tried qingmu213's solution above?

@AndreaVitale
Copy link

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants