NSURLConnection
与请求一起使用Head
,响应将具有expectedContentLength:
要求
NSMutableURLRequest * req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://cdn.sstatic.net/stackoverflow/img/apple-touch-icon.png"]];
[req setHTTPMethod:@"Head"];
NSURLConnection * con = [[NSURLConnection alloc] initWithRequest:req
delegate:self];
[con start];
[con release];
代表
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"didReceiveResponse (%lld)", response.expectedContentLength);
}
请注意,它可能是负面的:
#define NSURLResponseUnknownLength ((long long)-1)
对于NSURLSession
,使用接受NSURLRequest
对象作为参数的任务工厂方法,例如- (NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request completionHandler:(void (^)(NSData *data, NSURLResponse *response, NSError *error))completionHandler: