一个简短的问题。我有以下代码获取服务器上文件的修改日期:
- (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 );
}
我的主要问题如下:
此调用是否仅通过 header 发送?如果文件很大,我不想下载整个文件。这就是我检查标题的原因。
++++++++++++++++++++++++++++++++++++++++ 更新后这个工作... 感谢现在看起来像:
NSMutableURLRequest *modReq = [NSMutableURLRequest requestWithURL:[NSURL URLWithString: URLInput.text]
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0f];
[modReq setHTTPMethod:@"HEAD"];
最佳答案
正如您现在所拥有的,您可能正在下载整个文件。关键是用于http请求的http方法。默认情况下,这是一个 GET 请求。你想要的是一个 HEAD 请求。您不想要该文件,您只希望服务器返回如果您这样做会得到的响应,对吗?
要做到这一点,您需要使用 NSMutableURLRequest
和 setHTTPMethod:
构造一个带有 HEAD 方法的请求,而不是 GET。
关于objective-c - iOS - 获取标题结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7251448/