For a static html page, the browser puts the page into the cache after the first visit.
When you access this page again, the request header will be attached:
If-Modified-Since: Mon, 17 Sep 2012 08:52:31 GMT data.
Sent to the server. After receiving the message, the server will send a 304 response to the client if it finds that the date has not changed. The client displays its own cache.
If you press CTRL + F5, the request is forcibly refreshed and the request header does not contain If-Modified-Since. Therefore, the server sends a 200 response directly after receiving the request.
The cache used by ASP. NET also has the same effect. For example, the following page code:
<% @ OutputCache Duration = "600" VaryByParam = "None" %>
Default: <% Response. Write (DateTime. Now. ToString ("yyyyMMdd"); %>
If Location = "ServerAndClient" is set here, the second access is a 304 response. The access to this page is the same as the Access to html, and the second access will have a 304 response. Of course, after the page is updated, the system will receive a 200 response. That's because the request header contains data such as If-Modified-Since: Tue, 18 Sep 2012 12:32:14 GMT. Note: There will be a 304 response in Chrome's attempt. However, in firefox, the request header does not have the If-Modified-Since information, so there is no 304, and each request is a 200 response. The reason is unknown .)
If Location = "Server" is set here, the 200 response is returned each time. This is cached in the Server memory, so although every time it is 200, it saves the page generation overhead.
When Location = "Server", the response header is changed to Cache-Control: no-cache. In fact, even if the response header Cache-Control is marked as no-cache, there will still be Cache locally, the cache content will not be sent to the client if it is not verified for the first time, the better name of no-cache is "no validation is not used as The output cache" NOTE: from HTTP The Definitive Guide by David Gourley and Brian Totty)
If Location = "Client" is set here, the 200 response is returned each time. This is the question. Although the client has a cache and the request header also has If-Modified-Since, the server data has not changed, but 200 is returned. We speculate that for dynamic pages, if there is no special processing, the status is always 200.
For example, if the following page is a php page or aspx page, even if the page content does not have any dynamic code.
For example:
- <Html xmlns = "http://www.w3.org/1999/xhtml">
- <Head>
- <Title> </title>
- </Head>
- <Body>
- <Form id = "form1">
- <Div>
- Do not modify
- </Div>
- </Form>
- </Body>
- </Html>
-
Each request always gets a 200 response and never uses the cache.
The following shows how the server uses the 304 status to trick the client into calling its own cache to relieve network pressure.
First, describe the following tested rules:
Response Header last-modified. After it is cached to the client, that is, when the client refreshes, this value will become the value of the request header of the client.
For example, Response. addHeader ("last-modified", new DateTime (, 1), then when you refresh or resubmit this page, Request. the Headers ["If-Modified-Since"] value is 2001-01-01. Therefore, If-Modified-Since can be understood as the last update time of the cache.
Assume that the following function is implemented. If the client requests a page, the client sends a 304 status code again within 10 seconds, requesting the client to read the cache. If the client CTRL + F5 cannot read the cache)
The ASPX code is implemented as follows:
- <% @ Page Title = "Homepage" Language = "C #" AutoEventWireup = "true" CodeBehind = "Default. aspx. cs"
- Inherits = "WebApplication1. _ Default" %>
- <%
- Response. write ("If-Modified-Since:" + Request. headers ["If-Modified-Since"] + "<br/>"); // print the value of If-Modified-Since in the Read Request Header
- Response. Write ("Now:" + DateTime. Now. ToString () + "<br/>"); // print the current time
- DateTime If_Modified_Since = new DateTime ();
- If (Request. Headers ["If-Modified-Since"] = null | Request. Headers ["If-Modified-Since"] = "")
- {
- If_Modified_Since = new DateTime (1900, 1, 1); // If the read request header contains no value If-Modified-Since, the default value is 19000101.
- }
- Else
- {
- If_Modified_Since = DateTime. Parse (Request. Headers ["If-Modified-Since"]);
- }
- If (If_Modified_Since.AddSeconds (10)> DateTime. Now) // if the last modification time of the cache is 10 seconds different from the current time
- {
- Response. StatusCode = 304; // return code 304 to read the cache.
- Response. End ();
- }
- Else // otherwise
- {
- // Display content
- Response. AddHeader ("last-modified", DateTime. Now. ToString ());
- Response. Write ("Content Changed;" + DateTime. Now. ToString () + "<br> ");
- }
- %>
The PHP code is as follows:
- <? Php
- Date_default_timezone_set ('prc'); // time of the People's Republic of China
- $ Arr = getallheaders ();
- $ If_Modified_Since = new DateTime ();
- If (isset ($ arr ["If-Modified-Since"]) = false)
- {
- $ If_Modified_Since = new DateTime ("1900-1-1"); // If the read request header contains no value If-Modified-Since, the default value is 19000101.
- }
- Else
- {// Echo "##< br> ";
- // Echo "If-Modified-Since:". $ arr ["If-Modified-Since"]. "<br> ";
- // Echo "##< br> ";
- $ If_Modified_Since = new DateTime ($ arr ["If-Modified-Since"]);
- }
- $ Now = new DateTime ();
- Echo "##< br> ";
- Echo $ now-> format ('Y-m-d H: I: s '). "---". $ If_Modified_Since-> format ('Y-m-d H: I: s '). "<br> ";
- Echo "##< br> ";
- $ Diff = strtotime ($ now-> format ('Y-m-d H: I: s ')) -strtotime ($ If_Modified_Since-> format ('Y-m-d H: I: s '));
- Echo "diff:". "$ diff <br> ";
- If ($ diff <10) // if the last modification time of the cache is 10 seconds different from the current time
- {
- Header ('etag: ', true, 304); // return code 304 to read the cache.
- Exit ();
- }
- Else // otherwise
- {
- $ Now = new DateTime ();
- // Display content
- Header ("last-modified:". $ now-> format ('Y-m-d H: I: s '));
- Print ("Content Changed:". $ now-> format ('Y-m-d H: I: s'). "<br> ");
- }
- ?>
-
Take the PHP page as an example:
At the first access, the response header is 200, and the request header does not contain If_Modified_Since.
650) this. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "title =" clipboard "border =" 0 "alt =" clipboard "height =" 406 "src =" http://www.bkjia.com/uploads/allimg/131228/1245014415-0.png "/>
Access again within 10 s:
650) this. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "title =" clipboard [1] "border =" 0 "alt =" clipboard [1] "height =" 463 "src =" http://www.bkjia.com/uploads/allimg/131228/1245015X3-1.png "/>
The value of the If_Modified_Since header sent is 16:16:30. The server sent 304 messages within 10 seconds, forcing the browser to call the cache.
This article is from the "one blog" blog, please be sure to keep this source http://cnn237111.blog.51cto.com/2359144/997633