addheader
addheader 方法用指定的值添加 html 标题。该方法常常向响应添加新的 http 标题。它并不替代现有的同名标题。一旦标题被添加,将不能删除。
此方法仅供高级用户使用。若其他 response 方法提供了您所需的功能,建议您使用该方法。
语法
response.addheader name, value
参数
- name
- 新的标题变量的名称。
- value
- 存储在新的标题变量中的初始值。
注释
为避免命名不明确,name 中不能包含任何下划线字符 (_)。servervariables 集合将标题中的下划线字符解释为反斜杠。例如,下面的脚本使服务器查找一个名为 my-header 的标题名。
<% request.servervariables(“http_my_header”) %>
由于 http 协议要求所有的标题都必须在内容之前发送,所以您必须在任何的输出(例如由 html 或 write 方法生成的输出)发送到客户端之前在脚本中调用 addheader。但当 buffer 属性被设置为 true 时例外。若输出被缓冲,那么您就可以在脚本中的任何地方调用 addheader 方法,只要它在 flush 之前执行即可。否则,对 addheader 的调用将产生一个运行错误。
下面的两个 .asp 文件对这一点进行了解释。
-------file1.asp--------- <% response.addheader "warning", "error message text" %> <html> some text on the web page. </html>
在前面的例子中,页没有缓冲。但是,因为在服务器将输出
some text on the web page
发送到客户端之前调用了 addheader 方法,所以脚本能正常工作。如果调换一下顺序,则对 addheader 方法的调用将产生一个运行时错误。
------file2.asp----------
<% response.buffer = true %>
<html>
heres some text on your web page.
<% response.addheader "warning", "error message text" %> heres some more interesting and illuminating text.
<% response.flush %>
<%= response.write("some string") %>
</html>
在前面的示例中,页被缓冲了,其结果是,直到此页上所有的 asp 脚本执行后或 flush 方法被调用后,服务器才会将输出发送到客户端。带缓冲的输出中对 addheader 的调用可在脚本的任何地方出现,只要在 flush 调用之前即可。在前面的示例中,若对 addheader 的调用在对 flush 的调用之后出现,脚本将产生一个运行时错误。
您可以通过这一方法用不同的值发送同一标题的多份拷贝,比如用 www-authenticate 标题。
示例
下面这个示例使用 addheader 方法要求客户端使用 basic 验证。
<% response.addheader "www-authenticate", "basic" %>
注意 前面的脚本仅通知客户端浏览器使用哪个验证。若您在 web 应用程序中使用该脚本,则一定要启用 web 服务器的 basic 验证。
应用于
response 对象
