SDWebImage 报错 403 406 处理

403

因为一些原因 SDWebImage 没设置 User-Agent 所以自己设置一下

//设置 User-Agent 
NSString *userAgent = @"";
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]];
    
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;
            }
        }
        [[SDWebImageDownloader sharedDownloader] setValue:userAgent forHTTPHeaderField:@"User-Agent"];
    }


//调用示例
[img sd_setImageWithURL:[NSURL URLWithString:@"http://img3.imgtn.bdimg.com/it/u=17435921,1676944870&fm=21&gp=0.jpg"] 
placeholderImage:[UIImage imageNamed:@"1"] 
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
       [img drawCircleImage1];
}];

出处
406

//设置头文件
[[SDWebImageDownloader sharedDownloader] 
setValue:@"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" 
forHTTPHeaderField:@"Accept"];

你可能感兴趣的:(SDWebImage 报错 403 406 处理)