为什么[response expectedContentLength]总是返回-1

kol*_*lek 9 iphone connection nsurlconnection ipad ios

-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse    *)response {
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    if([recievedData length]) [ recievedData setLength:0 ];

    download_size =[response expectedContentLength];
}
Run Code Online (Sandbox Code Playgroud)

我有这个代码.download_size是NSInteger.expectedContentLenght总是返回:-1.也许有人知道为什么?我尝试使用很长时间,但效果是一样的.

感谢帮助.

Jer*_*man 22

预期的内容长度仅在服务器提供时设置,例如通过Content-Length响应头.-1大小表示预期的内容大小未知.

如果您设置Accept-Encoding: gzip了您的请求,则无论服务器发送什么Content-Length,URL加载系统都将显示并告诉您预期大小为-1.这是因为它在将数据传递给您之前对其进行了解压缩,但是在所有数据都已下载之前它无法知道最终的未压缩大小,这在您收到此回调之后就已经存在了.