刚刚找到的IP地址对应地区数据库
项目原因,网上下载了 We7 CMS安装版,(想下载源码竟然发现要加入什么什么源码小组,还要一年交¥100,晕,这叫什么GP开源啊!!!),下载下来看了下app_data目录,惊奇发现有个ip.db,呵呵,我猜可能是IP地址数据库了,而且是sqlite的,打开一看:

嘿嘿 ,果然是IP地址数据库,而且都转成长IP格式的了,自己写了个aspx页面来获取IP的:
http://5mdn.com/getip.aspx

嘿嘿 ,果然是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;
}
}
}
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;
}
}
}
相关文件下载:点击下载
撸码:复制、粘贴,拿起键盘就是“干”!!!
分类:
C#
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2009-06-15 求一整数的所有拆分方式