Ejemplo n.º 1
0
        ///// <summary>
        ///// (弃用)获取IP地址
        ///// 只获取新浪接口返回信息,已改用 GetIPAddressAdv,从多个接口获取后比较获取最佳结果
        ///// </summary>
        ///// <returns></returns>
        //public static EnpotIPAddressModel GetIPAddress()
        //{
        //    string arg_Ip = IPAddressUtils.GetRequestIP();
        //    //arg_Ip = "205.251.196.138";

        //    if (!IPAddressUtils.IsIP(arg_Ip))
        //    {
        //        throw new Exception("错误:无效IP信息。");
        //    }

        //    string url = string.Format("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip={0}", arg_Ip);
        //    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        //    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        //    System.IO.StreamReader sr = new System.IO.StreamReader(response.GetResponseStream());
        //    string tmpString = sr.ReadToEnd();

        //    SinaIPAddressModel tmpIpAddressModel = null;
        //    try
        //    {
        //        tmpIpAddressModel = JsonConvert.DeserializeObject<SinaIPAddressModel>(tmpString);
        //    }
        //    catch (Exception ex)
        //    {
        //        throw new Exception("错误:转换Json信息失败。", ex);
        //    }
        //    EnpotIPAddressModel result = new EnpotIPAddressModel(arg_Ip, tmpIpAddressModel);
        //    result.Device = IPAddressUtils.GetUserAgent();
        //    return result;
        //}

        /// <summary>
        /// 获取IP地址 多接口比较版
        /// </summary>
        /// <returns></returns>
        public static EnpotIPAddressModel GetIPAddressAdv()
        {
            string arg_Ip = IPAddressUtils.GetRequestIP();

            if (!IPAddressUtils.IsIP(arg_Ip))
            {
                throw new Exception("错误:无效IP信息。");
            }

            List <EnpotIPAddressModel> all = new List <EnpotIPAddressModel>();

            all.Add(IPAddressUtils.SinaGetIPAddress(arg_Ip));
            //all.Add(IPAddressUtils.BaiduGetIPAddress(arg_Ip));
            //all.Add(IPAddressUtils.TaobaoGetIPAddress(arg_Ip));

            EnpotIPAddressModel result = IPAddressUtils.SelectAddress(arg_Ip, all);

            result.Device = IPAddressUtils.GetUserAgent();
            return(result);
        }
Ejemplo n.º 2
0
        protected static EnpotIPAddressModel SinaGetIPAddress(string arg_Ip)
        {
            EnpotIPAddressModel result = null;

            string url = string.Format("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip={0}", arg_Ip);

            try
            {
                HttpWebRequest         request  = (HttpWebRequest)WebRequest.Create(url);
                HttpWebResponse        response = (HttpWebResponse)request.GetResponse();
                System.IO.StreamReader sr       = new System.IO.StreamReader(response.GetResponseStream());
                string tmpString = sr.ReadToEnd();

                dynamic tmpIpAddressModel = JsonConvert.DeserializeObject <dynamic>(tmpString);


                result = new EnpotIPAddressModel()
                {
                    IP       = arg_Ip,
                    Country  = tmpIpAddressModel.country,
                    Province = tmpIpAddressModel.province,
                    City     = tmpIpAddressModel.city,
                    District = tmpIpAddressModel.district,
                    Device   = string.Empty
                };
            }
            catch (Exception)
            {
                //throw new Exception("错误:转换Json信息失败。", ex);
                result          = new EnpotIPAddressModel();
                result.IP       = arg_Ip;
                result.Country  = "未知";
                result.Province = "未知";
                result.City     = "未知";
                result.District = "未知";
            }

            return(result);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 按 市=》省=》国 来选出较准确的地址信息
        /// </summary>
        /// <param name="arg_IP"></param>
        /// <param name="all"></param>
        /// <returns></returns>
        private static EnpotIPAddressModel SelectAddress(string arg_IP, List <EnpotIPAddressModel> all)
        {
            EnpotIPAddressModel result = null;
            var query_City             = from i in all
                                         where i.City != "未知"
                                         group i by new { i.City } into ix
                select new
            {
                City  = ix.Key.City,
                Count = ix.Count()
            };

            var matchCity = query_City.FirstOrDefault(i => i.Count == query_City.Max(o => o.Count));

            if ((matchCity != null && query_City.Count() == 1) ||
                (matchCity != null && matchCity.Count > 1 && query_City.Count() > 1))
            {
                result = all.FirstOrDefault(i => i.City == matchCity.City);
            }
            else
            {
                var query_Province = from i in all
                                     where i.Province != "未知"
                                     group i by new { i.Province } into ix
                    select new
                {
                    Province = ix.Key.Province,
                    Count    = ix.Count()
                };

                var matchProvince = query_Province.FirstOrDefault(i => i.Count == query_Province.Max(o => o.Count));

                if (
                    ((matchProvince != null && query_Province.Count() == 1) ||
                     (matchProvince != null && matchProvince.Count > 1 && query_Province.Count() > 1))

                    )
                {
                    result      = all.FirstOrDefault(i => i.Province == matchProvince.Province);
                    result.City = string.Empty;
                }
                else
                {
                    var query_Country = from i in all
                                        where i.Country != "未知"
                                        group i by new { i.Country } into ix
                        select new
                    {
                        Country = ix.Key.Country,
                        Count   = ix.Count()
                    };

                    var matchCountry = query_Country.FirstOrDefault(i => i.Count == query_Country.Max(o => o.Count));
                    if ((matchCountry != null && query_Country.Count() == 1) ||
                        (matchCountry != null && matchCountry.Count > 1 && query_Country.Count() > 1))
                    {
                        result          = all.FirstOrDefault(i => i.Country == matchCountry.Country);
                        result.Province = string.Empty;
                        result.City     = string.Empty;
                    }
                }
            }

            if (result == null)
            {
                result = new EnpotIPAddressModel()
                {
                    IP       = arg_IP,
                    Country  = "未知",
                    Province = string.Empty,
                    City     = string.Empty
                };
            }

            return(result);
        }