| | |
| | | using System; |
| | | using Newtonsoft.Json.Linq; |
| | | using System; |
| | | using System.IO; |
| | | using System.Net; |
| | | using System.Net.Mail; |
| | |
| | | using System.Text.RegularExpressions; |
| | | using System.Threading; |
| | | using System.Web; |
| | | using System.Xml; |
| | | |
| | | namespace Common |
| | | { |
| | |
| | | string str = string.Empty; |
| | | return HttpContext.Current.Request.ServerVariables["HTTP_VIA"] == null ? (HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"] == null ? "0.0.0.0" : HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString()) : (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null ? HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString() : (HttpContext.Current.Request.ServerVariables["HTTP_CLIENT_IP"] == null ? (HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"] == null ? "0.0.0.0" : HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString()) : HttpContext.Current.Request.ServerVariables["HTTP_CLIENT_IP"].ToString())); |
| | | } |
| | | /// <summary> |
| | | /// 获取客户端IP地址 |
| | | /// </summary> |
| | | /// <returns>若失败则返回回送地址</returns> |
| | | public static string GetCurrentUserIp() |
| | | { |
| | | //如果客户端使用了代理服务器,则利用HTTP_X_FORWARDED_FOR找到客户端IP地址 |
| | | string userHostAddress = string.IsNullOrEmpty(HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]) ? null : HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString().Split(',')[0].Trim(); |
| | | //否则直接读取REMOTE_ADDR获取客户端IP地址 |
| | | if (string.IsNullOrEmpty(userHostAddress)) |
| | | { |
| | | userHostAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; |
| | | } |
| | | //前两者均失败,则利用Request.UserHostAddress属性获取IP地址,但此时无法确定该IP是客户端IP还是代理IP |
| | | if (string.IsNullOrEmpty(userHostAddress)) |
| | | { |
| | | userHostAddress = HttpContext.Current.Request.UserHostAddress; |
| | | } |
| | | //最后判断获取是否成功,并检查IP地址的格式(检查其格式非常重要) |
| | | if (!string.IsNullOrEmpty(userHostAddress) && IsIP(userHostAddress)) |
| | | { |
| | | return userHostAddress; |
| | | } |
| | | return "127.0.0.1"; |
| | | } |
| | | |
| | | |
| | | //获取本地IP地址,如:192.168.1.102 |
| | | public static string GetLocalIPAddress() |
| | | { |
| | | var host = Dns.GetHostEntry(Dns.GetHostName()); |
| | | foreach (var ip in host.AddressList) |
| | | { |
| | | if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) |
| | | { |
| | | return ip.ToString(); |
| | | } |
| | | } |
| | | throw new Exception("No network adapters with an IPv4 address in the system!"); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 根据IP 获取物理地址 |
| | | /// </summary> |
| | | /// <param name="ip">Ip地址</param> |
| | | /// <param name="AddressClasses">返回地址类别:0-全地址,1-国家编码, 2-国家,3-地区编码,4-地区名称,5-城市名称</param> |
| | | /// <returns></returns> |
| | | public static string GetIpAddress(string ip, string addressClasses) |
| | | { |
| | | string url = "http://ip-api.com/json/" + ip + "?lang=zh-CN"; |
| | | string result = ""; |
| | | WebRequest wrt = null; |
| | | WebResponse wrp = null; |
| | | try |
| | | { |
| | | wrt = WebRequest.Create(url); |
| | | wrt.Credentials = CredentialCache.DefaultCredentials; |
| | | |
| | | wrp = wrt.GetResponse(); |
| | | StreamReader sr = new StreamReader(wrp.GetResponseStream(), Encoding.UTF8); |
| | | //获取到的是Json数据 |
| | | string html = sr.ReadToEnd(); |
| | | |
| | | if (string.IsNullOrEmpty(html)) |
| | | return null; |
| | | |
| | | //Newtonsoft.Json读取数据 |
| | | JObject obj = Newtonsoft.Json.JsonConvert.DeserializeObject<JObject>(html); |
| | | string countryCode = obj["countryCode"] == null ? "" : obj["countryCode"].ToString(); //国家编码 |
| | | string country = obj["country"] == null ? "" : obj["country"].ToString(); //国家 |
| | | string region = obj["region"] == null ? "" : obj["region"].ToString(); //地区编码 |
| | | string regionName = obj["regionName"] == null ? "" : obj["regionName"].ToString(); //地区名称 |
| | | string city = obj["city"] == null ? "" : obj["city"].ToString(); //城市名称 |
| | | //string province = obj["regionName"].ToString(); |
| | | |
| | | //地址全称 |
| | | if (addressClasses.Equals("0")) |
| | | return country + (city.Equals(regionName) ? city : (regionName + city)); |
| | | //国家编码 |
| | | if (addressClasses.Equals("1")) |
| | | return countryCode; |
| | | //国家名称 |
| | | if (addressClasses.Equals("2")) |
| | | return country; |
| | | //地区编码 |
| | | if (addressClasses.Equals("3")) |
| | | return region; |
| | | //地区名称 |
| | | if (addressClasses.Equals("4")) |
| | | return regionName; |
| | | //城市名称 |
| | | if (addressClasses.Equals("5")) |
| | | //return city.Equals(regionName) ? city : (regionName + city); |
| | | return city; |
| | | |
| | | else |
| | | return country + (city.Equals(regionName) ? city : (regionName + city)); |
| | | |
| | | } |
| | | catch (Exception) |
| | | { |
| | | } |
| | | finally |
| | | { |
| | | if (wrp != null) |
| | | wrp.Close(); |
| | | if (wrt != null) |
| | | wrt.Abort(); |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 检查IP地址格式 |
| | | /// </summary> |
| | | /// <param name="ip"></param> |
| | | /// <returns></returns> |
| | | public static bool IsIP(string ip) |
| | | { |
| | | return System.Text.RegularExpressions.Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$"); |
| | | } |
| | | |
| | | public static string GetTrueWebSitePath() |
| | | { |