淘寶IP地址庫采集器c#代碼
采集器概貌,如下:

最近做一個項目,功能類似于CNZZ站長統(tǒng)計功能,要求顯示Ip所在的省份市區(qū)/提供商等信息。網(wǎng)上的Ip純真數(shù)據(jù)庫,下載下來一看,發(fā)現(xiàn)沒提供商內(nèi)容,省市區(qū)都很少,居然有XXX網(wǎng)吧,哥瞬間倒了。沒標(biāo)準(zhǔn)化、并且雜亂、還不連續(xù)的IP段、總體說來沒達(dá)到要求。
在百度上找啊找,找到淘寶Ip地址庫,官方介紹的準(zhǔn)確率高,數(shù)據(jù)質(zhì)量有保障,提供國家、省、市、縣、運營商全方位信息,信息維度廣,格式規(guī)范,但是限制每秒10次的訪問(這個比較無語)。
接口說明
1. 請求接口(GET):
http://ip.taobao.com/service/getIpInfo.php?ip=[ip地址字串]
2. 響應(yīng)信息:
(json格式的)國家 、?。ㄗ灾螀^(qū)或直轄市)、市(縣)、運營商
3. 返回數(shù)據(jù)格式:
{"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轉(zhuǎn)換
準(zhǔn)備好工具,后面就好弄啦, IPHelper提供了各種,IP<->byte[]<->Long 轉(zhuǎn)換
public class IPHelper
{
/// <summary>
/// ip轉(zhuǎn)成long
/// </summary>
/// <param name="ip"></param>
/// <returns></returns>
public static long Ip2Long(string ip)
{
byte[] bytes = Ip2Bytes(ip);
return Bytes2Long(bytes);
}
/// <summary>
/// long轉(zhuǎn)成ip
/// </summary>
/// <param name="ipLong"></param>
/// <returns></returns>
public static string Long2Ip(long ipLong)
{
byte[] bytes = Long2Bytes(ipLong);
return Bytes2Ip(bytes);
}
/// <summary>
/// long轉(zhuǎn)成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[]轉(zhuǎn)成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轉(zhuǎn)成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[]轉(zhuǎn)成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結(jié)果,并反序列化成對象
http請求這個相當(dāng)簡單。網(wǎng)上一大把,這里主要說一下json序列化,在這里本人建議采用Newtonsoft.Json.dll 下載地址: http://json.codeplex.com/ 性能和兼容性達(dá)到最好
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";
//得到處理結(jié)果
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>
/// 淘寶數(shù)據(jù)
/// </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>
/// 地區(qū)
/// </summary>
public string area
{
get { return _area; }
set { _area = value; }
}
private string _area_id;
/// <summary>
/// 地區(qū)編號
/// </summary>
public string area_id
{
get { return _area_id; }
set { _area_id = value; }
}
private string _region;
/// <summary>
/// 區(qū)域
/// </summary>
public string region
{
get { return _region; }
set { _region = value; }
}
private string _region_id;
/// <summary>
/// 區(qū)域編號
/// </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>
/// 供應(yīng)商
/// </summary>
public string isp
{
get { return _isp; }
set { _isp = value; }
}
private string _isp_id;
/// <summary>
/// 供應(yīng)商ID
/// </summary>
public string isp_id
{
get { return _isp_id; }
set { _isp_id = value; }
}
}
/// <summary>
/// 淘寶api 返回的json數(shù)據(jù)
/// </summary>
public partial class TaobaoJsonData
{
public int code { get; set; }
public taobaoIPdata data { get; set; }
}
序列號對象taobaoIPdata
相關(guān)文章
Unity3D利用DoTween實現(xiàn)卡牌翻轉(zhuǎn)效果
這篇文章主要為大家詳細(xì)介紹了Unity3D利用DoTween實現(xiàn)卡牌翻轉(zhuǎn)效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-02-02
Windows 8 Metro用C#連接SQLite及創(chuàng)建數(shù)據(jù)庫,數(shù)據(jù)表的增刪改查的實現(xiàn)
本篇文章小編為大家介紹,Windows 8 Metro用C#連接SQLite及創(chuàng)建數(shù)據(jù)庫,數(shù)據(jù)表的增刪改查的實現(xiàn)。需要的朋友參考下2013-04-04
c# asp .net 動態(tài)創(chuàng)建sql數(shù)據(jù)庫表的方法
c# asp .net 動態(tài)創(chuàng)建sql數(shù)據(jù)庫表的方法,需要的朋友可以參考一下2013-04-04

