开发者

Can I get the date when an HTTP file was modified?

开发者 https://www.devze.com 2023-03-17 09:31 出处:网络
I\'m trying to check if a file (on web) was modified since the last time I checked. Is it possible to do this by getting http headers to read the last time the file was modified (or uploade开发者_Stac

I'm trying to check if a file (on web) was modified since the last time I checked. Is it possible to do this by getting http headers to read the last time the file was modified (or uploade开发者_StackOverflow社区d)?


You can use the built-in Net::HTTP library to do most of this for you:

require 'net/http'

Net::HTTP.start('stackoverflow.com') do |http|
  response = http.request_head('/robots.txt')

  response['Last-Modified']
  # => Sat, 04 Jun 2011 08:51:44 GMT
end

If you want, you can convert that to a proper date using Time.parse.


As @tadman says in his answer, a HTTP "HEAD" request is the proper way to check the last modification date.

You can also do it using a conditional GET request using the "IF-*" modifier headers.

Which to use depends on whether you intend to immediately download the page. If you just want the date use HEAD. If you want the content if there has been a change use GET with the "IF-*" headers.

0

精彩评论

暂无评论...
验证码 换一张
取 消