http协议:
国内大陆优化(支持ipv6)http://api.ipshudi.com/ipdata/
国际各洲覆盖(部份ipv6)http://api.ip138.com/ipdata/
https协议:
国内大陆优化(支持ipv6)https://api.ipshudi.com/ipdata/
国际各洲覆盖(部份ipv6)https://api.ip138.com/ipdata/
* API接口可能会因为各种网络原因和攻击都可能产生阻断,请开发时做好冗余和异常处理
* 当HTTP请求返回的状态码非200时,请做异常处理,比如 202 状态码造成的原因可能是无效Token、余额不足、格式错误
C#(.net)web调用iP查询接口示例:
using System; using System.Collections.Generic; using System.Web; using System.Net; using System.Text; public class IP138 { public static string GetIPData(string token, string ip = null, string datatype = "txt") { if (string.IsNullOrEmpty(ip)) { ip = HttpContext.Current.Request.UserHostAddress; } 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 client.DownloadString(url); } } } string data = IP138.GetIPData("00d5cb1fac5dc5cbfe2ff218222a2dfd33", Request.UserHostAddress); Console.WriteLine(data); Response.Write(data);
<% string ip = Request.UserHostAddress; string strMid = "777"; //用户中心查看您的mid string oid = "777"; //用户中心查看您的oid string token = "00d5cb1fac5dc5cbfe2ff218222a2dfd33"; //用户中心查看您的token string url = "https://api.ip138.com/ipdata/?"; string str = "ip=" + ip + "&token=" + token; string sign = FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").ToLower(); %> <script type="text/javascript"> function find(str){alert(str.data);} </script> <script type="text/javascript" src="<%= url+"ip="+ip+"&callback=find&mid="+strMid+"&oid="+oid+"&sign="+sign %>"></script>