当前位置: 移动技术网 > IT编程>开发语言>.net > ASP.NET实现根据IP获取省市地址的方法

ASP.NET实现根据IP获取省市地址的方法

2017年12月12日  | 移动技术网IT编程  | 我要评论

夏骄阳老婆,小说卡徒,掌声落下

本文实例讲述了asp.net实现根据ip获取省市地址的方法,分享给大家供大家参考。具体实现方法如下:

1、在网站的跟路径下面添加qqwry.dat(点击此处本站下载)文件,这个文件是ip数据库文件

2、添加以下一个类 ipscanner

复制代码 代码如下:
public class ipscanner
{
        //私有成员#region 私有成员
        private string datapath;
        private string ip;
        private string country;
        private string local;

        private long firststartip = 0;
        private long laststartip = 0;
        private filestream objfs = null;
        private long startip = 0;
        private long endip = 0;
        private int countryflag = 0;
        private long endipoff = 0;
        private string errmsg = null;


        //构造函数#region 构造函数
        public ipscanner()
        {
            this.datapath = system.web.httpcontext.current.server.mappath("/ipdata/qqwry.dat");
        }

        //公共属性#region 公共属性
        public string datapath
        {
            set { datapath = value; }
        }
        public string ip
        {
            set { ip = value; }
        }
        public string country
        {
            get { return country; }
        }
        public string local
        {
            get { return local; }
        }
        public string errmsg
        {
            get { return errmsg; }
        }

