![](http://niunan.org/upload/20100615153504.jpg)
嘿嘿 ,果然是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;
}
}
}