ASP.NET獲取客戶端IP地址、系統版本、瀏覽器版本

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class WebStart : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    //獲取瀏覽器版本號
    public string getBrowser() {
        string browsers;
        HttpBrowserCapabilities bc = HttpContext.Current.Request.Browser;
        string aa = bc.Browser.ToString();
        string bb = bc.Version.ToString();
        browsers = aa + bb;
        return browsers;
    }
    //獲取客戶端IP地址
    public string getIP()
    {
        string result = String.Empty;
        result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
        if (null == result || result == String.Empty)
        {
            result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
        }
        if (null == result || result == String.Empty)
        {
            result = HttpContext.Current.Request.UserHostAddress;
        }
        if (null == result || result == String.Empty)
        {
            return "0.0.0.0";
        }
        return result;
    }
    //獲取操作系統版本號
    public static string SystemCheck()
    {
        string Agent = HttpContext.Current.Request.ServerVariables["HTTP_USER_AGENT"];

        if (Agent.IndexOf("NT 4.0") > 0)
        {
            return "Windows NT ";
        }
        else if (Agent.IndexOf("NT 5.0") > 0)
        {
            return "Windows 2000";
        }
        else if (Agent.IndexOf("NT 5.1") > 0)
        {
            return "Windows XP";
        }
        else if (Agent.IndexOf("NT 5.2") > 0)
        {
            return "Windows 2003";
        }
        else if (Agent.IndexOf("NT 6.0") > 0)
        {
            return "Windows Vista";
        }
        else if (Agent.IndexOf("WindowsCE") > 0)
        {
            return "Windows CE";
        }
        else if (Agent.IndexOf("NT") > 0)
        {
            return "Windows NT ";
        }
        else if (Agent.IndexOf("9x") > 0)
        {
            return "Windows ME";
        }
        else if (Agent.IndexOf("98") > 0)
        {
            return "Windows 98";
        }
        else if (Agent.IndexOf("95") > 0)
        {
            return "Windows 95";
        }
        else if (Agent.IndexOf("Win32") > 0)
        {
            return "Win32";
        }
        else if (Agent.IndexOf("Linux") > 0)
        {
            return "Linux";
        }
        else if (Agent.IndexOf("SunOS") > 0)
        {
            return "SunOS";
        }
        else if (Agent.IndexOf("Mac") > 0)
        {
            return "Mac";
        }
        else if (Agent.IndexOf("Linux") > 0)
        {
            return "Linux";
        }
        else if (Agent.IndexOf("Windows") > 0)
        {
            return "Windows";
        }
        return "未知類型";

    }
 

}

 

發佈了27 篇原創文章 · 獲贊 0 · 訪問量 1077
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章