2015年6月2日 星期二

ASP.NET 取得使用者及伺服器 環境變數

http://devco.re/blog/2014/06/19/client-ip-detection/
在撰寫網站程式的時候,有時候會需要依照使用者的環境狀態作一些調整,這時你必須要有方法得知使用者電腦軟體使用狀態,你就可以下面方法 ex: Request.Browser.Browser 得知使用者的瀏覽器,來變更調整你的程式動作。
取得使用者的IP: Request.UserHostAddress
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim bc As HttpBrowserCapabilities = Request.Browser
Response.Write("Browser Capabilities:
")
Response.Write("Type = " & bc.Type & "
")
Response.Write("Name = " & bc.Browser & "
")
Response.Write("Version = " & bc.Version & "
")
Response.Write("Major Version = " & bc.MajorVersion & "
")
Response.Write("Minor Version = " & bc.MinorVersion & "
")
Response.Write("Platform = " & bc.Platform & "
")
Response.Write("Is Beta = " & bc.Beta & "
")
Response.Write("Is Crawler = " & bc.Crawler & "
")
Response.Write("Is AOL = " & bc.AOL & "
")
Response.Write("Is Win16 = " & bc.Win16 & "
")
Response.Write("Is Win32 = " & bc.Win32 & "
")
Response.Write("Supports Frames = " & bc.Frames & "
")
Response.Write("Supports Tables = " & bc.Tables & "
")
Response.Write("Supports Cookies = " & bc.Cookies & "
")
Response.Write("Supports VB Script = " & bc.VBScript & "
")
Response.Write("Supports JavaScript = " & bc.JavaScript & "
")
Response.Write("Supports Java Applets = " & bc.JavaApplets & "
")
Response.Write("Supports ActiveX Controls = " & bc.ActiveXControls & "
")
Response.Write("CDF = " & bc.CDF & "
")
Dim loop1, loop2 As Integer
Dim arr1(), arr2() As String
Dim coll As NameValueCollection
       ' Load ServerVariable collection into NameValueCollection object.
        coll = Request.ServerVariables
        ' Get names of all keys into a string array.
arr1 = coll.AllKeys
For loop1 = 0 To arr1.GetUpperBound(0)
  If Server.HtmlEncode(coll.GetValues(loop1)(0)) <> "" Then
       Response.Write("Key: _" & loop1 & "_" & arr1(loop1) & ": " & Server.HtmlEncode(coll.GetValues(loop1)(0)) & "
")
  End If
'arr2 = coll.GetValues(loop1) ' Get all values under this key.
'For loop2 = 0 To arr2.GetUpperBound(0)
'Response.Write("Value ++" & loop2 & "++" & CStr(loop2) & ": " & Server.HtmlEncode(arr2(loop2)) & "
")
'Next loop2
Next loop1
End Sub
--- 執行的結果如下:----
Browser Capabilities:
Type = IE7
Name = IE
Version = 7.0
Major Version = 7
Minor Version = 0
Platform = WinXP
Is Beta = False
Is Crawler = False
Is AOL = False
Is Win16 = False
Is Win32 = True
Supports Frames = True
Supports Tables = True
Supports Cookies = True
Supports VB Script = True
Supports JavaScript = True
Supports Java Applets = True
Supports ActiveX Controls = True
CDF = False
Key: _0_ALL_HTTP: HTTP_CONNECTION:Keep-Alive HTTP_ACCEPT:*/* HTTP_ACCEPT_ENCODING:gzip, deflate HTTP_ACCEPT_LANGUAGE:zh-tw HTTP_HOST:localhost:2788 HTTP_USER_AGENT:Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; GTB6; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729) HTTP_UA_CPU:x86
Key: _1_ALL_RAW: Connection: Keep-Alive Accept: */* Accept-Encoding: gzip, deflate Accept-Language: zh-tw Host: localhost:2788 User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; GTB6; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729) UA-CPU: x86
Key: _3_APPL_PHYSICAL_PATH: C:\Documents and Settings\Jerry\function_test\
Key: _4_AUTH_TYPE: NTLM
Key: _5_AUTH_USER: WINNER\Jerry
Key: _7_LOGON_USER: WINNER\Jerry
Key: _8_REMOTE_USER: WINNER\Jerry
Key: _18_CONTENT_LENGTH: 0
Key: _28_LOCAL_ADDR: 127.0.0.1
Key: _29_PATH_INFO: /function_test/Default.aspx
Key: _30_PATH_TRANSLATED: C:\Documents and Settings\Jerry\function_test\Default.aspx
Key: _32_REMOTE_ADDR: 127.0.0.1
Key: _33_REMOTE_HOST: 127.0.0.1
Key: _35_REQUEST_METHOD: GET
Key: _36_SCRIPT_NAME: /function_test/Default.aspx
Key: _37_SERVER_NAME: localhost
Key: _38_SERVER_PORT: 2788
Key: _39_SERVER_PORT_SECURE: 0
Key: _40_SERVER_PROTOCOL: HTTP/1.1
Key: _42_URL: /function_test/Default.aspx
Key: _43_HTTP_CONNECTION: Keep-Alive
Key: _44_HTTP_ACCEPT: */*
Key: _45_HTTP_ACCEPT_ENCODING: gzip, deflate
Key: _46_HTTP_ACCEPT_LANGUAGE: zh-tw
Key: _47_HTTP_HOST: localhost:2788
Key: _48_HTTP_USER_AGENT: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; GTB6; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)
Key: _49_HTTP_UA_CPU: x86

沒有留言:

張貼留言