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

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

TaobaoIPHelper.cs

复制代码 代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LixinCommon
{
    ///


    /// 淘宝IP地址库帮助类。
    /// 提供查询ip地址信息功能。
    /// 服务的主页:http://ip.taobao.com
    /// 作者:lixin
    /// 作者Email:[email protected]
    /// 日期:2014年3月30日
    /// 备注:为了保障服务正常运行,每个用户的访问频率需小于10qps。
    ///

    public class TaobaoIPHelper
    {
        ///
        /// 查询ip信息
        ///

        ///
        ///
        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() { }
                };
            }
        }
        ///


        /// 根据域名返回域名的ip信息
        ///

        ///
        ///
        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
    {
        ///
        /// 0:成功;1:失败
        ///

        public int code;
        ///
        /// ip响应信息
        ///

        public TaobaoIP_Data data;
        ///
        /// 错误信息
        ///

        [NonSerialized]
        public string errorMsg;
    }
    public struct TaobaoIP_Data
    {
        ///
        /// ip地址
        ///

        public string ip;
        ///
        /// 国家
        ///

        public string country;
        ///
        /// 区域
        ///

        public string area;
        ///
        /// 省份
        ///

        public string region;
        ///
        /// 城市
        ///

        public string city;
        ///
        /// 运营商
        ///

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

你可能感兴趣的:(使用淘宝ip地址库查ip的示例)