我有一个连接使用NSURLConnection,我想为这个连接画一个进度但是当我尝试打印当前接收的数据和总数据长度时出现问题,response.expectedContentLength总是返回-1,为什么以及发生了什么我的联系,我该如何解决呢?
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
receivedDataBytes += [data length];
NSLog(@"data: %f",receivedDataBytes);
[responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
self.HTTPStatusCode = [(NSHTTPURLResponse *)response statusCode];
totalFileSize = response.expectedContentLength;
NSLog(@"total: %lf",totalFileSize);
}
输出:
total: -1.000000
data: 1256.000000
total: -1.000000
data: 1034.000000
total: -1.000000
data: 1127.000000