iOS 快速只获取Http响应头


iOS 快速只获取Http响应头
有些时候需要某个Http的响应头不需要响应文件
如对于一个.mp4的连接怎么快速获取文件大小或者是否经过了302跳转等。

//代码开始时间和结束时间
#define TICK   NSDate *startTime = [NSDate date]
#define TOCK NSLog(@"__func__%s \r\n", __func__);\
NSLog(@"code runing Time duration : %f", -[startTime timeIntervalSinceNow]);\
NSLog(@"\n");

//这样获取的是全部文件 下面给出正确方案
    NSURL *url = [NSURL URLWithString:@"http://static.tripbe.com/videofiles/20121214/9533522808.f4v.mp4"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod = @"GET";
    //[request setHTTPMethod:@"HEAD"];
    NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
    config.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
    NSURLSession *urlSession = [NSURLSession sessionWithConfiguration:config delegate:nil delegateQueue:[NSOperationQueue currentQueue]];
    NSURLSessionDataTask *task = [urlSession dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        TOCK;
        NSHTTPURLResponse *res = (NSHTTPURLResponse *)response;
        NSDictionary *allHeaderFields = res.allHeaderFields;
        TOCK;
        if ([response.URL.absoluteString containsString:urlStr] == NO) {
            NSLog(@"302");
        }
        
        if ([allHeaderFields.allKeys containsObject:@"Content-Length"]) {
            NSNumber *lengthObj = [allHeaderFields objectForKey:@"Content-Length"];
            //fileLength = [lengthObj integerValue];
        }
        
        NSLog(@"allHeaderFields is %@", allHeaderFields);
    }];
    
    [task resume];

//正确的只获取视频文件的响应头
    TICK;
    NSURL *url = [NSURL URLWithString:@"http://static.tripbe.com/videofiles/20121214/9533522808.f4v.mp4"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    //只获取响应头 大概100ms就能返回
    [request setHTTPMethod:@"HEAD"];
    NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
    config.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
    NSURLSession *urlSession = [NSURLSession sessionWithConfiguration:config delegate:nil delegateQueue:[NSOperationQueue currentQueue]];
    NSURLSessionDataTask *task = [urlSession dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        TOCK;
        NSHTTPURLResponse *res = (NSHTTPURLResponse *)response;
        NSDictionary *allHeaderFields = res.allHeaderFields;
        if ([response.URL.absoluteString containsString:urlStr] == NO) {
            NSLog(@"302");
        }
        
        if ([allHeaderFields.allKeys containsObject:@"Content-Length"]) {
            NSNumber *lengthObj = [allHeaderFields objectForKey:@"Content-Length"];
            //fileLength = [lengthObj integerValue];
        }
        
        NSLog(@"allHeaderFields is %@", allHeaderFields);
    }];
    
    [task resume];


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值