码迷,mamicode.com
首页 > Windows程序 > 详细

c# 获取客户端ip、mac、机器名、操作系统、浏览器信息

时间:2015-07-15 19:03:32      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

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 }

 

c# 获取客户端ip、mac、机器名、操作系统、浏览器信息

标签:

原文地址:http://www.cnblogs.com/guohu/p/4649122.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!