刚刚找到的IP地址对应地区数据库

系统 1538 0

    项目原因,网上下载了 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;
        }
    }

}

  
  相关文件下载: 点击下载

刚刚找到的IP地址对应地区数据库


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论