qiniu / happy-dns-objc

dns library for objective c
MIT License
505 stars 83 forks source link

分享happy-dns 配合 SDWebImage 的使用 #3

Closed iTA9178 closed 8 years ago

iTA9178 commented 9 years ago

1.先pod 'HappyDNS', '~> 0.1.1'到项目中;

2.项目中使用QNDnsManager 返回ips; NSMutableArray array = [[NSMutableArray alloc]init]; [array addObject:[QNResolver systemResolver]]; [array addObject:[[QNResolver alloc] initWithAddres:@"114.114.115.115"]]; QNDnsManager dnsManager = [[QNDnsManager alloc] init:array networkInfo:[QNNetworkInfo normal]]; NSArray *ips = [dnsManager query:@"http://xxxx.com2.z0.glb.qiniucdn.com"];

3.请求七牛图片时使用ip替换域名http://xxxx.com2.z0.glb.qiniucdn.com,并设置HTTPHeadField; [SDWebImageManager.sharedManager.imageDownloader setValue:@"http://xxxx.com2.z0.glb.qiniucdn.com" forHTTPHeaderField:@"Host"]; 注:自己封装请求图片时使用[request setValue: @"http://xxxx.com2.z0.glb.qiniucdn.com" forHTTPHeaderField:@"Host"];

4.项目中因为图片还有其他来源,这里区分使用设置HTTPHeadField中Host; SDWebImageManager.sharedManager.imageDownloader.headersFilter = ^NSDictionary (NSURL url, NSDictionary headers) { NSMutableDictionary mutableHeaders = [headers mutableCopy]; [mutableHeaders removeObjectForKey:@"Host"]; //是否为ip格式 if ([url.host rh_isIp]) { [mutableHeaders setValue:@"xxxx.com2.z0.glb.qiniucdn.com" forKey:@"Host"]; } return mutableHeaders; };

最后,谢谢longbai在此过程中提供的帮助,由于缺少相关文档,冒昧抛砖引玉,希望可以帮助到小伙伴们,欢迎一起交流!

longbai commented 9 years ago

@iTA9178 十分感谢啊,这个地方 还有可以优化的地方 就是使用NSURLProtocol 这样代码会更干净些,另外可以对这些IP 进行挨个重试,假如一个失败 就切换到下一个

longbai commented 8 years ago

@neil-wu 需要设置 Host header

longbai commented 8 years ago

... use domain to set