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

淘宝IP地址库采集器c#代码

程序员文章站 2024-02-18 22:31:28
采集器概貌,如下: 最近做一个项目,功能类似于cnzz站长统计功能,要求显示ip所在的省份市区/提供商等信息。网上的ip纯真数据库,下载下来一看,发现没提供商内容,省...

采集器概貌,如下:

淘宝IP地址库采集器c#代码

最近做一个项目,功能类似于cnzz站长统计功能,要求显示ip所在的省份市区/提供商等信息。网上的ip纯真数据库,下载下来一看,发现没提供商内容,省市区都很少,居然有xxx网吧,哥瞬间倒了。没标准化、并且杂乱、还不连续的ip段、总体说来没达到要求。

  在百度上找啊找,找到淘宝ip地址库,官方介绍的相当诱人,准确率高,数据质量有保障,提供国家、省、市、县、运营商全方位信息,信息维度广,格式规范,但是限制每秒10次的访问(这个比较无语)。

  淘宝ip地址库,提供api   

接口说明

  1. 请求接口(get):
      http://ip.taobao.com/service/getipinfo.php?ip=[ip地址字串]
  2. 响应信息:
      (json格式的)国家 、省(自治区或直辖市)、市(县)、运营商
  3. 返回数据格式:
      {"code":0,"data":{"ip":"210.75.225.254","country":"\u4e2d\u56fd","area":"\u534e\u5317",
        "region":"\u5317\u4eac\u5e02","city":"\u5317\u4eac\u5e02","county":"","isp":"\u7535\u4fe1",
        "country_id":"86","area_id":"100000","region_id":"110000","city_id":"110000",
        "county_id":"-1","isp_id":"100017"}}

      其中code的值的含义为,0:成功,1:失败。

1 :ip转换

  准备好工具,后面就好弄啦, iphelper提供了各种,ip<->byte[]<->long 转换

复制代码 代码如下:

public class iphelper
        {
        /// <summary>
        /// ip转成long
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public static long ip2long(string ip)
        {
            byte[] bytes = ip2bytes(ip);
            return bytes2long(bytes);
        }
        /// <summary>
        /// long转成ip
        /// </summary>
        /// <param name="iplong"></param>
        /// <returns></returns>
        public static string long2ip(long iplong)
        {
            byte[] bytes = long2bytes(iplong);
            return bytes2ip(bytes);
        }
        /// <summary>
        /// long转成byte[]
        /// </summary>
        /// <param name="ipvalue"></param>
        /// <returns></returns>
        public static byte[] long2bytes(long ipvalue)
        {
            byte[] b = new byte[4];
            for (int i = 0; i < 4; i++)
            {
                b[3 - i] = (byte)(ipvalue >> 8 * i & 255);
            }
            return b;
        }
        /// <summary>
        /// byte[]转成long
        /// </summary>
        /// <param name="bt"></param>
        /// <returns></returns>
        public static long bytes2long(byte[] bt)
        {
            int x = 3;
            long o = 0;
            foreach (byte f in bt)
            {
                o += (long)f << 8 * x--;
            }
            return o;
        }
        /// <summary>
        /// ip转成byte[]
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public static byte[] ip2bytes(string ip)
        {
            string[] sp = ip.split('.');
            return new byte[] { convert.tobyte(sp[0]), convert.tobyte(sp[1]), convert.tobyte(sp[2]), convert.tobyte(sp[3]) };
        }
        /// <summary>
        /// byte[]转成ip
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static string bytes2ip(byte[] bytes)
        {
            return string.format("{0}.{1}.{2}.{3}"
                                   , bytes[0]
                                   , bytes[1]
                                   , bytes[2]
                                   , bytes[3]);
        }
    }

2 :多线程疯狂获取ip

复制代码 代码如下:

/// <summary>
        /// 描述:开始采集
        /// </summary>
        private void stratcollect()
        {
            foreach (thread thread in threadlist)
            {
                thread.start();
            }
        }
        /// <summary>
        /// 描述:获取要采集的ip long
        /// </summary>
        private long getcurrentip()
        {
            long curip = system.threading.interlocked.increment(ref currentcollectip);
            return curip;
        }
        /// <summary>
        /// 线程中采集的方法
        /// </summary>
        private void gettaobaodata()
        {
            long currentiplong = getcurrentip();
            while (currentiplong <= endip)
            {
                try
                {
                    capturetaobaoipdata(currentiplong);
                }
                catch (exception ex)
                {
                     textlog.setstring(currentiplong + ex.message);
                }
                currentiplong = getcurrentip();
            }
        }
        /// <summary>
        /// 描述:线程中采集并得到ip
        /// </summary>
        private void capturetaobaoipdata(long currentiplong)
        {
            string ip = iphelper.long2ip(currentiplong);
            string url = string.format(urlfomat, ip);
            string js =httphelper. httprequest(url, encoding.utf8);
            taobaoipdata m = newtonsoft.json.jsonconvert.deserializeobject<taobaojsondata>(js).data;
            m.iplong = currentiplong;
            //更新界面
            this.invoke(new action<taobaoipdata>(v =>
            {
                taobaoipdatalist.add(v);
                this.dgv.datasource = taobaoipdatalist;
            }), m);

        }

