我们已经在几处见到ASP如何创建或修改在响应页面请示时被发送到客户的HTTP报头。在Response对象中有几个属性和方法可帮助我们做到一点。下面是一些报头方法:
· 控制缓存和有效期。
· 创建状态和定制的HTTP报头。
· 指定MIME类型或内容类型。
· 添加PICS标签。
接下来将简要地研究每一个方面。可在“Response Object”主页(show_response.asp)上,单击相关属性名或方法名,来检查我们所说的属性和方法。
1. 缓存和“到期”ASP网页
用户的浏览器以及他们和服务器这间的任一代理服务器,都可以缓存HTML和用ASP创建的网页。当用户随后请求页面时,浏览器就发送一个“最新修改”的请求到服务器(使用一个包含缓存版本的日期的HTTP_IF_MODIFIED_SINCE报头),询问网页是否已被修改。
若没有被修改,服务器应用状态码和消息“304 Not Modified”来响应,浏览器将使用缓存的内容而不会通过网络下载一个副本。若已经存在已修改的版本,它就会与“200 OK”状态码和消息一道被发送出去。
1) Response.CacheContol属性
其他的一些因素也会影响这一处理过程。然而,任一被网页使用的网络路由内的代理服务器(一般位于客户机端),能被通过设置Response.CacheControl属性为Private来放弃缓存网页。在ASP 3.0中对ASP网页这是缺省的,不用设置。但在网页为个别访问者特别定制时尤其有用。这可以阻止别的在同一网络上的用户进入同一网页。当CacheControl的属性值被设定为Public时,允许服务器缓存网页。注意,一些代理服务器可能表现得不尽相同,或忽视或越过这个报头。
在IE4中,在代理服务器缓存可用时,有可能得到一个虚假的“This page has expired”消息。我们已提供了一个网页(expiretest_form.asp),可以通过自己的代理服务器在网络上做试验,来检查这一属性的影响。可以通过在“Response Object”主页中单击“Response. CacheControl”链接来显示这个网页。如下图所示:
这一页面提交到expiretest_result.asp网页时,能够设置Response.CacheControl属性,然后在网页中插入值和脚本被执行的时间:
| <% If Request.Form(“public”) = “on” Then ‘Cache-Control check box was ticked Response.CacheControl = “Public” Else Response.CacheControl = “Private” End If %> <HTML> ... Cache-Control is: <B><% = Response.CacheControl %></B><P> Value in text box is: <B><% Response.Write Request.Form(“textbox”) %> <% Response.Write Right(“0” & Hour(Now),2) & “:” & Right(“0” & Minute(Now),_ & 2) & “:” & Right(“0” & Second(Now),2) %></B> |
| <% ‘Write HTTP headers before any other output If Request.Form(“expires”) = “on” Then _ Response.Expires = Request.Form(“expires_value”) If Request.Form(“expiresabs”) = “on” Then _ Response.ExpiresAbsolute = Request.Form(“expiresabs_value”) If Request.Form(“lastmod”) = “on” Then _ Response.AddHeader “LAST-MODIFIED”, Cstr(Request.Form(“lastmod_value”)) If Request.Form(“pragma”) = “on” Then _ Response.AddHeader “PRAGMA”, CStr(Request.Form(“pragma_value”)) If Request.Form(“refresh”) = “on” Then _ Response.AddHeader “REFRESH”, CStr(Request.Form(“refresh_value”)) If Request.Form(“addheader”) = “on” And Len(Request.Form(“addheader_name”)) Then _ Response.AddHeader CStr(Request.Form(“addheader_name”)), _ CStr(Request.Form(“addheader_value”)) If Request.Form(“status”) = “on” Then _ Response.Status = Request.Form(“status_value”) %> <HTML> ... ... Show code and execution time ... |
| Response.AddHeader “REFRESH”, ”60;URL=newpath/newpage.asp” |
| <META HTTP-EQUIV=”REFRESH”, “60;URL=newpath/newpage.asp”> |
| Response.Status = “302 Object Moved” Response.Addheader “Location”, “newpath/newpage.asp” |
| Response.ContentType = “image/jpeg” |
| QUOT = Chr(34) StrPicsLabel = “(PICS-1.0” & QUOT & “http://www.rsac.org/ratingsv01.html”_ & QUOT & “ 1 gen true comment “ & QUOT _ & “RSACi North America Server” & QUOT & “ for “ & QUOT _ & “http://yoursite.com” & QUOT & “ on “ & QUOT _ & “1999.08.01T03:04-0500” & QUOT & “ r (n 0 s 0 v 2 l 3))” Response.Pics(strPicsLabel) |
| (PICS-1.0 “http://www.rsac.org/ratingsv01.html” 1 gen true comment “RSACi North America Server” for “http://yoursite.com” on “1999.08.01T03:04-0500” r (n 0 s 0 v 2 l 3)) |
| <TABEL CELLPADDING=0 CELLSPACING=0> <% For Each keyItem In Request.ClientCertificate() StrItemValue = Request.ClientCertificate(keyItem) If Len(strItemValue) > 90 Then strItemValue = Left(strItemValue, 60) & “..etc.” Response.Write “<TR><TD>” & keyItem & “ = “ & strItemValue & “</TD></TR>” Next %> </TABLE> |
| If Request.ClientCertificate(“SubjectO”) = “Wrox Press Inc” Then Response.Redirect “/wrox_staff/default.asp” ‘Wrox staff site Else Response.Redirect “/public/Default.asp” ‘Normal public site End If |
| Select Case Request.ClientCertificate(“SubjectC”) Case “UK”: Response.Redirect “http://uk_site.co.uk/” Case “DE”: Response.Redirect “http://de_site.co.de/” Case “FR”: Response.Redirect “http://fr_site.co.fr/” ‘... ect. Case Else: Response.Redirect “http://us_site.com/” End Select |
| varContent = Request.BinaryRead(64) |
| Response.BinaryWrite(varContent) |
| ... If intItemCount > 25 Then Response.AppendToLog “Large order from ‘” & strDept & department.” End If ... |
| strToAppend = “Page executed on ” & Now Response.AppendToLog strToAppend |
发表评论