为什么 [response expectedContentLength] 总是返回 -1 [英] why [response expectedContentLength] always return -1

查看:24
本文介绍了为什么 [response expectedContentLength] 总是返回 -1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

    download_size =[response expectedContentLength];
}

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

I have this code. download_size is NSInteger. expectedContentLenght always return: -1. Maybe someone know why? I tried use long, but effect was the same.

感谢您的帮助.

推荐答案

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

The expected content length is only set when the server provides it, such as by a Content-Length response header. A -1 size means the expected content size is unknown.

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

If you set Accept-Encoding: gzip on your request, the URL loading system will fib and tell you the expected size is -1, no matter what Content-Length the server sends. This is because it decompresses the data before passing it to you, but it can't know the final uncompressed size till all the data has been downloaded, which is well after you receive this callback.

这篇关于为什么 [response expectedContentLength] 总是返回 -1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