简体   繁体   中英

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

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 uploaded)?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM