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"];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、基础知识篇:Http Header之User-AgentUser Agent中文名为用户代理,是Http协议中...
    iPhone阅读 16,000评论 0 13
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,887评论 25 709
  • 深入浅出HTTP协议(WEB开发和面试必备) 1.基础概念篇 a.简介 HTTP是Hyper Text Trans...
    半世韶华忆阑珊阅读 1,257评论 0 7
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,292评论 19 139
  • 不让你一个人孤单 是你青涩稚嫩的祈愿 为了那份承诺 多了几重痴痴的依恋 不让你一个人孤单 是你曾经沧海的誓言 你说...
    海天一色y阅读 326评论 0 4