采集工具概貌,如下:
最近做一個項目,功能類似于CNZZ站長統計功能,要求顯示Ip所在的省份市區/提供商等信息。網上的Ip純真數據庫,下載下來一看,發現沒提供商內容,省市區都很少,居然有XXX網吧,哥瞬間倒了。沒標準化、并且雜亂、還不連續的IP段、總體說來沒達到要求。
在百度上找啊找,找到淘寶Ip地址庫,官方介紹的相當誘人,準確率高,數據質量有保障,提供國家、省、市、縣、運營商全方位信息,信息維度廣,格式規范,但是限制每秒10次的訪問(這個比較無語)。
淘寶IP地址庫,提供API http://ip.taobao.com/
接口說明
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 轉換
1 public class IPHelper 2 { 3 ///4 /// ip轉成long 5 /// 6 /// 7 /// 8 public static long Ip2Long(string ip) 9 { 10 byte[] bytes = Ip2Bytes(ip); 11 return Bytes2Long(bytes); 12 } 13 /// 14 /// long轉成ip 15 /// 16 /// 17 /// 18 public static string Long2Ip(long ipLong) 19 { 20 byte[] bytes = Long2Bytes(ipLong); 21 return Bytes2Ip(bytes); 22 } 23 /// 24 /// long轉成byte[] 25 /// 26 /// 27 /// 28 public static byte[] Long2Bytes(long ipvalue) 29 { 30 byte[] b = new byte[4]; 31 for (int i = 0; i < 4; i++) 32 { 33 b[3 - i] = (byte)(ipvalue >> 8 * i & 255); 34 } 35 return b; 36 } 37 /// 38 /// byte[]轉成long 39 /// 40 /// 41 /// 42 public static long Bytes2Long(byte[] bt) 43 { 44 int x = 3; 45 long o = 0; 46 foreach (byte f in bt) 47 { 48 o += (long)f << 8 * x--; 49 } 50 return o; 51 } 52 /// 53 /// ip轉成byte[] 54 /// 55 /// 56 /// 57 public static byte[] Ip2Bytes(string ip) 58 { 59 string[] sp = ip.Split('.'); 60 return new byte[] { Convert.ToByte(sp[0]), Convert.ToByte(sp[1]), Convert.ToByte(sp[2]), Convert.ToByte(sp[3]) }; 61 } 62 /// 63 /// byte[]轉成ip 64 /// 65 /// 66 /// 67 public static string Bytes2Ip(byte[] bytes) 68 { 69 return string.Format("{0}.{1}.{2}.{3}" 70 , bytes[0] 71 , bytes[1] 72 , bytes[2] 73 , bytes[3]); 74 } 75 }
2 :多線程瘋狂獲取IP代碼
1 ///2 /// 描述:開始采集 3 /// 4 private void StratCollect() 5 { 6 foreach (Thread thread in ThreadList) 7 { 8 thread.Start(); 9 } 10 } 11 /// 12 /// 描述:獲取要采集的ip long 13 /// 14 private long GetCurrentIp() 15 { 16 long curip = System.Threading.Interlocked.Increment(ref CurrentCollectIP); 17 return curip; 18 } 19 /// 20 /// 線程中采集的方法 21 /// 22 private void GetTaobaoData() 23 { 24 long currentipLong = GetCurrentIp(); 25 while (currentipLong <= EndIP) 26 { 27 try 28 { 29 CaptureTaobaoIPData(currentipLong); 30 } 31 catch (Exception ex) 32 { 33 TextLog.SetString(currentipLong + ex.Message); 34 } 35 currentipLong = GetCurrentIp(); 36 } 37 } 38 /// 39 /// 描述:線程中采集并得到IP 40 /// 41 private void CaptureTaobaoIPData(long currentipLong) 42 { 43 string ip = IPHelper.Long2Ip(currentipLong); 44 string url = string.Format(UrlFomat, ip); 45 string js =HttpHelper. HttpRequest(url, Encoding.UTF8); 46 taobaoIPdata m = Newtonsoft.Json.JsonConvert.DeserializeObject (js).data; 47 m.ipLong = currentipLong; 48 //更新界面 49 this.Invoke(new Action (v => 50 { 51 taobaoIPdataList.Add(v); 52 this.dgv.DataSource = taobaoIPdataList; 53 }), m); 54 55 }
3: Http請求的Json結果,并反序列化成對象代碼
http請求這個相當簡單。網上一大把,這里主要說一下json序列化,在這里本人建議采用Newtonsoft.Json.dll 下載地址: http://json.codeplex.com/ 性能和兼容性達到最好
1 public class HttpHelper 2 { 3 public static string HttpRequest(string url, Encoding encoding) 4 { 5 try 6 { 7 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 8 request.Timeout = 6 * 1000; 9 request.Method = "GET"; 10 //得到處理結果 11 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 12 Stream myResponseStream = response.GetResponseStream(); 13 StreamReader myStreamReader = new StreamReader(myResponseStream, encoding); 14 string result = myStreamReader.ReadToEnd(); 15 return result; 16 } 17 catch (Exception ex) 18 { 19 throw ex; 20 } 21 22 } 23 24 }
1 taobaoIPdata m = Newtonsoft.Json.JsonConvert.DeserializeObject(js).data;
1 ///2 /// 淘寶數據 3 /// 4 public partial class taobaoIPdata 5 { 6 private long _ipLong; 7 /// 8 /// IP 長整形 9 /// 10 public long ipLong 11 { 12 get { return _ipLong; } 13 set { _ipLong = value; } 14 } 15 16 private string _ip; 17 /// 18 /// IP地址 19 /// 20 public string ip 21 { 22 get { return _ip; } 23 set { _ip = value; } 24 } 25 26 private string _country; 27 /// 28 /// 國家 29 /// 30 public string country 31 { 32 get { return _country; } 33 set { _country = value; } 34 } 35 36 private string _country_id; 37 /// 38 /// 國家編號 39 /// 40 public string country_id 41 { 42 get { return _country_id; } 43 set { _country_id = value; } 44 } 45 46 private string _area; 47 /// 48 /// 地區 49 /// 50 public string area 51 { 52 get { return _area; } 53 set { _area = value; } 54 } 55 56 private string _area_id; 57 /// 58 /// 地區編號 59 /// 60 public string area_id 61 { 62 get { return _area_id; } 63 set { _area_id = value; } 64 } 65 66 private string _region; 67 /// 68 /// 區域 69 /// 70 public string region 71 { 72 get { return _region; } 73 set { _region = value; } 74 } 75 76 private string _region_id; 77 /// 78 /// 區域編號 79 /// 80 public string region_id 81 { 82 get { return _region_id; } 83 set { _region_id = value; } 84 } 85 86 private string _city; 87 /// 88 ///城市 89 /// 90 public string city 91 { 92 get { return _city; } 93 set { _city = value; } 94 } 95 96 private string _city_id; 97 /// 98 /// 城市編號 99 /// 100 public string city_id 101 { 102 get { return _city_id; } 103 set { _city_id = value; } 104 } 105 106 private string _county; 107 /// 108 /// 縣 109 /// 110 public string county 111 { 112 get { return _county; } 113 set { _county = value; } 114 } 115 116 private string _county_id; 117 /// 118 /// 縣編號 119 /// 120 public string county_id 121 { 122 get { return _county_id; } 123 set { _county_id = value; } 124 } 125 126 private string _isp; 127 /// 128 /// 供應商 129 /// 130 public string isp 131 { 132 get { return _isp; } 133 set { _isp = value; } 134 } 135 136 private string _isp_id; 137 /// 138 /// 供應商ID 139 /// 140 public string isp_id 141 { 142 get { return _isp_id; } 143 set { _isp_id = value; } 144 } 145 146 147 148 } 149 /// 150 /// 淘寶api 返回的json數據 151 /// 152 public partial class TaobaoJsonData 153 { 154 public int code { get; set; } 155 public taobaoIPdata data { get; set; } 156 }
4:插入到數據庫中。。。剩下的自己隨便搞啦
源碼下載:淘寶IP獲取器.rar
Copyright@ 2011-2016 版權所有:大連千億科技有限公司 遼ICP備11013762-3號 google網站地圖 百度網站地圖 網站地圖
公司地址:大連市沙河口區中山路692號辰熙星海國際2317 客服電話:0411-39943997 QQ:2088827823 37482752
法律聲明:未經許可,任何模仿本站模板、轉載本站內容等行為者,本站保留追究其法律責任的權利! 隱私權政策聲明