获取客户端IP、系统版本

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write(HttpContext.Current.Request.ServerVariables["HTTP_USER_AGENT"]+"<br/>");
 
        IPAddress ip = IPAddress.Parse(Page.Request.UserHostAddress);
        IPHostEntry ine = Dns.GetHostByAddress(ip);
        Response.Write("客户端计算机名:" + ine.HostName + "<br/>");
    }
 
    //获取浏览器版本号
     //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 / Windows Server 2008 ";   
        }
        else if (Agent.IndexOf("NT 6.1") > 0)
        {
            return "Windows 7 / Windows Server 2008 R2";
        }
        else if (Agent.IndexOf("NT 6.2") > 0)
        {
            return "Windows 8";
        }   
        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 "未知类型";   
    }

    /// <summary>
    /// 获取网卡物理地址
    /// </summary>
    /// <returns></returns>
    public string getMacAddr_Local()
    {
        string madAddr = null;
        ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
        ManagementObjectCollection moc2 = mc.GetInstances();
        foreach (ManagementObject mo in moc2)
        {
            if (Convert.ToBoolean(mo["IPEnabled"]) == true)
            {
                madAddr = mo["MacAddress"].ToString();
                madAddr = madAddr.Replace(':', '-');
            }
            mo.Dispose();
        }
        return madAddr;
    }
    /// <summary>
    /// SendArp获取MAC地址(获取本机IP没问题,远程获取不到)
    /// </summary>
    /// <param name="RemoteIP">目标机器的IP地址如(192.168.1.1)</param>
    /// <returns>目标机器的mac 地址</returns>
    [DllImport("Iphlpapi.dll")]
    static extern int SendARP(Int32 DestIP, Int32 SrcIP, ref Int64 MacAddr, ref Int32 PhyAddrLen);
    [DllImport("Ws2_32.dll")]
    static extern Int32 inet_addr(string ipaddr);         
    public string getMacAddr_Remote(string RemoteIP)
    {
        StringBuilder macAddress = new StringBuilder();
        try
        {
            Int32 remote = inet_addr(RemoteIP);
            Int64 macInfo = new Int64();
            Int32 length = 6;
            SendARP(remote, 0, ref macInfo, ref length);
            string temp = Convert.ToString(macInfo, 16).PadLeft(12, '0').ToUpper();
            int x = 12;
            for (int i = 0; i < 6; i++)
            {
                if (i == 5)
                {
                    macAddress.Append(temp.Substring(x - 2, 2));
                }
                else
                {
                    macAddress.Append(temp.Substring(x - 2, 2) + "-");
                }
                x -= 2;
            }
            return macAddress.ToString();
        }
        catch
        {
            return macAddress.ToString();
        }
    }

 

posted @ 2014-08-18 11:43  linyongqin  阅读(271)  评论(0编辑  收藏  举报