1、在网站的跟路径下面添加 QQWry.dat 文件,这个文件是IP数据库文件

2、添加以下一个类 IPScanner

 
 
C# 代码   复制
ASP.NET根据IP获取省市地址-LMLPHP
ASP.NET根据IP获取省市地址-LMLPHP public class IPScanner
ASP.NET根据IP获取省市地址-LMLPHP {
ASP.NET根据IP获取省市地址-LMLPHP //私有成员#region 私有成员
ASP.NET根据IP获取省市地址-LMLPHP private string dataPath;
ASP.NET根据IP获取省市地址-LMLPHP private string ip;
ASP.NET根据IP获取省市地址-LMLPHP private string country;
ASP.NET根据IP获取省市地址-LMLPHP private string local;
ASP.NET根据IP获取省市地址-LMLPHP
ASP.NET根据IP获取省市地址-LMLPHP private long firstStartIp = 0;
ASP.NET根据IP获取省市地址-LMLPHP private long lastStartIp = 0;
ASP.NET根据IP获取省市地址-LMLPHP private FileStream objfs = null;
ASP.NET根据IP获取省市地址-LMLPHP private long startIp = 0;
ASP.NET根据IP获取省市地址-LMLPHP private long endIp = 0;
ASP.NET根据IP获取省市地址-LMLPHP private int countryFlag = 0;
ASP.NET根据IP获取省市地址-LMLPHP private long endIpOff = 0;
ASP.NET根据IP获取省市地址-LMLPHP private string errMsg = null;
ASP.NET根据IP获取省市地址-LMLPHP
ASP.NET根据IP获取省市地址-LMLPHP
ASP.NET根据IP获取省市地址-LMLPHP //构造函数#region 构造函数
ASP.NET根据IP获取省市地址-LMLPHP public IPScanner()
ASP.NET根据IP获取省市地址-LMLPHP {
ASP.NET根据IP获取省市地址-LMLPHP this.dataPath = System.Web.HttpContext.Current.Server.MapPath("/ipdata/QQWry.dat");
ASP.NET根据IP获取省市地址-LMLPHP }
ASP.NET根据IP获取省市地址-LMLPHP
ASP.NET根据IP获取省市地址-LMLPHP //公共属性#region 公共属性
ASP.NET根据IP获取省市地址-LMLPHP public string DataPath
ASP.NET根据IP获取省市地址-LMLPHP {
ASP.NET根据IP获取省市地址-LMLPHP set { dataPath = value; }
ASP.NET根据IP获取省市地址-LMLPHP }
ASP.NET根据IP获取省市地址-LMLPHP public string IP
ASP.NET根据IP获取省市地址-LMLPHP {
ASP.NET根据IP获取省市地址-LMLPHP set { ip = value; }
ASP.NET根据IP获取省市地址-LMLPHP }
ASP.NET根据IP获取省市地址-LMLPHP public string Country
ASP.NET根据IP获取省市地址-LMLPHP {
ASP.NET根据IP获取省市地址-LMLPHP get { return country; }
ASP.NET根据IP获取省市地址-LMLPHP }
ASP.NET根据IP获取省市地址-LMLPHP public string Local
ASP.NET根据IP获取省市地址-LMLPHP {
ASP.NET根据IP获取省市地址-LMLPHP get { return local; }
ASP.NET根据IP获取省市地址-LMLPHP }
ASP.NET根据IP获取省市地址-LMLPHP public string ErrMsg
ASP.NET根据IP获取省市地址-LMLPHP {
ASP.NET根据IP获取省市地址-LMLPHP get { return errMsg; }
ASP.NET根据IP获取省市地址-LMLPHP }
ASP.NET根据IP获取省市地址-LMLPHP
ASP.NET根据IP获取省市地址-LMLPHP //搜索匹配数据#region 搜索匹配数据
ASP.NET根据IP获取省市地址-LMLPHP private int QQwry()
ASP.NET根据IP获取省市地址-LMLPHP {
ASP.NET根据IP获取省市地址-LMLPHP 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]))";
ASP.NET根据IP获取省市地址-LMLPHP Regex objRe = new Regex(pattern);
ASP.NET根据IP获取省市地址-LMLPHP Match objMa = objRe.Match(ip);
ASP.NET根据IP获取省市地址-LMLPHP if (!objMa.Success)
ASP.NET根据IP获取省市地址-LMLPHP {
ASP.NET根据IP获取省市地址-LMLPHP this.errMsg = "IP格式错误";
ASP.NET根据IP获取省市地址-LMLPHP return 4;
ASP.NET根据IP获取省市地址-LMLPHP }
ASP.NET根据IP获取省市地址-LMLPHP
ASP.NET根据IP获取省市地址-LMLPHP long ip_Int = this.IpToInt(ip);
ASP.NET根据IP获取省市地址-LMLPHP int nRet = 0;
ASP.NET根据IP获取省市地址-LMLPHP if (ip_Int >= IpToInt("127.0.0.0") && ip_Int <= IpToInt("127.255.255.255"))
ASP.NET根据IP获取省市地址-LMLPHP {
ASP.NET根据IP获取省市地址-LMLPHP this.country = "本机内部环回地址";
ASP.NET根据IP获取省市地址-LMLPHP this.local = "";
ASP.NET根据IP获取省市地址-LMLPHP nRet = 1;
ASP.NET根据IP获取省市地址-LMLPHP }
ASP.NET根据IP获取省市地址-LMLPHP 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")))
ASP.NET根据IP获取省市地址-LMLPHP {
ASP.NET根据IP获取省市地址-LMLPHP this.country = "网络保留地址";
ASP.NET根据IP获取省市地址-LMLPHP this.local = "";
ASP.NET根据IP获取省市地址-LMLPHP nRet = 1;
ASP.NET根据IP获取省市地址-LMLPHP }
ASP.NET根据IP获取省市地址-LMLPHP objfs = new FileStream(this.dataPath, FileMode.Open, FileAccess.Read);
ASP.NET根据IP获取省市地址-LMLPHP try
ASP.NET根据IP获取省市地址-LMLPHP {
ASP.NET根据IP获取省市地址-LMLPHP objfs.Position = 0;
ASP.NET根据IP获取省市地址-LMLPHP byte[] buff = new Byte[8];
ASP.NET根据IP获取省市地址-LMLPHP objfs.Read(buff, 0, 8);
ASP.NET根据IP获取省市地址-LMLPHP firstStartIp = buff[0] + buff[1] * 256 + buff[2] * 256 * 256 + buff[3] * 256 * 256 * 256;
ASP.NET根据IP获取省市地址-LMLPHP lastStartIp = buff[4] * 1 + buff[5] * 256 + buff[6] * 256 * 256 + buff[7] * 256 * 256 * 256;
ASP.NET根据IP获取省市地址-LMLPHP long recordCount = Convert.ToInt64((lastStartIp - firstStartIp) / 7.0);
ASP.NET根据IP获取省市地址-LMLPHP if (recordCount <= 1)
ASP.NET根据IP获取省市地址-LMLPHP {
ASP.NET根据IP获取省市地址-LMLPHP country = "FileDataError";
ASP.NET根据IP获取省市地址-LMLPHP objfs.Close();
ASP.NET根据IP获取省市地址-LMLPHP return 2;
ASP.NET根据IP获取省市地址-LMLPHP }
ASP.NET根据IP获取省市地址-LMLPHP long rangE = recordCount;
ASP.NET根据IP获取省市地址-LMLPHP long rangB = 0;
ASP.NET根据IP获取省市地址-LMLPHP long recNO = 0;
ASP.NET根据IP获取省市地址-LMLPHP while (rangB < rangE - 1)
ASP.NET根据IP获取省市地址-LMLPHP {
recNO ASP.NET根据IP获取省市地址-LMLPHP= (rangE + rangB) /2;
ASP.NET根据IP获取省市地址-LMLPHPthis.GetStartIp(recNO);
ASP.NET根据IP获取省市地址-LMLPHPif (ip_Int ==this.startIp)
ASP.NET根据IP获取省市地址-LMLPHP{
rangB ASP.NET根据IP获取省市地址-LMLPHP= recNO;
ASP.NET根据IP获取省市地址-LMLPHPbreak;
}ASP.NET根据IP获取省市地址-LMLPHPASP.NET根据IP获取省市地址-LMLPHPif (ip_Int >this.startIp)
rangB ASP.NET根据IP获取省市地址-LMLPHP= recNO;
ASP.NET根据IP获取省市地址-LMLPHPelse
rangE ASP.NET根据IP获取省市地址-LMLPHP= recNO;
}ASP.NET根据IP获取省市地址-LMLPHPASP.NET根据IP获取省市地址-LMLPHPthis.GetStartIp(rangB);
ASP.NET根据IP获取省市地址-LMLPHPthis.GetEndIp();
ASP.NET根据IP获取省市地址-LMLPHPif (this.startIp <= ip_Int &&this.endIp >= ip_Int)
ASP.NET根据IP获取省市地址-LMLPHP{
ASP.NET根据IP获取省市地址-LMLPHPthis.GetCountry();
ASP.NET根据IP获取省市地址-LMLPHPthis.local =this.local.Replace("(我们一定要解放台湾!!!)", "");
}ASP.NET根据IP获取省市地址-LMLPHPASP.NET根据IP获取省市地址-LMLPHPelseASP.NET根据IP获取省市地址-LMLPHP{
nRet ASP.NET根据IP获取省市地址-LMLPHP=3;
ASP.NET根据IP获取省市地址-LMLPHPthis.country ="未知";
ASP.NET根据IP获取省市地址-LMLPHPthis.local ="";
}ASP.NET根据IP获取省市地址-LMLPHP
objfs.Close();
ASP.NET根据IP获取省市地址-LMLPHPASP.NET根据IP获取省市地址-LMLPHPreturn nRet;
}ASP.NET根据IP获取省市地址-LMLPHPASP.NET根据IP获取省市地址-LMLPHPcatchASP.NET根据IP获取省市地址-LMLPHP{
ASP.NET根据IP获取省市地址-LMLPHPreturn1;
}ASP.NET根据IP获取省市地址-LMLPHP
}ASP.NET根据IP获取省市地址-LMLPHPASP.NET根据IP获取省市地址-LMLPHPASP.NET根据IP获取省市地址-LMLPHP// IP地址转换成Int数据#region IP地址转换成Int数据 ASP.NET根据IP获取省市地址-LMLPHPprivatelong IpToInt(string ip)
ASP.NET根据IP获取省市地址-LMLPHP{
ASP.NET根据IP获取省市地址-LMLPHPchar[] dot =newchar[] { '.' };
ASP.NET根据IP获取省市地址-LMLPHPstring[] ipArr = ip.Split(dot);
ASP.NET根据IP获取省市地址-LMLPHPif (ipArr.Length ==3)
ip ASP.NET根据IP获取省市地址-LMLPHP= ip +".0";
ipArr ASP.NET根据IP获取省市地址-LMLPHP= ip.Split(dot); ASP.NET根据IP获取省市地址-LMLPHPASP.NET根据IP获取省市地址-LMLPHPlong ip_Int =0;
ASP.NET根据IP获取省市地址-LMLPHPlong p1 =long.Parse(ipArr[0]) *256*256*256;
ASP.NET根据IP获取省市地址-LMLPHPlong p2 =long.Parse(ipArr[1]) *256*256;
ASP.NET根据IP获取省市地址-LMLPHPlong p3 =long.Parse(ipArr[2]) *256;
ASP.NET根据IP获取省市地址-LMLPHPlong p4 =long.Parse(ipArr[3]);
ip_Int ASP.NET根据IP获取省市地址-LMLPHP= p1 + p2 + p3 + p4;
ASP.NET根据IP获取省市地址-LMLPHPreturn ip_Int;
}ASP.NET根据IP获取省市地址-LMLPHPASP.NET根据IP获取省市地址-LMLPHPASP.NET根据IP获取省市地址-LMLPHP//获取起始IP范围#region 获取起始IP范围 ASP.NET根据IP获取省市地址-LMLPHPprivatelong GetStartIp(long recNO)
ASP.NET根据IP获取省市地址-LMLPHP{
ASP.NET根据IP获取省市地址-LMLPHPlong offSet = firstStartIp + recNO *7;
ASP.NET根据IP获取省市地址-LMLPHP//objfs.Seek(offSet,SeekOrigin.Begin); ASP.NET根据IP获取省市地址-LMLPHP objfs.Position = offSet;
ASP.NET根据IP获取省市地址-LMLPHPbyte[] buff =new Byte[7];
objfs.Read(buff, ASP.NET根据IP获取省市地址-LMLPHP0, 7); endIpOff ASP.NET根据IP获取省市地址-LMLPHPASP.NET根据IP获取省市地址-LMLPHP= Convert.ToInt64(buff[4].ToString()) + Convert.ToInt64(buff[5].ToString()) *256+ Convert.ToInt64(buff[6].ToString()) *256*256;
startIp ASP.NET根据IP获取省市地址-LMLPHP= 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;
ASP.NET根据IP获取省市地址-LMLPHPreturn startIp;
}ASP.NET根据IP获取省市地址-LMLPHPASP.NET根据IP获取省市地址-LMLPHP// 获取结束IP#region 获取结束IP ASP.NET根据IP获取省市地址-LMLPHPprivatelong GetEndIp()
ASP.NET根据IP获取省市地址-LMLPHP{
ASP.NET根据IP获取省市地址-LMLPHP//objfs.Seek(endIpOff,SeekOrigin.Begin); ASP.NET根据IP获取省市地址-LMLPHP objfs.Position = endIpOff;
ASP.NET根据IP获取省市地址-LMLPHPbyte[] buff =new Byte[5];
objfs.Read(buff, ASP.NET根据IP获取省市地址-LMLPHP0, 5);
ASP.NET根据IP获取省市地址-LMLPHPthis.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;
ASP.NET根据IP获取省市地址-LMLPHPthis.countryFlag = buff[4];
ASP.NET根据IP获取省市地址-LMLPHPreturnthis.endIp;
}ASP.NET根据IP获取省市地址-LMLPHPASP.NET根据IP获取省市地址-LMLPHP//获取国家/区域偏移量#region 获取国家/区域偏移量 ASP.NET根据IP获取省市地址-LMLPHPprivatestring GetCountry()
ASP.NET根据IP获取省市地址-LMLPHP{
ASP.NET根据IP获取省市地址-LMLPHPswitch (this.countryFlag)
ASP.NET根据IP获取省市地址-LMLPHP{
ASP.NET根据IP获取省市地址-LMLPHPcase1:
ASP.NET根据IP获取省市地址-LMLPHPcase2:
ASP.NET根据IP获取省市地址-LMLPHPthis.country = GetFlagStr(this.endIpOff +4);
ASP.NET根据IP获取省市地址-LMLPHPthis.local = (1==this.countryFlag) ?"" : this.GetFlagStr(this.endIpOff +8);
ASP.NET根据IP获取省市地址-LMLPHPbreak;
ASP.NET根据IP获取省市地址-LMLPHPdefault:
ASP.NET根据IP获取省市地址-LMLPHPthis.country =this.GetFlagStr(this.endIpOff +4);
ASP.NET根据IP获取省市地址-LMLPHPthis.local =this.GetFlagStr(objfs.Position);
ASP.NET根据IP获取省市地址-LMLPHPbreak;
}ASP.NET根据IP获取省市地址-LMLPHPASP.NET根据IP获取省市地址-LMLPHPreturn"";
}ASP.NET根据IP获取省市地址-LMLPHPASP.NET根据IP获取省市地址-LMLPHP//获取国家/区域字符串#region 获取国家/区域字符串 ASP.NET根据IP获取省市地址-LMLPHPprivatestring GetFlagStr(long offSet)
ASP.NET根据IP获取省市地址-LMLPHP{
ASP.NET根据IP获取省市地址-LMLPHPint flag =0;
ASP.NET根据IP获取省市地址-LMLPHPbyte[] buff =new Byte[3];
ASP.NET根据IP获取省市地址-LMLPHPwhile (1==1)
ASP.NET根据IP获取省市地址-LMLPHP{
objfs.Position ASP.NET根据IP获取省市地址-LMLPHP= offSet;
flag ASP.NET根据IP获取省市地址-LMLPHP= objfs.ReadByte();
ASP.NET根据IP获取省市地址-LMLPHPif (flag ==1|| flag ==2)
ASP.NET根据IP获取省市地址-LMLPHP{
objfs.Read(buff, ASP.NET根据IP获取省市地址-LMLPHP0, 3);
ASP.NET根据IP获取省市地址-LMLPHPif (flag ==2)
ASP.NET根据IP获取省市地址-LMLPHP{
ASP.NET根据IP获取省市地址-LMLPHPthis.countryFlag =2;
ASP.NET根据IP获取省市地址-LMLPHPthis.endIpOff = offSet -4;
}ASP.NET根据IP获取省市地址-LMLPHP
offSet ASP.NET根据IP获取省市地址-LMLPHP= Convert.ToInt64(buff[0].ToString()) + Convert.ToInt64(buff[1].ToString()) *256+ Convert.ToInt64(buff[2].ToString()) *256*256;
}ASP.NET根据IP获取省市地址-LMLPHPASP.NET根据IP获取省市地址-LMLPHPelseASP.NET根据IP获取省市地址-LMLPHP{
ASP.NET根据IP获取省市地址-LMLPHPbreak;
}ASP.NET根据IP获取省市地址-LMLPHP
}ASP.NET根据IP获取省市地址-LMLPHPASP.NET根据IP获取省市地址-LMLPHPif (offSet <12)
ASP.NET根据IP获取省市地址-LMLPHPreturn"";
objfs.Position ASP.NET根据IP获取省市地址-LMLPHP= offSet;
ASP.NET根据IP获取省市地址-LMLPHPreturn GetStr();
}ASP.NET根据IP获取省市地址-LMLPHPASP.NET根据IP获取省市地址-LMLPHP//GetStr#region GetStr ASP.NET根据IP获取省市地址-LMLPHPprivatestring GetStr()
ASP.NET根据IP获取省市地址-LMLPHP{
ASP.NET根据IP获取省市地址-LMLPHPbyte lowC =0;
ASP.NET根据IP获取省市地址-LMLPHPbyte upC =0;
ASP.NET根据IP获取省市地址-LMLPHPstring str ="";
ASP.NET根据IP获取省市地址-LMLPHPbyte[] buff =newbyte[2];
ASP.NET根据IP获取省市地址-LMLPHPwhile (1==1)
ASP.NET根据IP获取省市地址-LMLPHP{
lowC ASP.NET根据IP获取省市地址-LMLPHP= (Byte)objfs.ReadByte();
ASP.NET根据IP获取省市地址-LMLPHPif (lowC ==0)
ASP.NET根据IP获取省市地址-LMLPHPbreak;
ASP.NET根据IP获取省市地址-LMLPHPif (lowC >127)
ASP.NET根据IP获取省市地址-LMLPHP{
upC ASP.NET根据IP获取省市地址-LMLPHP= (byte)objfs.ReadByte();
buff[ASP.NET根据IP获取省市地址-LMLPHP0] = lowC;
buff[ASP.NET根据IP获取省市地址-LMLPHP1] = upC;
System.Text.Encoding enc ASP.NET根据IP获取省市地址-LMLPHP= System.Text.Encoding.GetEncoding("GB2312");
str ASP.NET根据IP获取省市地址-LMLPHP+= enc.GetString(buff);
}ASP.NET根据IP获取省市地址-LMLPHPASP.NET根据IP获取省市地址-LMLPHPelseASP.NET根据IP获取省市地址-LMLPHP{
str ASP.NET根据IP获取省市地址-LMLPHP+= (char)lowC;
}ASP.NET根据IP获取省市地址-LMLPHP
}ASP.NET根据IP获取省市地址-LMLPHPASP.NET根据IP获取省市地址-LMLPHPreturn str;
}ASP.NET根据IP获取省市地址-LMLPHPASP.NET根据IP获取省市地址-LMLPHPASP.NET根据IP获取省市地址-LMLPHPpublicstring IPLocation(string ip)
ASP.NET根据IP获取省市地址-LMLPHP{
ASP.NET根据IP获取省市地址-LMLPHPthis.ip = ip;
ASP.NET根据IP获取省市地址-LMLPHPthis.QQwry();
ASP.NET根据IP获取省市地址-LMLPHPif (this.country.Trim().Contains(this.local.Trim()))
ASP.NET根据IP获取省市地址-LMLPHPreturnthis.country.Trim();
ASP.NET根据IP获取省市地址-LMLPHPreturnthis.country.Trim() +this.local.Trim();
}ASP.NET根据IP获取省市地址-LMLPHP }ASP.NET根据IP获取省市地址-LMLPHPASP.NET根据IP获取省市地址-LMLPHPASP.NET根据IP获取省市地址-LMLPHP

3、ASP.NET根据IP获取省市地址的调用

 
 
C# 代码   复制
ASP.NET根据IP获取省市地址-LMLPHP
ASP.NET根据IP获取省市地址-LMLPHP string ip="123.81.176.99";
ASP.NET根据IP获取省市地址-LMLPHP IPScanner ipscaner = new IPScanner();
ASP.NET根据IP获取省市地址-LMLPHP ipscaner.IPLocation(ip);
ASP.NET根据IP获取省市地址-LMLPHP string strIPAddress=ipscaner.Country;
05-08 14:57