ASP中使用Request对象ServerVariable获取浏览器http header所有请求头数据
当前位置:点晴教程→知识管理交流
→『 技术文档交流 』
当浏览器向服务器发送页面请求时,以及URL还会向服务器提供更多信息。可以在服务器端收集该附加头信息。在发回浏览器请求的页面内容时,服务器向其发送一些更多的头信息。所有这些头信息都可以在不同的端收集并在我们的脚本中使用。我们可以访问这些HTTP头信息,并相应地修改服务器端的内容。例如,我们需要为不同的语言显示不同的页面。通过读取HTTP_USER_AGENT上的标题信息,我们可以找出客户端浏览器的语言设置,然后重定向到用户的特定语言页面。 要获取http标头,我们将在ASP中使用Request对象的ServerVariables集合。要显示特定标头的值,我们必须像这样使用标头名称: <%= Request.ServerVariables("HTTP_USER_AGENT")%> 以上行将显示客户端浏览器详细信息,语言设置等。以下是客户端Firefox浏览器的一个示例输出: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.8) Gecko/20061025 Firefox/1.5.0.8 我们可以一次显示所有标题信息,而无需使用其名称逐个调用。在这里我们必须使用ALL_RAW: <%= Request.ServerVariables("ALL_RAW")%> 如果我们需要带有标题列表(名称)的格式化输出,那么我们必须使用ALL_HTTP: <%= Request.ServerVariables("ALL_HTTP")%> 我们可以像这样显示所有标题信息的所有名称值对: Dim tmpVar For Each tmpVar in Request.ServerVariables Response.Write "<B>" & tmpVar & "</B>:" Response.Write Request.ServerVariables(tmpVar) Response.Write "<br>" Next 上面的代码将逐个显示所有标题名称及其值(此输出位于我的笔记本电脑上,在WinXP上运行IIS,因此服务器地址为localhost或127.0.0.1): ALL_HTTP: HTTP_ACCEPT:*/* HTTP_ACCEPT_LANGUAGE:en-us HTTP_CONNECTION:Keep-Alive HTTP_HOST:127.0.0.1 HTTP_REFERER:https://127.0.0.1/a/request/ HTTP_USER_AGENT:Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; GTB6.6; InfoPath.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729) HTTP_COOKIE:__utmc=96992031; ASPSESSIONIDAQSACAQC=FHOPPKJCEMKILAHMPECCIAJJ; __utma=96992031.1294834349.1272246700.1286419100.1286425811.117; __utmz=96992031.1272246700.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmb=96992031 HTTP_ACCEPT_ENCODING:gzip, deflate ALL_RAW : Accept: */* Accept-Language: en-us Connection: Keep-Alive Host: 127.0.0.1 Referer: https://127.0.0.1/a/request/ User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; GTB6.6; InfoPath.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729) Cookie: __utmc=96992031; ASPSESSIONIDAQSACAQC=FHOPPKJCEMKILAHMPECCIAJJ; __utma=96992031.1294834349.1272246700.1286419100.1286425811.117; __utmz=96992031.1272246700.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmb=96992031 Accept-Encoding: gzip, deflate APPL_MD_PATH /LM/W3SVC/1/ROOT APPL_PHYSICAL_PATH I:\FL\ AUTH_PASSWORD AUTH_TYPE AUTH_USER CERT_COOKIE CERT_FLAGS CERT_ISSUER CERT_KEYSIZE CERT_SECRETKEYSIZE CERT_SERIALNUMBER CERT_SERVER_ISSUER CERT_SERVER_SUBJECT CERT_SUBJECT CONTENT_LENGTH 0 CONTENT_TYPE GATEWAY_INTERFACE CGI/1.1 HTTPS off HTTPS_KEYSIZE HTTPS_SECRETKEYSIZE HTTPS_SERVER_ISSUER HTTPS_SERVER_SUBJECT INSTANCE_ID 1 INSTANCE_META_PATH /LM/W3SVC/1 LOCAL_ADDR 127.0.0.1 LOGON_USER PATH_INFO /a/request/header.asp PATH_TRANSLATED I:\FL\a\ request\header.asp QUERY_STRING REMOTE_ADDR 127.0.0.1 REMOTE_HOST 127.0.0.1 REMOTE_USER REQUEST_METHOD GET script_NAME /a/request/header.asp SERVER_NAME 127.0.0.1 SERVER_PORT 80 SERVER_PORT_SECURE 0 SERVER_PROTOCOL HTTP/1.1 SERVER_SOFTWARE Microsoft-IIS/5.1 URL /a/request/header.asp HTTP_ACCEPT */* HTTP_ACCEPT_LANGUAGE en-us HTTP_CONNECTION Keep-Alive HTTP_HOST 127.0.0.1 HTTP_REFERER https://127.0.0.1/a/ request/ HTTP_ACCEPT_ENCODING gzip, deflate HTTP_USER_AGENT: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; GTB6.6; InfoPath.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729) HTTP_COOKIE __utmc=96992031; ASPSESSIONIDAQSACAQC=FHOPPKJCEMKILAHMPECCIAJJ; __utma=96992031.1294834349.1272246700.1286419100.1286425811.117; __utmz=96992031.1272246700.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmb=96992031 该文章在 2023/10/9 17:48:58 编辑过 |
关键字查询
相关文章
正在查询... |