这是用于iOS的:NSMutableURLRequest的addValue不起作用?
我正尝试从先前中断的连接恢复文件下载。要做到这一点,我简单地使用HTTP标头的范围字段的下载请求的起点设定为这样:
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString: url]];
// Find out if file already exists.
if([[NSFileManager defaultManager] fileExistsAtPath:filename])
{
NSError *error;
unsigned long long currentSize =[[[NSFileManager defaultManager] attributesOfItemAtPath:filename error:&error] fileSize];
NSLog(@"File exists, size:%llu",currentSize);
// If the file was not completly downloaded -> resume download
[urlRequest addValue:[NSString stringWithFormat:@"bytes=%llu-",currentSize] forHTTPHeaderField:@"Range"];
//[urlRequest setValue:[NSString stringWithFormat:@"%llu-",currentSize] forHTTPHeaderField:@"range"];
}
正如你可以在上面的SRC看,我也用的setValue尝试。在这两种情况下,似乎添加了“范围”字段但不起作用。该文件总是从字节偏移量0发送。当我做一个NSLog(@“%@”,[urlRequest allHTTPHeaderFields]),我得到的范围值被设置,但它似乎仍然不工作:
做一个NSLog(@“%@”,[urlRequest allHTTPHeaderFields])显示:{Range =“bytes = 13207107-”; }或者在注释掉setValue代码的情况下:{Range =“13207107-”;}。
我想也许HTTP服务器不支持范围,但IT人向我保证它...
有谁看到在上面的代码中的任何问题? 有没有简单的方法来测试一个Web服务器是否支持范围字段?
谢谢!