怎样防止从Cache中读取WML页面 算神 四月 25, 2011 编程相关 share Facebook Twitter Google + LinkedIn Pinterest Email Share... 当WML页面下载到WAP设备后,它将保存在WAP设备内存中一段时间,直到这个时间过期。在这之后,页面将从服务器下载,而不是从WAP设备的缓存读取。这个过程被称做Cache。但是有些时候不想让页面从缓存中读取,而是从服务器端读取。一个典型的例子就是当服务器的内容不断在更新的时候,通过在HTTP头中加入一定的cache信息,来告诉WAP设备该页面将不存储在缓存中。可以在服务器端生成HTTP头,或者使用PHP、ASP、Perl或者其他服务端开发语言。这一行不能被包括在页面里,既然是HTTP的信息头,就不是WML元素。对于静态页面,或许没有使用服务器端脚本语言,许多浏览器支持META标签来控制浏览器的Cache。看本部分的最后的例子。将下面代码加入到HTTP头中,页面将马上过期:Expires: Mon, 26 Jul 1997 05:00:00 GMTLast-Modified: DD. month YYYY HH:MM:SS GMTCache-Control: no-cache, must-revalidatePragma: no-cache第一行告诉微型浏览器,页面已经过期一段时间了。第二行告诉浏览器页面最后一次修改的时间。DD应该换成当天的日期,month YY HH MM SS等等类推。第三行和第四行有同样的效果。告诉浏览器页面不被Cache(第三行适用于 HTTP 1.1,第四行适用于HTTP 1.0)。下面的是PHP的一个例子:<?// set the correct MIME type header("Content-type: text/vnd.wap.wml");// expires in the past header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");// Last modified, right now header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // Prevent caching, HTTP/1.1 header("Cache-Control: no-cache, must-revalidate");// Prevent caching, HTTP/1.0 header("Pragma: no-cache"); ?>下面是使用WebClasses(VB)的例子。使用"Response.Expires=-1",防止Cache。 Private Sub WebClass_Start() 'Set correct MIME type Response.ContentType = "text/vnd.wap.wml" 'Make sure no caching Response.Expires = -1 Response.AddHeader "Pragma", "no-cache" Response.AddHeader "Cache-Control", "no-cache, must-revalidate" 'Use basicwml(my own) as template Set NextItem = basicwml End Sub 这里有一个ASP的例子,同样使用“Response.Expires=-1”防止Cache。<% Response.ContentType = "text/vnd.wap.wml" Response.Expires = -1 Response.AddHeader "Pragma", "no-cache" Response.AddHeader "Cache-Control", "no-cache, must-revalidate"%> 最后是使用META的例子:<?xml version="1.0"?><!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN""http://www.wapforum.org/DTD/wml_1.1.xml"> <wml> <head> <meta forua="true" http-equiv="Cache-Control" content="max-age=0"/> </head> <card id="alwaysexpire"> <p>This deck will never be stored in the cache</p> </card> </wml>下面的页面是在经过86400秒(24 hours)后过期。<?xml version="1.0"?><!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN""http://www.wapforum.org/DTD/wml_1.1.xml"> <wml> <head> <meta forua="true" http-equiv="Cache-Control" content="max-age=86400"/> </head> <card id="expire1day"> <p>This card will live in the cache for a day</p> </card> </wml>有些浏览器例如:UP.Simulator如果可以通过“返回”达到另外一个卡片,那么它将不会重新装载卡片。为了强制这个更新动作,用户必须在META标签中使用must-revalidate 参数。<meta forua="true" http-equiv="Cache-Control" content="must-revalidate"/>