第一次请求
请求:
GET /pic/201408/102.jpg HTTP/1.1
Host: www.jb51.net
Connection: keep-alive
Cache-Control: no-cache
Accept: image/webp,*/*;q=0.8
Pragma: no-cache
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36
Referer: https://www.jb51.net/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: zh-CN,zh;q=0.8
响应:
HTTP/1.1 200 OK
Content-Length: 66529
Content-Type: image/jpeg
Last-Modified: Tue, 19 Aug 2014 12:23:54 GMT
Accept-Ranges: bytes
ETag: "029e570a8bbcf1:1ae2"
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
Date: Wed, 20 Aug 2014 00:29:03 GMT
xxx
第二次请求
请求:
GET /pic/201408/102.jpg HTTP/1.1
Host: www.jb51.net
Connection: keep-alive
Cache-Control: max-age=0
Accept: image/webp,*/*;q=0.8
Pragma: no-cache
If-Modified-Since: Tue, 19 Aug 2014 12:23:54 GMT
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36
If-None-Match: "029e570a8bbcf1:1ae2"
Referer: https://www.jb51.net/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: zh-CN,zh;q=0.8
响应:
HTTP/1.1 304 Not Modified
Last-Modified: Tue, 19 Aug 2014 12:23:54 GMT
Accept-Ranges: bytes
ETag: "029e570a8bbcf1:1ae2"
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
Date: Wed, 20 Aug 2014 00:29:54 GMT
HTTP缓存是为了节省网络传输数据量,服务器端的资源没有变化时返回304且内容为空。Last-Modified和ETag都是用于HTTP缓存,浏览器在第二次请求时会将其传回给服务器用于判断资源是否有变化。ETag是HTTP/1.1标准开始引入的,它是对Last-Modified的补充,主要原因有以下几点:
?一些周期性修改的文件,修改时间变了但内容没变,此时不希望重新GET;
?一些文件修改非常频繁,比如1秒内修改了多次,Last-Modified只能精确到秒;
?一些服务器不能得到文件修改的精确时间;
说明:HTTP/1.1标准没有规定Etag的内容是什么或者说要怎么实现,唯一规定的是Etag需要放在引号内。