目前亲测可用的几个免费IP地址API接口,无需申请
3、爱奇艺接口
http://ip.geo.iqiyi.com/cityjson?format=json&ip=[IP地址]
4、ip-api接口
http://ip-api.com/json/[IP地址]?lang=zh-CN
5、太平洋接口
http://whois.pconline.com.cn/ipJson.jsp?ip=[IP地址]&json=true
如果使用ip138的接口(收费),代码如下:
public static string GetIPData(string token, string ip = null, string datatype = "txt")
{
if (string.IsNullOrEmpty(ip))
{
ip = HttpContext.Current.Request.UserHostAddress;
}
if (ip.Contains("::")) return "保留地址";
string url = string.Format("https://api.ip138.com/ipdata/?ip={0}&datatype={1}&token={2}", ip, datatype, token);
using (WebClient client = new WebClient())
{
client.Encoding = Encoding.UTF8;
return Regex.Replace(client.DownloadString(url), @"[\d.\s]+", "");
}
}