通过 HTTP 获取文件创建日期
给定网络服务器上的文件(例如 http://foo.com/bar.zip ->只能通过 HTTP 访问),有什么方法可以获取日期属性(例如,日期[创建、修改]),而无需首先下载整个存档?
现在,我下载存档并以编程方式读取属性。问题是存档有几十个 MiB,因此下载整个内容并最终读取几个字节的信息似乎是浪费资源。
我意识到带宽实际上是免费的,但无论如何我都不喜欢浪费。
Given a file on a webserver (e.g., http://foo.com/bar.zip -> only accessible through HTTP), is there any way to get the date attributes (e.g., date [created, modified]) without downloading the entire archive in the first place?
Right now, I download the archive and read the attributes programmatically. Trouble is that the archive is dozens of MiB so it seems like a waste of resources to download the entire thing and end up reading off just a couple of bytes of information.
I realize that bandwidth is practically free, but I don't like to be wasteful in any case.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试从标题中读取 Last-Modified
Try to read Last-Modified from header
请务必使用 HTTP HEAD 请求而不是 HTTP GET 请求来仅读取 HTTP 标头。如果您执行 HTTP GET,即使您决定只检查 HTTP 标头,您也将下载整个文件。
Be sure to use a HTTP HEAD request instead of a HTTP GET request to read the HTTP headers only. If you do a HTTP GET, you will download the whole file nevertheless, even if you decide just to inspect the HTTP headers.
为了简单起见,这里是来自 @ihorko 和 @JanThomä 的现有(完美)答案的汇编,它使用了curl。当然,也可以使用其他选项,但这里有一个功能齐全的答案。
将curl 与
-I
选项一起使用:另外,
-s
选项在这里也很好:因此,像这样的事情就可以解决问题:
Just for the sake of simplicity, here's a compilation of the existing (perfect) answers from @ihorko and @JanThomä, that uses curl. Other option are available too, of course, but here's a fully functional answer.
Use curl with the
-I
option:Also, the
-s
option is nice here:Hence, something like this would do the trick: