1.
不获取响应体,只获取响应头
a.
一般在下载之前先获取文件的大小
b.
NSURLResponse 的属性
2.
a.
MIMEType
返回的文件的类型Content-Type
b.
expectedContentLength
文件的预期大小(实际大小)
c.
suggestedFilename
建议保存的文件的名字
代码示例:异步请求
3.
输出的data是空的
-(void)getHead:(NSString*)urlStr{
NSURL*url = [NSURLURLWithString:urlStr];
NSMutableURLRequest*request = [NSMutableURLRequestrequestWithURL:url cachePolicy:1timeoutInterval:10];
//设置请求方法
request.HTTPMethod= @"head";
[NSURLConnectionsendAsynchronousRequest:request queue:[NSOperationQueuemainQueue] completionHandler:^(NSURLResponse*response, NSData*data, NSError*connectionError) {
NSLog(@"%@",response);
NSLog(@"%@",data);
}];
}
4.
代码示例:同步请求
NSURLResponse*response = nil;
NSData*data = [NSURLConnectionsendSynchronousRequest:request returningResponse:&response error:NULL];
NSHTTPURLResponse*httpResponse = (NSHTTPURLResponse*)response;
NSLog(@"%@",response.suggestedFilename);
NSLog(@"%zd",httpResponse.statusCode);
NSLog(@"%@",data);