d
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 6 using System.Text; 7 using System.Runtime.InteropServices; 8 9 /// <summary> 10 ///NetHelper 的摘要说明 11 /// </summary> 12 public class NetHelper 13 { 14 public NetHelper() 15 { 16 17 } 18 19 public static string GetBrowserType() 20 { 21 return HttpContext.Current.Request.Browser.Type; 22 } 23 24 public static string GetSysVersion() 25 { 26 string Agent = HttpContext.Current.Request.ServerVariables["HTTP_USER_AGENT"]; 27 28 if (Agent.IndexOf("NT 4.0") > 0) 29 { 30 return "Windows NT "; 31 } 32 else if (Agent.IndexOf("NT 5.0") > 0) 33 { 34 return "Windows 2000"; 35 } 36 else if (Agent.IndexOf("NT 5.1") > 0) 37 { 38 return "Windows XP"; 39 } 40 else if (Agent.IndexOf("NT 5.2") > 0) 41 { 42 return "Windows 2003"; 43 } 44 else if (Agent.IndexOf("NT 6.0") > 0) 45 { 46 return "Windows Vista"; 47 } 48 else if (Agent.IndexOf("WindowsCE") > 0) 49 { 50 return "Windows CE"; 51 } 52 else if (Agent.IndexOf("NT") > 0) 53 { 54 return "Windows NT "; 55 } 56 else if (Agent.IndexOf("9x") > 0) 57 { 58 return "Windows ME"; 59 } 60 else if (Agent.IndexOf("98") > 0) 61 { 62 return "Windows 98"; 63 } 64 else if (Agent.IndexOf("95") > 0) 65 { 66 return "Windows 95"; 67 } 68 else if (Agent.IndexOf("Win32") > 0) 69 { 70 return "Win32"; 71 } 72 else if (Agent.IndexOf("Linux") > 0) 73 { 74 return "Linux"; 75 } 76 else if (Agent.IndexOf("SunOS") > 0) 77 { 78 return "SunOS"; 79 } 80 else if (Agent.IndexOf("Mac") > 0) 81 { 82 return "Mac"; 83 } 84 else if (Agent.IndexOf("Linux") > 0) 85 { 86 return "Linux"; 87 } 88 else if (Agent.IndexOf("Windows") > 0) 89 { 90 return "Windows"; 91 } 92 return "unknow"; 93 94 } 95 96 97 /// <summary> 98 /// 如果有代理那么越过代理直接取值 99 /// </summary> 100 /// <returns></returns> 101 public static string GetClientIp() 102 { 103 if (HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null) 104 return HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString(); 105 else 106 return HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString(); 107 } 108 109 public static string GetHostName(string ipStr) 110 { 111 112 string hostName = string.Empty; 113 try 114 { 115 System.Net.IPAddress ip = System.Net.IPAddress.Parse(ipStr); 116 System.Net.IPHostEntry host = System.Net.Dns.GetHostEntry(ip); 117 hostName = host.HostName; 118 } 119 catch 120 { } 121 return hostName; 122 } 123 124 125 126 [DllImport("Iphlpapi.dll")] 127 static extern int SendARP(Int32 DestIP, Int32 SrcIP, ref Int64 MacAddr, ref Int32 PhyAddrLen); 128 [DllImport("Ws2_32.dll")] 129 static extern Int32 inet_addr(string ipaddr); 130 /// <summary> 131 /// SendArp获取MAC地址 132 /// </summary> 133 /// <param name="RemoteIP">目标机器的IP地址如(192.168.1.1)</param> 134 /// <returns>目标机器的mac 地址</returns> 135 public static string getMacAddr_Remote(string RemoteIP) 136 { 137 StringBuilder macAddress = new StringBuilder(); 138 try 139 { 140 Int32 remote = inet_addr(RemoteIP); 141 Int64 macInfo = new Int64(); 142 Int32 length = 6; 143 SendARP(remote, 0, ref macInfo, ref length); 144 string temp = Convert.ToString(macInfo, 16).PadLeft(12, '0').ToUpper(); 145 int x = 12; 146 for (int i = 0; i < 6; i++) 147 { 148 if (i == 5) 149 { 150 macAddress.Append(temp.Substring(x - 2, 2)); 151 } 152 else 153 { 154 macAddress.Append(temp.Substring(x - 2, 2) + "-"); 155 } 156 x -= 2; 157 } 158 return macAddress.ToString(); 159 } 160 catch 161 { 162 return macAddress.ToString(); 163 } 164 } 165 }
程序员的基础教程:菜鸟程序员
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现