通过纯真IP地址实现根据用户地址显示信息

为了实现中关村在线商品报价中通过用户的地理位置信息显示相应的报价。

示例地址:http://detail.zol.com.cn/lens/index224693.shtml

现把我做的使用asp.net实现的代码贴出来,与大家共讨之。

第一步:

需要的工具:纯真 IP地址库

下载地址:http://www.baidu.com/s?ie=utf-8&bs=asp.net+%E8%8E%B7%E5%8F%96%E7%94%A8%E6%88%B7%E5%85%AC%E7%BD%91IP&f=8&rsv_bp=1&rsv_spt=3&wd=%E7%BA%AF%E7%9C%9Fip%E5%9C%B0%E5%9D%80%E5%BA%93&rsv_sug3=10&rsv_sug1=10&rsv_sug4=1468&oq=%E7%BA%AF%E7%9C%9Fip%E5%9C%B0%E5%9D%80&sug=%E7%BA%AF%E7%9C%9Fip%E5%9C%B0%E5%9D%80%E6%95%B0%E6%8D%AE%E5%BA%93&rsv_n=1&rsp=0&rsv_sug5=0&rsv_sug=0&inputT=13118

第二步:得到能够通过IP地址获得用户地理定位的代码

在web.config里加上引用纯真IP数据库的具体位置

<appSettings>
<!--QQ纯真IP地址数据库-->
<add key="IPData" value="/data/qqwry.dat"/>
</appSettings>

  

using System;
using System.IO;
using System.Collections;
using System.Text;
using System.Text.RegularExpressions;
using System.Configuration;
using System.Collections.Generic;
using System.Net;
namespace Music.Musical.WebUtility
{
/**/
/// <summary>
/// to scan the ip location from qqwry.dat
/// </summary>
public class IPScaner
{
//私有成员#region 私有成员
private string dataPath = System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["IPData"]);
private string ip;
private string country;
private string local; private long firstStartIp = ;
private long lastStartIp = ;
private FileStream objfs = null;
private long startIp = ;
private long endIp = ;
private int countryFlag = ;
private long endIpOff = ;
private string errMsg = null; //构造函数#region 构造函数
public IPScaner()
{
//
// TODO: 在此处添加构造函数逻辑
//
} //公共属性#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 ;
} long ip_Int = this.IpToInt(ip);
int nRet = ;
if (ip_Int >= IpToInt("127.0.0.0") && ip_Int <= IpToInt("127.255.255.255"))
{
this.country = "本机内部环回地址";
this.local = "";
nRet = ;
}
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 = ;
}
objfs = new FileStream(this.dataPath, FileMode.Open, FileAccess.Read);
try
{
//objfs.Seek(0,SeekOrigin.Begin);
objfs.Position = ;
byte[] buff = new Byte[];
objfs.Read(buff, , );
firstStartIp = buff[] + buff[] * + buff[] * * + buff[] * * * ;
lastStartIp = buff[] * + buff[] * + buff[] * * + buff[] * * * ;
long recordCount = Convert.ToInt64((lastStartIp - firstStartIp) / 7.0);
if (recordCount <= )
{
country = "FileDataError";
objfs.Close();
return ;
}
long rangE = recordCount;
long rangB = ;
long recNO = ;
while (rangB < rangE - )
{
recNO = (rangE + rangB) / ;
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 = ;
this.country = "未知";
this.local = "";
}
objfs.Close();
return nRet;
}
catch
{
return ;
} } // IP地址转换成Int数据#region IP地址转换成Int数据
private long IpToInt(string ip)
{
char[] dot = new char[] { '.' };
string[] ipArr = ip.Split(dot);
if (ipArr.Length == )
ip = ip + ".0";
ipArr = ip.Split(dot); long ip_Int = ;
long p1 = long.Parse(ipArr[]) * * * ;
long p2 = long.Parse(ipArr[]) * * ;
long p3 = long.Parse(ipArr[]) * ;
long p4 = long.Parse(ipArr[]);
ip_Int = p1 + p2 + p3 + p4;
return ip_Int;
} //int转换成IP#region int转换成IP
private string IntToIP(long ip_Int)
{
long seg1 = (ip_Int & 0xff000000) >> ;
if (seg1 < )
seg1 += 0x100;
long seg2 = (ip_Int & 0x00ff0000) >> ;
if (seg2 < )
seg2 += 0x100;
long seg3 = (ip_Int & 0x0000ff00) >> ;
if (seg3 < )
seg3 += 0x100;
long seg4 = (ip_Int & 0x000000ff);
if (seg4 < )
seg4 += 0x100;
string ip = seg1.ToString() + "." + seg2.ToString() + "." + seg3.ToString() + "." + seg4.ToString(); return ip;
}
//获取起始IP范围#region 获取起始IP范围
private long GetStartIp(long recNO)
{
long offSet = firstStartIp + recNO * ;
//objfs.Seek(offSet,SeekOrigin.Begin);
objfs.Position = offSet;
byte[] buff = new Byte[];
objfs.Read(buff, , ); endIpOff = Convert.ToInt64(buff[].ToString()) + Convert.ToInt64(buff[].ToString()) * + Convert.ToInt64(buff[].ToString()) * * ;
startIp = Convert.ToInt64(buff[].ToString()) + Convert.ToInt64(buff[].ToString()) * + Convert.ToInt64(buff[].ToString()) * * + Convert.ToInt64(buff[].ToString()) * * * ;
return startIp;
}
// 获取结束IP#region 获取结束IP
private long GetEndIp()
{
//objfs.Seek(endIpOff,SeekOrigin.Begin);
objfs.Position = endIpOff;
byte[] buff = new Byte[];
objfs.Read(buff, , );
this.endIp = Convert.ToInt64(buff[].ToString()) + Convert.ToInt64(buff[].ToString()) * + Convert.ToInt64(buff[].ToString()) * * + Convert.ToInt64(buff[].ToString()) * * * ;
this.countryFlag = buff[];
return this.endIp;
}
//获取国家/区域偏移量#region 获取国家/区域偏移量
private string GetCountry()
{
switch (this.countryFlag)
{
case :
case :
this.country = GetFlagStr(this.endIpOff + );
this.local = ( == this.countryFlag) ? " " : this.GetFlagStr(this.endIpOff + );
break;
default:
this.country = this.GetFlagStr(this.endIpOff + );
this.local = this.GetFlagStr(objfs.Position);
break;
}
return " ";
}
//获取国家/区域字符串#region 获取国家/区域字符串
private string GetFlagStr(long offSet)
{
int flag = ;
byte[] buff = new Byte[];
while ( == )
{
//objfs.Seek(offSet,SeekOrigin.Begin);
objfs.Position = offSet;
flag = objfs.ReadByte();
if (flag == || flag == )
{
objfs.Read(buff, , );
if (flag == )
{
this.countryFlag = ;
this.endIpOff = offSet - ;
}
offSet = Convert.ToInt64(buff[].ToString()) + Convert.ToInt64(buff[].ToString()) * + Convert.ToInt64(buff[].ToString()) * * ;
}
else
{
break;
}
}
if (offSet < )
return " ";
objfs.Position = offSet;
return GetStr();
}
//GetStr#region GetStr
private string GetStr()
{
byte lowC = ;
byte upC = ;
string str = "";
byte[] buff = new byte[];
while ( == )
{
lowC = (Byte)objfs.ReadByte();
if (lowC == )
break;
if (lowC > )
{
upC = (byte)objfs.ReadByte();
buff[] = lowC;
buff[] = upC;
System.Text.Encoding enc = System.Text.Encoding.GetEncoding("GB2312");
str += enc.GetString(buff);
}
else
{
str += (char)lowC;
}
}
return str;
}
//获取IP地址#region 获取IP地址
public string IPLocation()
{
this.QQwry();
return this.country + this.local;
}
public string IPLocation(string dataPath, string ip)
{
this.dataPath = dataPath;
this.ip = ip;
this.QQwry();
return this.country + this.local;
} }
}

