首页 > 代码库 > 根据IP获取国家
根据IP获取国家
国外的还算比较权威的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获取国家
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。