欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用淘宝ip地址库查ip的示例

程序员文章站 2024-02-23 14:19:34
使用淘宝ip地址库的api查询ip地址信息。 taobaoiphelper.cs 复制代码 代码如下:using system;using syste...

使用淘宝ip地址库的api查询ip地址信息。

taobaoiphelper.cs

复制代码 代码如下:

using system;
using system.collections.generic;
using system.linq;
using system.text;

namespace lixincommon
{
    /// <summary>
    /// 淘宝ip地址库帮助类。
    /// 提供查询ip地址信息功能。
    /// 服务的主页:http://ip.taobao.com
    /// 作者:lixin
    /// 作者email:lixin@lixin.me
    /// 日期:2014年3月30日
    /// 备注:为了保障服务正常运行,每个用户的访问频率需小于10qps。
    /// </summary>
    public class taobaoiphelper
    {
        /// <summary>
        /// 查询ip信息
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public static taobaoip getip(string ip)
        {
            system.net.webclient client = new system.net.webclient();
            try
            {
                string data = client.downloadstring("http://ip.taobao.com/service/getipinfo.php?ip=" + ip);
                //当code==1时,表示失败
                if (data.indexof("\"code\":1") > -1)
                {
                    return new taobaoip()
                    {
                        code = 1,
                        errormsg = data
                    };
                }
                using (system.io.memorystream mm = new system.io.memorystream(encoding.utf8.getbytes(data)))
                {
                    system.runtime.serialization.json.datacontractjsonserializer myjson = new system.runtime.serialization.json.datacontractjsonserializer(typeof(taobaoip));
                    taobaoip item = (taobaoip)myjson.readobject(mm);

                    return item;
                }
            }
            catch (exception ex)
            {
                return new taobaoip()
                {
                    code = 1,
                    errormsg = ex.message,
                    data = new taobaoip_data() { }
                };
            }
        }
        /// <summary>
        /// 根据域名返回域名的ip信息
        /// </summary>
        /// <param name="domain"></param>
        /// <returns></returns>
        public static taobaoip[] getdomain(string domain)
        {
            system.net.ipaddress[] ips = system.net.dns.gethostaddresses(domain);
            taobaoip[] result = new taobaoip[ips.length];
            for (int i = 0, j = ips.length; i < j; i++)
            {
                result[i] = getip(ips[i].tostring());
            }
            return result;
        }
    }
    public struct taobaoip
    {
        /// <summary>
        /// 0:成功;1:失败
        /// </summary>
        public int code;
        /// <summary>
        /// ip响应信息
        /// </summary>
        public taobaoip_data data;
        /// <summary>
        /// 错误信息
        /// </summary>
        [nonserialized]
        public string errormsg;
    }
    public struct taobaoip_data
    {
        /// <summary>
        /// ip地址
        /// </summary>
        public string ip;
        /// <summary>
        /// 国家
        /// </summary>
        public string country;
        /// <summary>
        /// 区域
        /// </summary>
        public string area;
        /// <summary>
        /// 省份
        /// </summary>
        public string region;
        /// <summary>
        /// 城市
        /// </summary>
        public string city;
        /// <summary>
        /// 运营商
        /// </summary>
        public string isp;
        public string country_id;
        public string area_id;
        public string region_id;
        public string city_id;
        public string county_id;
        public string isp_id;
    }
}

复制代码 代码如下:

taobaoip result1 = taobaoiphelper.getip("8.8.8.8");
            if (result1.code == 0)
            {
                console.writeline(string.format("调用成功。该ip的国家为{0},省份为{1},城市为{2},运营商为{3}"
                    , result1.data.country, result1.data.region, result1.data.city, result1.data.isp)
                    );
            }
            else
            {
                console.writeline("失败,原因为:"+result1.errormsg);
            }
            taobaoip[] result2 = taobaoiphelper.getdomain("lixin.me");
            foreach (taobaoip item in result2)
            {
                if (item.code == 0)
                {
                    console.writeline(string.format("调用成功。该ip的国家为{0},省份为{1},城市为{2},运营商为{3}"
                    , item.data.country, item.data.region, item.data.city, item.data.isp)
                    );
                }
                else
                {
                    console.writeline("失败,原因为:" + item.errormsg);
                }
            }