HTTP If-Modified-Since,以毫秒为单位

时间:2013-09-03 23:40:34

标签: http http-headers

假设我从REST Web服务获取了一个对象,并且该对象具有时间戳。此时间戳具有毫秒组件。下次我请求相同的对象我不希望它被返回,除非它已经改变,所以我使用If-Modified-Since标头。但是该标题中的日期甚至不应该有毫秒。如果我将时间戳向下舍入,我将始终将对象恢复,就像它总是被修改一样。如果我把它弄清楚,我可能会错过一些更新。在这种情况下,If-Modified-Since标题对我来说完全没用,还是我错过了什么?

1 个答案:

答案 0 :(得分:11)

发送时间戳为毫秒的服务不符合HTTP标准。 Last-Modified必须作为 HTTP-date 发送,§3.3.1非常明确地指定:

HTTP-date    = rfc1123-date | rfc850-date | asctime-date
rfc1123-date = wkday "," SP date1 SP time SP "GMT"
rfc850-date  = weekday "," SP date2 SP time SP "GMT"
asctime-date = wkday SP date3 SP time SP 4DIGIT
date1        = 2DIGIT SP month SP 4DIGIT
              ; day month year (e.g., 02 Jun 1982)
date2        = 2DIGIT "-" month "-" 2DIGIT
              ; day-month-year (e.g., 02-Jun-82)
date3        = month SP ( 2DIGIT | ( SP 1DIGIT ))
              ; month day (e.g., Jun  2)
time         = 2DIGIT ":" 2DIGIT ":" 2DIGIT
              ; 00:00:00 - 23:59:59
wkday        = "Mon" | "Tue" | "Wed"
            | "Thu" | "Fri" | "Sat" | "Sun"
weekday      = "Monday" | "Tuesday" | "Wednesday"
            | "Thursday" | "Friday" | "Saturday" | "Sunday"
month        = "Jan" | "Feb" | "Mar" | "Apr"
            | "May" | "Jun" | "Jul" | "Aug"
            | "Sep" | "Oct" | "Nov" | "Dec"

提交您正在使用的服务的错误。发送Last-ModifiedIf-Modified-Since毫秒无效。

如果亚秒级准确度很重要,则使用entity tags (ETag)可能更合适。