C#获取客户端Ip工具类

2021-05-17 20:28

阅读:410

标签:result   hostname   browser   ipaddress   try   col   sum   pad   返回   

            string pcname = Dns.GetHostName();
            string ip = Dns.GetHostAddresses(pcname).First().ToString();

 

using Com.AppCode.Extend;
using Com.AppCode.Helper;
using System.Collections.Generic;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;
using System.Web;

namespace Com.AppCode.Net
{
    /// 
    /// 网络操作
    /// 
    public class NetHelper
    {
        #region Ip(获取Ip)
        /// 
        /// 获取Ip
        /// 
        public static string Ip
        {
            get
            {
                var result = string.Empty;
                if (HttpContext.Current != null)
                    result = GetWebClientIp();
                if (result.IsEmpty())
                    result = GetLanIp();
                return result;
            }
        }

        /// 
        /// 获取Web客户端的Ip
        /// 
        private static string GetWebClientIp()
        {
            var ip = GetWebRemoteIp();
            foreach (var hostAddress in Dns.GetHostAddresses(ip))
            {
                if (hostAddress.AddressFamily == AddressFamily.InterNetwork)
                    return hostAddress.ToString();
            }
            return string.Empty;
        }

        /// 
        /// 获取Web远程Ip
        /// 
        private static string GetWebRemoteIp()
        {
            return HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] ?? HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
        }

        /// 
        /// 获取局域网IP
        /// 
        private static string GetLanIp()
        {
            foreach (var hostAddress in Dns.GetHostAddresses(Dns.GetHostName()))
            {
                if (hostAddress.AddressFamily == AddressFamily.InterNetwork)
                    return hostAddress.ToString();
            }
            return string.Empty;
        }

        #endregion

        #region Host(获取主机名)

        /// 
        /// 获取主机名
        /// 
        public static string Host
        {
            get
            {
                return HttpContext.Current == null ? Dns.GetHostName() : GetWebClientHostName();
            }
        }

        /// 
        /// 获取Web客户端主机名
        /// 
        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

        #region 获取mac地址
        /// 
        /// 返回描述本地计算机上的网络接口的对象(网络接口也称为网络适配器)。
        /// 
        /// 
        public static NetworkInterface[] NetCardInfo()
        {
            return NetworkInterface.GetAllNetworkInterfaces();
        }
        ///
        /// 通过NetworkInterface读取网卡Mac
        ///
        ///
        public static Liststring> GetMacByNetworkInterface()
        {
            Liststring> macs = new Liststring>();
            NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface ni in interfaces)
            {
                macs.Add(ni.GetPhysicalAddress().ToString());
            }
            return macs;
        }
        #endregion

        #region Ip城市(获取Ip城市)
        /// 
        /// 获取IP地址信息
        /// 
        /// 
        /// 
        public static string GetLocation(string ip)
        {
            string res = "";
            try
            {
                string apiUrl = "http://apis.juhe.cn/ip/ip2addr?ip=" + ip + "&dtype=json&key=b39857e36bee7a305d55cdb113a9d725";
                res = HttpHelper.GetHttpWebResponseReturnString(apiUrl, null).ToString();
                var resjson = JsonHelper.ScriptDeserialize(res);
                res = resjson.result.area + " " + resjson.result.location;
            }
            catch
            {
                res = "";
            }
            if (!string.IsNullOrEmpty(res))
            {
                return res;
            }
            try
            {
                string url = "https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query=" + ip + "&resource_id=6006&ie=utf8&oe=gbk&format=json";
                res = HttpHelper.GetHttpWebResponseReturnString(url, null).ToString();
                var resjson = JsonHelper.ScriptDeserialize(res);
                res = resjson.data[0].location;
            }
            catch
            {
                res = "";
            }
            return res;
        }
        /// 
        /// 百度接口
        /// 
        public class obj
        {
            public List data { get; set; }
        }
        public class dataone
        {
            public string location { get; set; }
        }
        /// 
        /// 聚合数据
        /// 
        public class objex
        {
            public string resultcode { get; set; }
            public dataoneex result { get; set; }
            public string reason { get; set; }
            public string error_code { get; set; }
        }
        public class dataoneex
        {
            public string area { get; set; }
            public string location { get; set; }
        }
        #endregion

        #region Browser(获取浏览器信息)
        /// 
        /// 获取浏览器信息
        /// 
        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
    }
}

 

C#获取客户端Ip工具类

标签:result   hostname   browser   ipaddress   try   col   sum   pad   返回   

原文地址:https://www.cnblogs.com/hofmann/p/11766167.html


评论


亲,登录后才可以留言!