日期:2014-05-16 浏览次数:20537 次
?? ?项目原因,网上下载了?We7 CMS安装版,(想下载源码竟然发现要加入什么什么源码小组,还要一年交¥100,晕,这叫什么GP开源啊!!!),下载下来看了下app_data目录,惊奇发现有个ip.db,呵呵,我猜可能是IP地址数据库了,而且是sqlite的,打开一看:
嘿嘿 ,果然是IP地址数据库,而且都转成长IP格式的了,自己写了个aspx页面来获取IP的:
http://5mdn.com/getip.aspx
给网上的人测试发现其实这个IP数据库也不是很完全的,算了,不管了,先记下来吧,下面是aspx.cs的代码:
?
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Text.RegularExpressions; using System.Data; using System.Data.SQLite; public partial class test : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string ip = Request.UserHostAddress; string longip = IpToLong(ip).ToString(); litmes.Text = "<h3>您的IP:" + ip + "</h3>"; litmes.Text += "<h3>转换后的IP:" + longip + "</h3>"; litmes.Text += "<h4>从IP数据库中取出的记录:</h4>"; SQLiteConnection conn = null; SQLiteCommand cmd = null; SQLiteDataReader sdr = null; string connStr = "Data Source=" + HttpContext.Current.Server.MapPath("~/App_Data/ip.db"); conn = new SQLiteConnection(connStr); conn.Open(); string sql = "SELECT * FROM iptocity where " + longip + " between ip_start and ip_end"; cmd = new SQLiteCommand(sql, conn); DataTable dt = new DataTable(); using (sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)) { dt.Load(sdr); } GridView1.DataSource = dt; GridView1.DataBind(); } // 把IP地址转成长数字, // 算法:128.125.1.24 → (128*256*256*256) + (125*256*256) + (1*256) +24 public static ulong IpToLong(string ip) { try { string[] cip = ip.Trim().Split('.'); string[] aip = new string[4]; cip.CopyTo(aip, 0); if (cip.Length < 3) { for (int i = 3; i > cip.Length; i--) { aip[i] = "0"; } } uint[] iip = new uint[4]; Regex reg = new Regex(@"\d+"); for (int x = 0; x < aip.Length; x++) { if (reg.IsMatch(aip[x])) iip[x] = Convert.ToUInt32(aip[x]); else iip[x] = 0; } ulong uip = Convert.ToUInt64(256 * 256 * 256 * iip[0] + 256 * 256 * iip[1] + 256 * iip[2] + iip[3]); return uip; } catch (Exception ess) { throw ess; } } }?相关文件下载:点击下载