1

我有一个使用 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
4

0 回答 0