-(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.
也许有人知道为什么?我尝试使用很久,但效果是一样的.
感谢帮助.
解决方法
期望的内容长度仅在服务器提供时设置,例如通过Content-Length响应头. -1尺寸表示预期的内容大小未知.
如果您在请求中设置了Accept-Encoding:gzip,那么URL加载系统将会纤维化并告诉您,无论服务器发送什么Content-Length,预期大小为-1.这是因为它在将数据传递给您之前解压缩数据,但是无法知道最终的未压缩大小,直到所有数据都被下载,这在您收到此回调后很好.