        //搜索匹配数据#region 搜索匹配数据
        private int qqwry()
        {
            string pattern = @"(((\\d{1,2})|(1\\d{2})|(2[0-4]\\d)|(25[0-5]))\\.){3}((\\d{1,2})|(1\\d{2})|(2[0-4]\\d)|(25[0-5]))";
            regex objre = new regex(pattern);
            match objma = objre.match(ip);
            if (!objma.success)
            {
                this.errmsg = "ip格式错误";
                return 4;
            }

            long ip_int = this.iptoint(ip);
            int nret = 0;
            if (ip_int >= iptoint("127.0.0.0") && ip_int <= iptoint("127.255.255.255"))
            {
                this.country = "本机内部环回地址";
                this.local = "";
                nret = 1;
            }
            else if ((ip_int >= iptoint("0.0.0.0") && ip_int <= iptoint("2.255.255.255")) || (ip_int >= iptoint("64.0.0.0") && ip_int <= iptoint("126.255.255.255")) || (ip_int >= iptoint("58.0.0.0") && ip_int <= iptoint("60.255.255.255")))
            {
                this.country = "网络保留地址";
                this.local = "";
                nret = 1;
            }
            objfs = new filestream(this.datapath, filemode.open, fileaccess.read);
            try
            {
                objfs.position = 0;
                byte[] buff = new byte[8];
                objfs.read(buff, 0, 8);
                firststartip = buff[0] + buff[1] * 256 + buff[2] * 256 * 256 + buff[3] * 256 * 256 * 256;
                laststartip = buff[4] * 1 + buff[5] * 256 + buff[6] * 256 * 256 + buff[7] * 256 * 256 * 256;
                long recordcount = convert.toint64((laststartip - firststartip) / 7.0);
                if (recordcount <= 1)
                {
                    country = "filedataerror";
                    objfs.close();
                    return 2;
                }
                long range = recordcount;
                long rangb = 0;
                long recno = 0;
                while (rangb < range - 1)
                {
                    recno = (range + rangb) /2;
                    this.getstartip(recno);
                    if (ip_int ==this.startip)
                    {
                        rangb = recno;
                        break;
                    }if (ip_int >this.startip)
                        rangb = recno;
                    else
                        range = recno;
                }this.getstartip(rangb);
                this.getendip();
                if (this.startip <= ip_int &&this.endip >= ip_int)
                {
                    this.getcountry();
                    this.local =this.local.replace("(我们一定要解放台湾!!!)", "");
                }else{
                    nret =3;
                    this.country ="未知";
                    this.local ="";
                }
                objfs.close();
                return nret;
            }catch{
                return1;
            }
        }// ip地址转换成int数据#region ip地址转换成int数据 privatelong iptoint(string ip)
        {
            char[] dot =newchar[] { '.' };
            string[] iparr = ip.split(dot);
            if (iparr.length ==3)
                ip = ip +".0";
            iparr = ip.split(dot);

            long ip_int =0;
            long p1 =long.parse(iparr[0]) *256*256*256;
            long p2 =long.parse(iparr[1]) *256*256;
            long p3 =long.parse(iparr[2]) *256;
            long p4 =long.parse(iparr[3]);
            ip_int = p1 + p2 + p3 + p4;
            return ip_int;
        }//获取起始ip范围#region 获取起始ip范围 privatelong getstartip(long recno)
        {
            long offset = firststartip + recno *7;
            //objfs.seek(offset,seekorigin.begin);             objfs.position = offset;
            byte[] buff =new byte[7];
            objfs.read(buff, 0, 7);

            endipoff = convert.toint64(buff[4].tostring()) + convert.toint64(buff[5].tostring()) *256+ convert.toint64(buff[6].tostring()) *256*256;
            startip = convert.toint64(buff[0].tostring()) + convert.toint64(buff[1].tostring()) *256+ convert.toint64(buff[2].tostring()) *256*256+ convert.toint64(buff[3].tostring()) *256*256*256;
            return startip;
        }// 获取结束ip#region 获取结束ip privatelong getendip()
        {
            //objfs.seek(endipoff,seekorigin.begin);             objfs.position = endipoff;
            byte[] buff =new byte[5];
            objfs.read(buff, 0, 5);
            this.endip = convert.toint64(buff[0].tostring()) + convert.toint64(buff[1].tostring()) *256+ convert.toint64(buff[2].tostring()) *256*256+ convert.toint64(buff[3].tostring()) *256*256*256;
            this.countryflag = buff[4];
            returnthis.endip;
        }//获取国家/区域偏移量#region 获取国家/区域偏移量 privatestring getcountry()
        {
            switch (this.countryflag)
            {
                case1:
                case2:
                    this.country = getflagstr(this.endipoff +4);
                    this.local = (1==this.countryflag) ?"" : this.getflagstr(this.endipoff +8);
                    break;
                default:
                    this.country =this.getflagstr(this.endipoff +4);
                    this.local =this.getflagstr(objfs.position);
                    break;
            }return"";
        }//获取国家/区域字符串#region 获取国家/区域字符串 privatestring getflagstr(long offset)
        {
            int flag =0;
            byte[] buff =new byte[3];
            while (1==1)
            {
                objfs.position = offset;
                flag = objfs.readbyte();
                if (flag ==1|| flag ==2)
                {
                    objfs.read(buff, 0, 3);
                    if (flag ==2)
                    {
                        this.countryflag =2;
                        this.endipoff = offset -4;
                    }
                    offset = convert.toint64(buff[0].tostring()) + convert.toint64(buff[1].tostring()) *256+ convert.toint64(buff[2].tostring()) *256*256;
                }else{
                    break;
                }
            }if (offset <12)
                return"";
            objfs.position = offset;
            return getstr();
        }//getstr#region getstr privatestring getstr()
        {
            byte lowc =0;
            byte upc =0;
            string str ="";
            byte[] buff =newbyte[2];
            while (1==1)
            {
                lowc = (byte)objfs.readbyte();
                if (lowc ==0)
                    break;
                if (lowc >127)
                {
                    upc = (byte)objfs.readbyte();
                    buff[0] = lowc;
                    buff[1] = upc;
                    system.text.encoding enc = system.text.encoding.getencoding("gb2312");
                    str += enc.getstring(buff);
                }else{
                    str += (char)lowc;
                }
            }return str;
        }publicstring iplocation(string ip)
        {
            this.ip = ip;
            this.qqwry();
            if (this.country.trim().contains(this.local.trim()))
                returnthis.country.trim();
            returnthis.country.trim() +this.local.trim();
        }
}

3、asp.net根据ip获取省市地址的调用

复制代码 代码如下:
string ip="123.81.176.99";
ipscanner ipscaner = new ipscanner();
ipscaner.iplocation(ip); 
string stripaddress=ipscaner.country;

希望本文所述对大家的c#程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网