为啥我在请求头里面设置了Range,但是响应还是把
- (void)start {
NSURL *url = [NSURL URLWithString:self.sourceUrl];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url];
//实现暂停功能就是把每次下载的数据量纪录下来,然后通过这些来设置下次下载的请求头
NSString *value = [NSString stringWithFormat:@"bytes=%ld-%ld", self.startLocation+ self.currentLength, self.endLocation + 100];
[request setValue:value forHTTPHeaderField:@"Range"];
NSLog(@"请求头:%@",request.allHTTPHeaderFields);
self.connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
}- (void)pause {
[self.connection cancel];
self.connection = nil;
}- (void)end {
[self pause];
}- (NSFileHandle *)fileHandle {
if (!_fileHandle) {
//调用这个方法,参数路径必须是实际存在的,否则返回一个空文件句柄
_fileHandle = [NSFileHandle fileHandleForWritingAtPath:self.storePath];
}
return _fileHandle;
}#pragma - NSURLConnection常用的代理方法
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(@"连接服务器成功");
NSLog(@"-----respose:%@",response);//这里可以看到服务器给你的所有反馈,挺有用
//每次连接到服务器一次(下载一次),就重新声明一个data来存储数据
self.tempData = [[NSMutableData alloc]init];
}- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSLog(@"接收到数据");//调用多次,每次接受部分数据
[self.tempData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"这次下载所有的数据已经下载完毕");
[self.fileHandle writeData:self.tempData];
[self.fileHandle closeFile];
self.fileHandle = nil;
}- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"%s",__FUNCTION__);
}打印出来是这样的:
2016-03-09 14:15:19.968 Downloader[40691:2935301] 请求头:{
Range = "bytes=0-100";
}
2016-03-09 14:15:20.193 Downloader[40691:2935301] 连接服务器成功
2016-03-09 14:15:20.193 Downloader[40691:2935301] -----respose:<NSHTTPURLResponse: 0x7f96a1e3ee80> { URL: https://dn-nickey.qbox.me/124668.jpg } { status code: 200, headers {
"Accept-Ranges" = bytes;
"Access-Control-Allow-Origin" = "*";
"Access-Control-Expose-Headers" = "X-Log, X-Reqid";
"Access-Control-Max-Age" = 2592000;
"Cache-Control" = "public, max-age=31536000";
Connection = "keep-alive";
"Content-Disposition" = "inline; filename=\"124668.jpg\"";
"Content-Length" = 349812;
"Content-Transfer-Encoding" = binary;
"Content-Type" = "image/jpeg";
Date = "Wed, 09 Mar 2016 03:16:00 GMT";
Etag = "\"FrgKUK4hw_c9DDc6tq2bDghLrE7F\"";
"Last-Modified" = "Fri, 30 Oct 2015 03:36:19 GMT";
Server = "nginx/1.4.4";
"X-Log" = "mc.g;IO:3";
"X-Qiniu-Zone" = 0;
"X-Reqid" = xngAAHWdkj99DjoU;
"X-Via" = "1.1 wf96:88 (Cdn Cache Server V2.0), 1.1 chongqingwangtong13:5 (Cdn Cache Server V2.0)";
} }
2016-03-09 14:15:20.193 Downloader[40691:2935301] 接收到数据
2016-03-09 14:15:20.194 Downloader[40691:2935301] 这次下载所有的数据已经下载完毕
2016-03-09 14:15:53.999 Downloader[40691:2935301] 我这里获取到的data:<UIImage: 0x7f96a1d77290>, {1440, 1280}
- (void)start {
NSURL *url = [NSURL URLWithString:self.sourceUrl];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url];
//实现暂停功能就是把每次下载的数据量纪录下来,然后通过这些来设置下次下载的请求头
NSString *value = [NSString stringWithFormat:@"bytes=%ld-%ld", self.startLocation+ self.currentLength, self.endLocation + 100];
[request setValue:value forHTTPHeaderField:@"Range"];
NSLog(@"请求头:%@",request.allHTTPHeaderFields);
self.connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
}- (void)pause {
[self.connection cancel];
self.connection = nil;
}- (void)end {
[self pause];
}- (NSFileHandle *)fileHandle {
if (!_fileHandle) {
//调用这个方法,参数路径必须是实际存在的,否则返回一个空文件句柄
_fileHandle = [NSFileHandle fileHandleForWritingAtPath:self.storePath];
}
return _fileHandle;
}#pragma - NSURLConnection常用的代理方法
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(@"连接服务器成功");
NSLog(@"-----respose:%@",response);//这里可以看到服务器给你的所有反馈,挺有用
//每次连接到服务器一次(下载一次),就重新声明一个data来存储数据
self.tempData = [[NSMutableData alloc]init];
}- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSLog(@"接收到数据");//调用多次,每次接受部分数据
[self.tempData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"这次下载所有的数据已经下载完毕");
[self.fileHandle writeData:self.tempData];
[self.fileHandle closeFile];
self.fileHandle = nil;
}- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"%s",__FUNCTION__);
}打印出来是这样的:
2016-03-09 14:15:19.968 Downloader[40691:2935301] 请求头:{
Range = "bytes=0-100";
}
2016-03-09 14:15:20.193 Downloader[40691:2935301] 连接服务器成功
2016-03-09 14:15:20.193 Downloader[40691:2935301] -----respose:<NSHTTPURLResponse: 0x7f96a1e3ee80> { URL: https://dn-nickey.qbox.me/124668.jpg } { status code: 200, headers {
"Accept-Ranges" = bytes;
"Access-Control-Allow-Origin" = "*";
"Access-Control-Expose-Headers" = "X-Log, X-Reqid";
"Access-Control-Max-Age" = 2592000;
"Cache-Control" = "public, max-age=31536000";
Connection = "keep-alive";
"Content-Disposition" = "inline; filename=\"124668.jpg\"";
"Content-Length" = 349812;
"Content-Transfer-Encoding" = binary;
"Content-Type" = "image/jpeg";
Date = "Wed, 09 Mar 2016 03:16:00 GMT";
Etag = "\"FrgKUK4hw_c9DDc6tq2bDghLrE7F\"";
"Last-Modified" = "Fri, 30 Oct 2015 03:36:19 GMT";
Server = "nginx/1.4.4";
"X-Log" = "mc.g;IO:3";
"X-Qiniu-Zone" = 0;
"X-Reqid" = xngAAHWdkj99DjoU;
"X-Via" = "1.1 wf96:88 (Cdn Cache Server V2.0), 1.1 chongqingwangtong13:5 (Cdn Cache Server V2.0)";
} }
2016-03-09 14:15:20.193 Downloader[40691:2935301] 接收到数据
2016-03-09 14:15:20.194 Downloader[40691:2935301] 这次下载所有的数据已经下载完毕
2016-03-09 14:15:53.999 Downloader[40691:2935301] 我这里获取到的data:<UIImage: 0x7f96a1d77290>, {1440, 1280}
解决方案 »
- iOS中如何用UIAlertView来确认提交动作?
- 请问如何在地图(MapKit google map)上标记出一条路
- Mac OS X Lion 10.7.2系统语言无法显示中文
- 使用UIPagecontroll 如何让他置于顶层
- tabbar问题!
- ipod2 应用软件开发用什么工具?
- print print_r echo 区别
- 各路ios精英,本人小菜鸟一枚 想寻求好的学习方法
- linker command failed with exit code 1 (use -v to see invocation)求解答,快哭了
- ios中 类似微信 消息通知栏下拉免打扰是怎么做到的,是哪个api
- 关于MKMapView中setRegion对于delegate调用的影响
- 求大神解决,已经试了很多方式了,就是不行;卡死了。急死本宝宝了。谢谢各位大神!
Range = "bytes=0-100";
}
2016-03-12 14:02:53.294 DownloadRange[15134:5949962] 连接服务器成功
2016-03-12 14:02:53.295 DownloadRange[15134:5949962] -----respose:<NSHTTPURLResponse: 0x7fafbac027e0> { URL: https://dn-nickey.qbox.me/124668.jpg } { status code: 206, headers {
"Accept-Ranges" = bytes;
"Access-Control-Allow-Origin" = "*";
"Access-Control-Expose-Headers" = "X-Log, X-Reqid";
"Access-Control-Max-Age" = 2592000;
"Cache-Control" = "public, max-age=31536000";
Connection = "keep-alive";
"Content-Disposition" = "inline; filename=\"124668.jpg\"";
"Content-Length" = 101;
"Content-Range" = "bytes 0-100/349812";
"Content-Transfer-Encoding" = binary;
"Content-Type" = "image/jpeg";
Date = "Sat, 12 Mar 2016 06:02:53 GMT";
Etag = "\"FrgKUK4hw_c9DDc6tq2bDghLrE7F\"";
"Last-Modified" = "Fri, 30 Oct 2015 03:36:19 GMT";
Server = "nginx/1.4.4";
"X-Log" = "mc.g;IO:2";
"X-Qiniu-Zone" = 0;
"X-Reqid" = jDAAAGJpXf6BAjsU;
"X-Via" = "1.1 jx129:88 (Cdn Cache Server V2.0), 1.1 xfzh13:1 (Cdn Cache Server V2.0)";
} }
2016-03-12 14:02:53.295 DownloadRange[15134:5949962] 接收到数据
2016-03-12 14:02:53.296 DownloadRange[15134:5949962] 这次下载所有的数据已经下载完毕
如果你以后遇到类似的问题可以把start方法里面request的定义换成这样NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.sourceUrl] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:100];每次把不计缓存就好了。