您的位置:首页 > 其它

根据IP获取国家

2014-12-12 10:46 260 查看
国外的还算比较权威的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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: