仅在 iPhone 上获取 HTTP 标头字段 [英] Get HTTP header fields only on iPhone
问题描述
我只想获取 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 标头字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!