第三步:获取用户IP地址并将IP转换为地理位置

 public static List<string> GetIPInfo()
{
string ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (ip == null || ip.Length == || ip.ToLower().IndexOf("unknown") > -)
{
ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
else
{
if (ip.IndexOf(',') > -)
{
ip = ip.Substring(, ip.IndexOf(','));
}
if (ip.IndexOf(';') > -)
{
ip = ip.Substring(, ip.IndexOf(';'));
}
} Regex regex = new Regex("[^0-9.]");
if (ip == null || ip.Length == || regex.IsMatch(ip))
{
ip = HttpContext.Current.Request.UserHostAddress;
if (ip == null || ip.Length == || regex.IsMatch(ip))
{
ip = "101.104.213.253";
}
}
IPScaner objScan = new IPScaner();
objScan.IP = ip; ;
string addre = objScan.IPLocation();
//string err = objScan.ErrMsg;
Regex reg = new Regex(@"([\s\S]+省)?([\s\S]+市)");
List<string> list = new List<string>();
if (reg.IsMatch(addre))
{
GroupCollection groupList = reg.Match(addre).Groups;
foreach (Group gr in groupList)
{
list.Add(gr.Value);
}
}
return list;
}

第四步:通过得到的用户地理位置和省市比对,显示信息
通过纯真IP地址实现根据用户地址显示信息

上一篇:Attribute的理解和认识


下一篇:LeetCode 14. 最长公共前缀(Longest Common Prefix)