gpt4 book ai didi

iphone - NSMutableURLRequest 的 addValue 不起作用?

转载 作者:行者123 更新时间:2023-11-29 13:44:25 25 4
gpt4 key购买 nike

这是针对 iOS 的:

我正在尝试从之前中断的连接中恢复文件下载。为此,我只需使用 HTTP header 的范围字段来设置下载请求的起点:

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 服务器是否支持范围字段?

谢谢!

最佳答案

我会独立于您的应用测试服务器。请求的格式似乎是正确的。最简单的判断方法是使用 OSX 附带的 curl:

curl -sv -H 'Range: bytes=100-500' http://www.google.com/intl/en_com/images/srpr/logo3w.png > /dev/null

如果您没有在响应 header 中看到 Content-Range 和相应的 Content-Length header (在本例中为 401),您就会知道服务器有过错。

关于iphone - NSMutableURLRequest 的 addValue 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8002766/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com