Http请求头缓存设置方法
1、直接在.aspx页面中设置
最直接的,在.aspx页面中添加一行如下代码:
<%@ OutputCache Duration="3600" VaryByParam="None" %>
表示将这个页面缓存1小时。运行页面查看请求头信息:
第一次运行,效果如图:
再次请求页面
点击“转到”或者光标移入地址栏然后回车,或者F5刷新页面,效果如图:
注意:缓存对ctrl+F5强刷不起作用。
可以看到,设置后请求响应头中多了max-age、Expires、Last-Modified这三个属性,并且Http状态码也由200变成了304,说明我们成功设置了该页面的缓存。
对比两次请求头里的Cache-Control,发现第一次是no-cache,第二次是max-age=0,并且第三、第四。。次都是max-age=0,关于Cache-Control值的说明如下:
如果no-cache出现在请求中,则代表浏览器要求服务器,此次请求必须重新返回最新文件(请求完成后,你会看到http状态码是200);如果max-age=0出现在响应中,则代表服务器要求浏览器你在使用本地缓存的时候,必须先和服务器进行一遍通信,将etag、 If-Not-Modified等字段传递给服务器以便验证当前浏览器端使用的文件是否是最新的(如果浏览器用的是最新的文件,http状态码返回304,服务器告诉浏览器使用本地缓存即可;否则返回200,浏览器得自己吧文件重新下载一遍)。
请求页面方式说明:
| 首次请求 | 返回状态码200,显然得到全部正文。 |
| F5 | 刷新,对Last-Modified有效,它是让服务器判断是否需要读取缓存,所以,依然存在请求和返回数据。状态码是304。 |
| 点击“转到”或者光标移入地址栏然后回车 | 对Cache-Control有效,是浏览器自己决定是否读取缓存,如果在缓存期限内,浏览器不会向WEB服务器发送请求,我们可以看到send和receive的数据全部是0。无交互,故无状态码。 |
| ctrl+f5 | 相当于是强制刷新,所以状态码200 OK,返回全部正文数据。 |
2、在.cs代码中设置
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; namespace HttpTest
{
public partial class HttpTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
int intHttpCacheMaxAge = 0; //浏览器缓存时间,单位:秒
string strHttpCacheMaxAge = ConfigurationManager.AppSettings["httpCacheMaxAge"];
if (!string.IsNullOrEmpty(strHttpCacheMaxAge))
{
intHttpCacheMaxAge = Convert.ToInt32(strHttpCacheMaxAge);
} //设置缓存过期变量,默认为缓存时间多1秒,即第一次请求总让它返回200
int seconds = intHttpCacheMaxAge + 1;
DateTime? IfModifiedSince = null;
if (!string.IsNullOrEmpty(Request.Headers["If-Modified-Since"]))
{
IfModifiedSince = Convert.ToDateTime(Request.Headers["If-Modified-Since"]);
seconds = (DateTime.Now - Convert.ToDateTime(IfModifiedSince)).Seconds;
}
if (seconds > intHttpCacheMaxAge)
{
Common.Common.currDateTime = DateTime.Now;
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetExpires(Convert.ToDateTime(Common.Common.currDateTime).AddSeconds(intHttpCacheMaxAge));
Response.Cache.SetLastModified(Convert.ToDateTime(Common.Common.currDateTime));
}
else
{
Response.Status = "304 Not Modified..";
Response.StatusCode = 304;
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetExpires(Convert.ToDateTime(Common.Common.currDateTime).AddSeconds(intHttpCacheMaxAge));
Response.Cache.SetLastModified(Convert.ToDateTime(Common.Common.currDateTime));
////上面设置了响应请求状态为304后,我们不希望再继续执行其他的业务逻辑了,所以,
////要阻止后续代码的执行,有以下两种方法:
////方法一:使用CompleteRequest()方法直接执行EndRequest事件,如:
//HttpApplication app = (HttpApplication)sender;
//app.CompleteRequest();
////方法二:使用return跳出代码执行,不执行return后面的任何代码,如:
return;
}
}
catch (Exception ex)
{
Response.Write("异常!" + ex);
}
}
}
}
配置文件:
<!--设置浏览器缓存时间,单位:秒-->
<add key="httpCacheMaxAge" value="10"/>
这种方法可以针对单个页面设置,比较灵活。
3、利用HttpModule和HttpWorkerRequest设置
相比前两种,这种方法效率高些。
为了利用HttpModule,所以我们先要创建一个实现了IHttpModule接口的类,如:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web; namespace HttpTest.Module
{
public class HttpCacheModule:IHttpModule
{
public void Init(HttpApplication context)
{
context.EndRequest += context_EndRequest;
}
void context_EndRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
IServiceProvider provider = (IServiceProvider)app.Context;
HttpWorkerRequest worker = (HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest)); int intHttpCacheMaxAge = 0; //浏览器缓存时间,单位:秒
string strHttpCacheMaxAge = ConfigurationManager.AppSettings["httpCacheMaxAge"];
if (!string.IsNullOrEmpty(strHttpCacheMaxAge))
{
intHttpCacheMaxAge = Convert.ToInt32(strHttpCacheMaxAge);
}
//设置缓存过期变量,默认为缓存时间多1秒,即第一次请求总让它返回200
int seconds = intHttpCacheMaxAge+1;
DateTime? IfModifiedSince = null;
if (app.Context.Request.Headers["If-Modified-Since"]!=null)
{
IfModifiedSince = Convert.ToDateTime(app.Context.Request.Headers["If-Modified-Since"]);
seconds = (DateTime.Now - Convert.ToDateTime(IfModifiedSince)).Seconds;
}
if (seconds > intHttpCacheMaxAge)
{
Common.Common.currDateTime = DateTime.Now;
}
else
{
app.Context.Response.Status = "304 Not Modified..";
app.Context.Response.StatusCode = 304;
}
worker.SendKnownResponseHeader(HttpWorkerRequest.HeaderCacheControl, "public");
worker.SendKnownResponseHeader(HttpWorkerRequest.HeaderExpires, Convert.ToDateTime(Common.Common.currDateTime).AddSeconds(intHttpCacheMaxAge).ToUniversalTime().ToString("r"));
worker.SendKnownResponseHeader(HttpWorkerRequest.HeaderLastModified, Convert.ToDateTime(Common.Common.currDateTime).ToUniversalTime().ToString("r"));
} public void Dispose()
{
} }
}
配置文件:
IIS6中按以下配置:
<system.web>
<httpModules>
<add name="HttpCacheModule" type="HttpTest.Module.HttpCacheModule,HttpTest"/>
</httpModules>
</system.web>
IIS7及后续版中本:
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="HttpCacheModule" type="HttpTest.Module.HttpCacheModule,HttpTest"/>
</modules>
</system.webServer>
另外,为了保存当前时间做缓存过期判断,所以定义一个静态变量,如:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace HttpTest.Common
{
public static class Common
{
public static DateTime? currDateTime = null;
}
}
并且在Global文件的Application_Start方法中初始化它,如:
void Application_Start(object sender, EventArgs e)
{
Common.Common.currDateTime = DateTime.Now;
}
这种方式适合对所有页面都有效,适合批量设置。
参考资料:
https://www.cnblogs.com/fish-li/archive/2012/01/11/2320027.html
https://segmentfault.com/q/1010000002578217
https://www.cnblogs.com/ajunForNet/archive/2012/05/19/2509232.html
https://www.cnblogs.com/futan/archive/2013/04/21/cachehuancun.html
https://www.cnblogs.com/yuanyuan/p/4921717.html(HttpModule详解)
http://www.cnblogs.com/yuanyuan/archive/2010/11/15/1877709.html
http://www.cnblogs.com/luminji/archive/2011/09/14/2174751.html
http://www.cnblogs.com/luminji/archive/2011/09/13/2172737.html
Http请求头缓存设置方法的更多相关文章
- http请求头缓存实现
转自CSDN ouyang-web之路 原文链接:https://blog.csdn.net/cangqiong_xiamen/article/details/90405555 一.浏览器的缓存 st ...
- AngularJS 中设置 AJAX get 请求不缓存的方法
var app = angular.module('manager', ['ngRoute']); app.config(['$routeProvider', function($routeProvi ...
- ASP.NET MSSQL 依赖缓存设置方法
更多的时候,我们的服务器性能损耗还是在查询数据库的时候,所以对数据库的缓存还是显得特别重要,上面几种方式都可以实现部分数据缓存功能.但问题是我们的数据有时候是在变化的,这样用户可能在缓存期间查询的数据 ...
- node响应头缓存设置
我把react项目分成4个板块,在路由的顶层 今天在手机上打开react项目的时候,发现平级路由跳转时某一个图片较多的板块图片总是渲染得很慢,这分明是重新发起请求了. 然后我先查一下react-rou ...
- 网络请求 post 的接受请求头的代理方法
接收响应头 content-Length content-Type - (void)connection:(NSURLConnection *)connection didReceiveRespons ...
- jsonwebapi请求头的设置
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
- 接口测试——HttpClient工具的https请求、代理设置、请求头设置、获取状态码和响应头
目录 https请求 代理设置 请求头设置 获取状态码 接收响应头 https请求 https协议(Secure Hypertext Transfer Protocol) : 安全超文本传输协议, H ...
- 给RabbitMQ发送消息时,设置请求头Header。
消费者的请求头 生产者设置请求头 由于消费者那里,@Payload是接受的消息体,使用了@Header注解,需要请求头,生产者这边就要设置请求头,然后rabbitTemplate再调用convertA ...
- HttpWebRequest 改为 HttpClient 踩坑记-请求头设置
HttpWebRequest 改为 HttpClient 踩坑记-请求头设置 Intro 这两天改了一个项目,原来的项目是.net framework 项目,里面处理 HTTP 请求使用的是 WebR ...
随机推荐
- postgresql数据库的 to_date 和 to_timestamp 将 字符串转换为时间格式
数据库中:字符串 转换为 时间格式 二者区别: to_data 转换为 普通的时间格式 to_timestamp 转换可为 时间戳格式出错场景: 比较同一天 日期大小的时候,很容易出错 ...
- 前端知识点回顾之重点篇——ES6的Iterator和Generator
Iterator 迭代器是一种接口.是一种机制. 为各种不同的数据结构提供统一的访问机制.任何数据结构只要部署 Iterator 接口,就可以完成遍历操作(即依次处理该数据结构的所有成员). Iter ...
- 前端知识扫盲-VUE知识篇一(VUE核心知识)
最近对整个前端的知识做了一次复盘,总结了一些知识点,分享给大家有助于加深印象. 想要更加理解透彻的同学,还需要大家自己去查阅资料或者翻看源码.后面会陆续的更新一些相关注知识点的文章. 文章只提出问题, ...
- SMARTY核心
http://www.smarty.net/http://smarty.php.net/manual/en/ 1.配置 define("ROOTPATH",dirname(__FI ...
- mysql数据库指定ip远程访问
1.登录 mysql -u root -p 之后输入密码进行登陆 2.权限设置及说明 2.1添加远程ip访问权限 GRANT ALL PRIVILEGES ON *.* TO 'root'@'192. ...
- 记一次ceph集群的严重故障 (转)
问题:集群状态,坏了一个盘,pg状态好像有点问题[root@ceph-1 ~]# ceph -s cluster 72f44b06-b8d3-44cc-bb8b-2048f5b4acfe ...
- php进程创建慢导致的502
转自: 作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除地址:http://www.jackxiang.com/post/926 ...
- 还原Master数据库后SQLSERVER的服务无法开启
如果还原Master数据库后,SQLSERVER的服务无法开启,请注意是否因为其他的系统数据库在Master备份中记录的路径与现在的路径不一致导致的. 如果是,可以在cmd中执行“NET START ...
- JavaScript中函数文档注释
/** 方法说明 * @method 方法名 * @for 所属类名 * @param{参数类型}参数名 参数说明 * @return {返回值类型} 返回值说明 */
- Lua易忘点
仅针对自己 __index的理解 __index是:当我们访问一个表中的元素不存在时,则会触发去寻找__index元方法,如果不存在,则返回nil,如果存在,则返回结果 Window = {} Win ...