仅在 iPhone 上获取 HTTP 标头字段

发布时间:2023-02-17 / 作者:清心寡欲
本文介绍了仅在 iPhone 上获取 HTTP 标头字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想获取 URL 请求的标头.到目前为止,我一直在使用 stringWithContentsOfURL() 进行所有下载,但现在我只对标题感兴趣,并且下载整个文件不可行,因为它太大了.

I want to get only the headers of an URL request. I have been using stringWithContentsOfURL() for all downloading so far, but now I am only interested in the headers and downloading the entire file is not feasible as it is too large.

我找到了显示如何在收到响应后读取标头的解决方案,但是如何在请求中指定我只希望下载标头.跳过身体!

I have found solutions which show how to read the headers after the response has been receieved, but how do I specify in the request that I only wish to download headers. Skip the body!

谢谢.

推荐答案

我用过的.下面的代码是同步的,但您可以使用委托使其异步.

What I've used. The code below is synchronous but you can make it asynchronous using a delegate.

    NSMutableURLRequest *newRequest = ... //init your request...
    NSURLResponse *response=nil;
    NSError *error=nil;

    [newRequest setValue:@"HEAD" forKey:@"HTTPMethod"];
    [NSURLConnection sendSynchronousRequest:newRequest returningResponse:&response error:&error];
    [newRequest release];
    //Get MIME type from the response
    NSString* MIMEType = [response MIMEType];

编辑添加了用 NSMutableRequest 替换 NSURLRequest.

Edited to add replace NSURLRequest with NSMutableRequest.

这篇关于仅在 iPhone 上获取 HTTP 标头字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持吉威生活!



[英文标题]Get HTTP header fields only on iPhone


声明:本媒体部分图片、文章来源于网络,版权归原作者所有,如有侵权,请联系QQ:330946442删除。