首先从维基百科上对于返回码是406的定义:
406 Not Acceptable - The requested resource is only capable of generating content not acceptable according to the Accept headers sent in the request.
就是说我们的请求不被服务器所接受,原因是我们的请求头出现了问题。
那么我们碰到的问题是
Error Domain=NSURLErrorDomain Code=406 “The operation couldn’t be completed. (NSURLErrorDomain error 406.)”
返回码是406,因此可以发现是我们的请求头的问题
然后我们进到SDWebImageDownloader文件中,我们发现了设置请求头的代码:
#ifdef SD_WEBP
_HTTPHeaders = [@{@"Accept": @"image/webp,image/*;q=0.8"} mutableCopy];
#else
_HTTPHeaders = [@{@"Accept": @"image/*;q=0.8"} mutableCopy];
#endif
这里的请求头有可能不满足我们的服务器的条件,因此,我们在这里修改请求头,改为
_HTTPHeaders = [NSMutableDictionary dictionaryWithObject:@"image/webp,image/*;q=0.8" forKey:@"Accept"];
发现图片请求成功了,但是这不是长久之计,又因为SDWebImageDownloader是单例,所以我们可以从外部设置他的请求头,代码如下:
[SDWebImageDownloader.sharedDownloader setValue:@"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" forHTTPHeaderField:@"Accept"];
成功解决问题。