iOS – 获取标题结果

一个简单的问题。 我有以下代码获取服务器上的文件的moddate:

- (void) sendRequestForLastModifiedHeaders { /* send a request for file modification date */ NSURLRequest *modReq = [NSURLRequest requestWithURL:[NSURL URLWithString:URLInput.text] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0f]; [[NSURLConnection alloc] initWithRequest:modReq delegate:self]; } - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { /* convert response into an NSHTTPURLResponse, call the allHeaderFields property, then get the Last-Modified key. */ NSHTTPURLResponse *foo = [(NSHTTPURLResponse *)response allHeaderFields]; NSString * last_modified = [NSString stringWithFormat:@"%@", [[(NSHTTPURLResponse *)response allHeaderFields] objectForKey:@"Last-Modified"]]; NSLog(@"Last-Modified: %@", last_modified ); } 

我的主要问题是以下几点:

这个调用是否只发送头部? 如果文件很大,我不希望整个文件被下载。 这就是为什么我要检查标题。

++++++++++++++++++++++++++++++++++++++++更新后这个作品…现在看起来感谢喜欢:

 NSMutableURLRequest *modReq = [NSMutableURLRequest requestWithURL:[NSURL URLWithString: URLInput.text] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0f]; [modReq setHTTPMethod:@"HEAD"]; 

现在,你可能正在下载整个文件。 关键是用于http请求的http方法。 默认情况下,它是一个GET请求。 你想要的是一个HEAD请求。 你不需要这个文件,你只需要服务器返回你会得到的回应,对不对?

要做到这一点,你想要使用一个NSMutableURLRequestsetHTTPMethod:构造一个方法为HEAD的请求,而不是GET。