c# 获取客户端ip、mac、机器名、操作系统、浏览器信息
2015-07-15 18:02
826 查看
d
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Text; using System.Runtime.InteropServices; /// <summary> ///NetHelper 的摘要说明 /// </summary> public class NetHelper { public NetHelper() { } public static string GetBrowserType() { return HttpContext.Current.Request.Browser.Type; } public static string GetSysVersion() { 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 "unknow"; } /// <summary> /// 如果有代理那么越过代理直接取值 /// </summary> /// <returns></returns> public static string GetClientIp() { if (HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null) return HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString(); else return HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString(); } public static string GetHostName(string ipStr) { string hostName = string.Empty; try { System.Net.IPAddress ip = System.Net.IPAddress.Parse(ipStr); System.Net.IPHostEntry host = System.Net.Dns.GetHostEntry(ip); hostName = host.HostName; } catch { } return hostName; } [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); /// <summary> /// SendArp获取MAC地址 /// </summary> /// <param name="RemoteIP">目标机器的IP地址如(192.168.1.1)</param> /// <returns>目标机器的mac 地址</returns> public static 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(); } } }
相关文章推荐
- C#非递归先序遍历二叉树实例
- 转-C#让枚举返回字符串
- C# winform 导出pdf报表
- C# 鼠标全局钩子
- C#创建自签名认证文件的方法
- C#发送数据到剪贴板及从剪贴板中取数据的方法
- C#访问修饰符
- C# 学习笔记
- C# 结构体访问器错误提示
- C#实现Zip压缩目录中所有文件的方法
- C#简单实现SNMP的方法
- 在C#中实现Word页眉页脚的所有功能
- C# 批处理制作静默安装程序包
- C#使用CDO发送邮件的方法
- C# 判断文件的真实格式
- C# 事件穿透 事件冒泡 鼠标点击后在父级控件捕获
- C# 事件穿透 事件冒泡 鼠标点击后在父级控件捕获
- C#通过windows注册表获取软件清单的方法
- C#实现利用Windows API读写INI文件的方法
- C#图片加文字、图片水印