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

查看:2400
本文介绍了为什么[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响应头。

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 请求,URL加载系统将fib和告诉你预期的大小是-1,无论什么Content-Length服务器发送。这是因为它在将数据传递给你之前解压缩数据,但是它在下载完所有数据之后才能知道最终的未压缩大小,这在收到回调之后是很好的。

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天全站免登陆