【发布时间】:2016-12-02 06:46:59
【问题描述】:
我已经尝试过了
How to get NSURLSession to return Content-Length(http header) from server. Objective-c, ios
- (long long) getContentLength
{
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
request.HTTPMethod = @"HEAD";
[request addValue:@"identity" forHTTPHeaderField:@"Accept-Encoding"];
NSURLSessionDownloadTask *uploadTask
= [session downloadTaskWithRequest:request
completionHandler:^(NSURL *url,NSURLResponse *response,NSError *error) {
NSLog(@"handler size: %lld", response.expectedContentLength);
totalContentFileLength = [[NSNumber alloc] initWithFloat:response.expectedContentLength].longLongValue;
}];
NSLog(@"content length=%lld", totalContentFileLength);
[uploadTask resume];
return totalContentFileLength;
}
我总是从返回值中得到0。
【问题讨论】:
标签: ios objective-c nsurlsession nsurlsessiondownloadtask