public class WebHelper { [DllImport("Iphlpapi.dll")] private static extern int SendARP(int dest, int host, ref long mac, ref int length); [DllImport("Ws2_32.dll")] private static extern int inet_addr(string ip); /// <summary> /// 获取客户端IP /// </summary> /// <returns></returns> public static string GetClientIP() { string result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; if (string.IsNullOrEmpty(result)) { result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; } if (string.IsNullOrEmpty(result)) { result = HttpContext.Current.Request.UserHostAddress; } return result; } /// <summary> /// 获取客户端MAC地址 /// </summary> /// <returns></returns> public static string GetClientMACAddress() { string mac_dest = string.Empty; try { string strClientIP = HttpContext.Current.Request.UserHostAddress.Trim(); int ldest = inet_addr(strClientIP); //目的地的ip inet_addr("");//本地服务器的ip long macinfo = new long(); int len = 6; SendARP(ldest, 0, ref macinfo, ref len); string mac_src = macinfo.ToString("X"); if (mac_src != "0") { while (mac_src.Length < 12) { mac_src = mac_src.Insert(0, "0"); } for (int i = 0; i < 11; i++) { if (0 == (i % 2)) { mac_dest = i == 10 ? mac_dest.Insert(0, mac_src.Substring(i, 2)) : "-" + mac_dest.Insert(0, mac_src.Substring(i, 2)); } } } } catch { //igone } return mac_dest; } public static string GetClientHostName() { string result = HttpContext.Current.Request.ServerVariables["SERVER_NAME"]; if (string.IsNullOrEmpty(result)) { result=HttpContext.Current.Request.UserHostName; } return result; } /// <summary> /// 判断是否为AJAX请求 /// </summary> /// <param name="request"></param> /// <returns></returns> public static bool IsXHR(HttpRequestBase request) { bool ret = false; if (request != null) { if (request.Headers["X-Requested-With"] != null && request.Headers["X-Requested-With"].Trim().Length > 0) { ret = true; } } return ret; } }