123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- using System.Web;
- namespace Common
- {
- public sealed class NetHelper
- {
- #region Ip(获取Ip)
- /// <summary>
- /// 获取Ip
- /// </summary>
- public static string Ip
- {
- get
- {
- var result = string.Empty;
- if (HttpContext.Current != null)
- result = GetWebClientIp();
- if (!string.IsNullOrEmpty(result))
- result = GetLanIp();
- return result;
- }
- }
- /// <summary>
- /// 获取Web客户端的Ip
- /// </summary>
- private static string GetWebClientIp()
- {
- var ip = GetWebRemoteIp();
- foreach (var hostAddress in Dns.GetHostAddresses(ip))
- {
- if (hostAddress.AddressFamily == AddressFamily.InterNetwork)
- {
- return hostAddress.ToString();
- }
- else
- {
- return ip == "::1" ? "127.0.0.0" : ip;
- }
- }
- return string.Empty;
- }
- /// <summary>
- /// 获取Web远程Ip
- /// </summary>
- public static string GetWebRemoteIp()
- {
- return HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] ?? HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
- }
- /// <summary>
- /// 获取局域网IP
- /// </summary>
- private static string GetLanIp()
- {
- foreach (var hostAddress in Dns.GetHostAddresses(Dns.GetHostName()))
- {
- if (hostAddress.AddressFamily == AddressFamily.InterNetwork)
- return hostAddress.ToString();
- }
- return string.Empty;
- }
- #endregion Ip(获取Ip)
- #region Host(获取主机名)
- /// <summary>
- /// 获取主机名
- /// </summary>
- public static string Host
- {
- get
- {
- return HttpContext.Current == null ? Dns.GetHostName() : GetWebClientHostName();
- }
- }
- /// <summary>
- /// 获取Web客户端主机名
- /// </summary>
- private static string GetWebClientHostName()
- {
- if (!HttpContext.Current.Request.IsLocal)
- return string.Empty;
- var ip = GetWebRemoteIp();
- var result = Dns.GetHostEntry(IPAddress.Parse(ip)).HostName;
- if (result == "localhost.localdomain")
- result = Dns.GetHostName();
- return result;
- }
- #endregion Host(获取主机名)
- #region Browser(获取浏览器信息)
- /// <summary>
- /// 获取浏览器信息
- /// </summary>
- public static string Browser
- {
- get
- {
- if (HttpContext.Current == null)
- return string.Empty;
- var browser = HttpContext.Current.Request.Browser;
- return string.Format("{0} {1}", browser.Browser, browser.Version);
- }
- }
- #endregion Browser(获取浏览器信息)
- #region 通过IP得到IP所在地省市
- /// <summary>
- /// 将Unicode编码转换成中文
- /// </summary>
- /// <param name="result"></param>
- /// <returns></returns>
- private static string ConvertUnicode2Chinese(string result)
- {
- Regex reg = new Regex(@"(?i)\\[uU]([0-9a-f]{4})");
- return reg.Replace(result, delegate (Match m)
- {
- return ((char)Convert.ToInt32(m.Groups[1].Value, 16)).ToString();
- });
- }
- private static bool HasContains(Predicate<string> handler, string str)
- {
- return handler.Invoke(str);
- }
- #endregion 通过IP得到IP所在地省市
- #region 其他
- /// <summary>
- /// IP地址信息
- /// </summary>
- private class IPAddressItem
- {
- public int ret { get; set; }
- public string start { get; set; }
- public string end { get; set; }
- public string country { get; set; }
- public string province { get; set; }
- public string city { get; set; }
- public string district { get; set; }
- public string isp { get; set; }
- public string type { get; set; }
- public string desc { get; set; }
- }
- #endregion 其他
- }
- }
|