• asp.net获取服务端和客户端信息


    获取服务器名:Page.Server.ManchineName
    获取用户信息:Page.User

    获取客户端电脑名:Page.Request.UserHostName
    获取客户端IP:Page.Request.UserHostAddress


     

    protected void Page_Load(object sender, EventArgs e)
    {
        // 将客户端的信息添加到 Dictionary 中
        Dictionary<string, string> clientInfos = new Dictionary<string, string>();
        try
        {
            string ip = Request.UserHostAddress;
            string userAgent = Request.UserAgent == null ? "无" : Request.UserAgent;
            if(Request.ServerVariables["HTTP_UA_CPU"] == null)
                clientInfos.Add("CPU 类型", "未知");
            else
                clientInfos.Add("CPU 类型", Request.ServerVariables["HTTP_UA_CPU"]);
            clientInfos.Add("操作系统", this.GetOSNameByUserAgent(userAgent));
            clientInfos.Add("IP 地址", ip);
            if(Request.Browser.ClrVersion == null)
                clientInfos.Add(".NET CLR 版本", "不支持");
            else
                clientInfos.Add(".NET CLR 版本", Request.Browser.ClrVersion.ToString());
            clientInfos.Add("浏览器", Request.Browser.Browser + Request.Browser.Version);
            clientInfos.Add("支持 ActiveX", Request.Browser.ActiveXControls ? "支持" : "不支持");
            clientInfos.Add("支持 Cookies", Request.Browser.Cookies ? "支持" : "不支持");
            clientInfos.Add("支持 CSS", Request.Browser.SupportsCss ? "支持" : "不支持");
            clientInfos.Add("语言", Request.UserLanguages[0]);

            string httpAccept = Request.ServerVariables["HTTP_ACCEPT"];
            if (httpAccept == null)
                clientInfos.Add("计算机/手机", "未知");
            else if (httpAccept.IndexOf("wap") > -1)
                clientInfos.Add("计算机/手机", "手机");
            else
                clientInfos.Add("计算机/手机", "计算机");
            clientInfos.Add("Platform", Request.Browser.Platform);
            clientInfos.Add("Win16", Request.Browser.Win16 ? "是" : "不是");
            clientInfos.Add("Win32", Request.Browser.Win32 ? "是" : "不是");

            if(Request.ServerVariables["HTTP_ACCEPT_ENCODING"] == null)
                clientInfos.Add("Http Accept Encoding", "无");
            else
                clientInfos.Add("Http Accept Encoding", Request.ServerVariables["HTTP_ACCEPT_ENCODING"]);
            clientInfos.Add("User Agent", userAgent);
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
        // 根据 Dictionary 中的内容在 Table 中显示客户端信息
        this.Response.Write("<table cellpadding=0 cellspacing=0>");
        this.Response.Write("<tr>");
        this.Response.Write("<td width=145>项目</td>");
        this.Response.Write("<td>值</td>");
        this.Response.Write("</tr>");
        foreach (string key in clientInfos.Keys)
        {
            this.Response.Write("<tr>");
            this.Response.Write("<td>" + key + "</font></td>");
            this.Response.Write("<td>" + clientInfos[key].ToString() + "</td>");
            this.Response.Write("</tr>");
        }
        this.Response.Write("</table>");          
    }

    /// <summary>
    /// 根据 User Agent 获取操作系统名称
    /// </summary>
    private string GetOSNameByUserAgent(string userAgent)
    {
        string osVersion = "未知";

        if (userAgent.Contains("NT 6.0"))
        {
            osVersion = "Windows Vista/Server 2008";
        }
        else if (userAgent.Contains("NT 5.2"))
        {
            osVersion = "Windows Server 2003";
        }
        else if (userAgent.Contains("NT 5.1"))
        {
            osVersion = "Windows XP";
        }
        else if (userAgent.Contains("NT 5"))
        {
            osVersion = "Windows 2000";
        }
        else if (userAgent.Contains("NT 4"))
        {
            osVersion = "Windows NT4";
        }
        else if (userAgent.Contains("Me"))
        {
            osVersion = "Windows Me";
        }
        else if (userAgent.Contains("98"))
        {
            osVersion = "Windows 98";
        }
        else if (userAgent.Contains("95"))
        {
            osVersion = "Windows 95";
        }
        else if (userAgent.Contains("Mac"))
        {
            osVersion = "Mac";
        }
        else if (userAgent.Contains("Unix"))
        {
            osVersion = "UNIX";
        }
        else if (userAgent.Contains("Linux"))
        {
            osVersion = "Linux";
        }
        else if (userAgent.Contains("SunOS"))
        {
            osVersion = "SunOS";
        }
        return osVersion;
    }

    佛为心,道为骨,儒为表,大度看世界; 技在手,能在身,思在脑,从容过生活; 三千年读史,不外功名利禄; 九万里悟道,终归诗酒田园;
  • 相关阅读:
    12、多线程:Threading、守护线程
    11.1、socket连接中的粘包、精确传输问题
    python网络编程:socket、服务端、客户端
    python正则表达式模块re:正则表达式常用字符、常用可选标志位、group与groups、match、search、sub、split,findall、compile、特殊字符转义
    9.4、__del__、__doc__、__dict__、__module__、__getitem__、__setitem__、__delitem__、__str__、__repr__、__call__
    python:异常处理、自定义异常、断言
    9.3、反射
    9.1.1、私有变量,私有方法
    关掉百度商桥请您留言和在线咨询
    飞飞影视cms标签
  • 原文地址:https://www.cnblogs.com/taofx/p/4136866.html
Copyright © 2020-2023  润新知