通过IP得到IP所在地省市

/// <summary>         
        /// 通过IP得到IP所在地省市(Porschev)         
        /// </summary>         
        /// <param name="ip"></param>         
        /// <returns></returns>         
        private string GetAddressByIP(string ip)
        {
            string res = String.Empty;
            string url = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=" + ip;

            HttpItem item = new HttpItem
            {
                Url = url,
                Method = "POST",
                Accept = "application/json",
                ContentType = "application/x-www-form-urlencoded"
            };

            //得到请求结果
            string result = helper.GetHtml(item).Html;
            result = ConvertUnicode2Chinese(result);

            JObject obj = (JObject)result.JsonToObject();
            if (obj != null)
            {
                //{ "ret":1,"start":"115.28.0.0","end":"115.29.255.255","country":"中国","province":"北京","city":"北京","district":"","isp":"电信","type":"机房","desc":"中国万网机房电信"}

                res = obj["province"].ToString() + obj["city"].ToString();
            }
            return res;
        }

        /// <summary>
        /// 将Unicode编码转换成中文
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        private 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();
            });
        }

  

posted @ 2017-12-18 21:48  大师兄丶  阅读(1366)  评论(0编辑  收藏  举报