码迷,mamicode.com
首页 > 其他好文 > 详细

根据IP获取国家

时间:2014-12-12 11:16:49      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   sp   for   

国外的还算比较权威的IP地址库,而且免费,每天调用次数不超过1000免费。超过另收费。

public string Ip2Country(string ip)
        {
            try
            {
                string url = "http://api.ipinfodb.com/v3/ip-country/?key=<your key,should regist first>&ip=" + ip;
                string rst = HttpCrossDomain.Get(url,10000);
                if (rst.Split(;).Count() >= 4)
                    rst = rst.Split(;)[3];
                else
                    rst = "";
                return rst;
            }
            catch
            {
                return "ERROR";
            }
        }
/// <summary>
        /// 跨域访问
        /// </summary>
        /// <param name="url"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public static string Get(string url, int time = 60000)
        {
            Uri address = new Uri(url);
            HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
            request.Method = "GET";
            request.ContentType = "application/json;charset=utf-8"; //"application/x-www-form-urlencoded";
            request.Timeout = time;
            string result = "";
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                StreamReader reader = new StreamReader(response.GetResponseStream());
                result = reader.ReadToEnd();
            }
            return (result);
        }

 

根据IP获取国家

标签:style   blog   http   io   ar   color   os   sp   for   

原文地址:http://www.cnblogs.com/qidian10/p/4159226.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!