ASP中使用Request對象ServerVariable獲取瀏覽器http header所有請求頭數(shù)據(jù)
當(dāng)前位置:點(diǎn)晴教程→知識管理交流
→『 技術(shù)文檔交流 』
當(dāng)瀏覽器向服務(wù)器發(fā)送頁面請求時(shí),以及URL還會(huì)向服務(wù)器提供更多信息??梢栽诜?wù)器端收集該附加頭信息。在發(fā)回瀏覽器請求的頁面內(nèi)容時(shí),服務(wù)器向其發(fā)送一些更多的頭信息。所有這些頭信息都可以在不同的端收集并在我們的腳本中使用。我們可以訪問這些HTTP頭信息,并相應(yīng)地修改服務(wù)器端的內(nèi)容。例如,我們需要為不同的語言顯示不同的頁面。通過讀取HTTP_USER_AGENT上的標(biāo)題信息,我們可以找出客戶端瀏覽器的語言設(shè)置,然后重定向到用戶的特定語言頁面。 要獲取http標(biāo)頭,我們將在ASP中使用Request對象的ServerVariables集合。要顯示特定標(biāo)頭的值,我們必須像這樣使用標(biāo)頭名稱: <%= Request.ServerVariables("HTTP_USER_AGENT")%> 以上行將顯示客戶端瀏覽器詳細(xì)信息,語言設(shè)置等。以下是客戶端Firefox瀏覽器的一個(gè)示例輸出: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.8) Gecko/20061025 Firefox/1.5.0.8 我們可以一次顯示所有標(biāo)題信息,而無需使用其名稱逐個(gè)調(diào)用。在這里我們必須使用ALL_RAW: <%= Request.ServerVariables("ALL_RAW")%> 如果我們需要帶有標(biāo)題列表(名稱)的格式化輸出,那么我們必須使用ALL_HTTP: <%= Request.ServerVariables("ALL_HTTP")%> 我們可以像這樣顯示所有標(biāo)題信息的所有名稱值對: Dim tmpVar For Each tmpVar in Request.ServerVariables Response.Write "<B>" & tmpVar & "</B>:" Response.Write Request.ServerVariables(tmpVar) Response.Write "<br>" Next 上面的代碼將逐個(gè)顯示所有標(biāo)題名稱及其值(此輸出位于我的筆記本電腦上,在WinXP上運(yùn)行IIS,因此服務(wù)器地址為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 編輯過 |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |