仅在iPhone上获取HTTP标头字段 [英] Get HTTP header fields only on iPhone
问题描述
我想只获取网址请求的标头。到目前为止,我一直在使用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屋!