根据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); }