3: http请求的json结果,并反序列化成对象

       http请求这个相当简单。网上一大把,这里主要说一下json序列化,在这里本人建议采用newtonsoft.json.dll 下载地址: http://json.codeplex.com/ 性能和兼容性达到最好

http请求

复制代码 代码如下:

public class httphelper
    {
        public static string httprequest(string url, encoding encoding)
        {
            try
            {
                httpwebrequest request = (httpwebrequest)webrequest.create(url);
                request.timeout = 6 * 1000;
                request.method = "get";
                //得到处理结果                             
                httpwebresponse response = (httpwebresponse)request.getresponse();
                stream myresponsestream = response.getresponsestream();
                streamreader mystreamreader = new streamreader(myresponsestream, encoding);
                string result = mystreamreader.readtoend();
                return result;
            }
            catch (exception ex)
            {
                throw ex;
            }

        }

    }

json序列化

复制代码 代码如下:

taobaoipdata m = newtonsoft.json.jsonconvert.deserializeobject<taobaojsondata>(js).data;

序列号对象taobaoipdata
复制代码 代码如下:

/// <summary>
    /// 淘宝数据
    /// </summary>
    public partial class taobaoipdata
    {
        private long _iplong;
        /// <summary>
        /// ip 长整形
        /// </summary>
        public long iplong
        {
            get { return _iplong; }
            set { _iplong = value; }
        }

        private string _ip;
        /// <summary>
        /// ip地址
        /// </summary>
        public string ip
        {
            get { return _ip; }
            set { _ip = value; }
        }

        private string _country;
        /// <summary>
        /// 国家
        /// </summary>
        public string country
        {
            get { return _country; }
            set { _country = value; }
        }

        private string _country_id;
        /// <summary>
        /// 国家编号
        /// </summary>
        public string country_id
        {
            get { return _country_id; }
            set { _country_id = value; }
        }

        private string _area;
        /// <summary>
        /// 地区
        /// </summary>
        public string area
        {
            get { return _area; }
            set { _area = value; }
        }

        private string _area_id;
        /// <summary>
        /// 地区编号
        /// </summary>
        public string area_id
        {
            get { return _area_id; }
            set { _area_id = value; }
        }

        private string _region;
        /// <summary>
        /// 区域
        /// </summary>
        public string region
        {
            get { return _region; }
            set { _region = value; }
        }

        private string _region_id;
        /// <summary>
        /// 区域编号
        /// </summary>
        public string region_id
        {
            get { return _region_id; }
            set { _region_id = value; }
        }

        private string _city;
        /// <summary>
        ///城市
        /// </summary>
        public string city
        {
            get { return _city; }
            set { _city = value; }
        }

        private string _city_id;
        /// <summary>
        /// 城市编号
        /// </summary>
        public string city_id
        {
            get { return _city_id; }
            set { _city_id = value; }
        }

        private string _county;
        /// <summary>
        /// 县
        /// </summary>
        public string county
        {
            get { return _county; }
            set { _county = value; }
        }

        private string _county_id;
        /// <summary>
        /// 县编号
        /// </summary>
        public string county_id
        {
            get { return _county_id; }
            set { _county_id = value; }
        }

        private string _isp;
        /// <summary>
        /// 供应商
        /// </summary>
        public string isp
        {
            get { return _isp; }
            set { _isp = value; }
        }

        private string _isp_id;
        /// <summary>
        /// 供应商id
        /// </summary>
        public string isp_id
        {
            get { return _isp_id; }
            set { _isp_id = value; }
        }

       

    }
    /// <summary>
    /// 淘宝api 返回的json数据
    /// </summary>
    public partial class taobaojsondata
    {
        public int code { get; set; }
        public taobaoipdata data { get; set; }
    }

序列号对象taobaoipdata

4:插入到数据库中。。。剩下的自己随便搞啦

源码下载:淘宝ip获取器.rar