Ios NSURLResponse返回内容范围,忽略我请求的范围
下载文件时,我希望能够恢复下载。例如:Ios NSURLResponse返回内容范围,忽略我请求的范围,ios,nsurlrequest,nsurlcache,Ios,Nsurlrequest,Nsurlcache,下载文件时,我希望能够恢复下载。例如: NSFileManager *fm = [NSFileManager defaultManager]; NSString *downloadTempPath = [_tempPath path]; if ([fm fileExistsAtPath:downloadTempPath]) { // We have a partial download. Get the rest of the file starting at byteCount
NSFileManager *fm = [NSFileManager defaultManager];
NSString *downloadTempPath = [_tempPath path];
if ([fm fileExistsAtPath:downloadTempPath]) {
// We have a partial download. Get the rest of the file starting at byteCount
NSDictionary *dAttrib = [fm attributesOfItemAtPath:downloadTempPath error:nil];
NSUInteger byteCount = [dAttrib fileSize];
NSLog(@"Preparing to resume download: %@", downloadTempPath);
NSString *requestRange = [NSString stringWithFormat:@"bytes=%d-", byteCount];
[self.request setValue:requestRange forHTTPHeaderField:@"Range"];
}
这将生成一个get请求,其标题如下:
范围:字节=1000-
我希望第一个206响应会在我要求它开始的地方给我一个块,有这样的头和值(从allHeaderFields
转储):
“内容范围”=“字节1000-340197/340198”
但有时它会跳到前面,并返回如下内容:
“内容范围”=“字节32000-340197/340198”
这真的很糟糕,因为我需要1000-32000之间的字节,但它没有给我
我知道没有服务器错误,因为当我卷曲到服务器时,它会返回我请求的部分。另外,当我使用wireshark时,我没有看到任何数据包进入服务器。因此,这在iOS端是一个问题,可能与
NSURLCache
有关,它似乎是NSURLCache
中的一个bug。当我检测到问题发生时,我会执行以下操作并重试
[[NSURLCache sharedURLCache] removeCachedResponseForRequest:self.request];
一旦我这样做了,问题就不会再发生了。它似乎可以修复缓存。我在iOS5.0上看到了这一点,由于我无法再复制,我不知道它是否发生在较新的操作系统上 我在iOS 8.1.2上遇到了类似的问题。请求不包含任何
Range
或If Range
标题,但响应返回了带有content Range
标题的部分内容。我通过使用NSURLRequestReloadIgnoringLocalCacheData
缓存策略完全删除了缓存的使用,解决了这个问题。