public string GetAddressWithIP(string IPValue) { if (!bFilePathInitialized) return " "; Initialize(); UInt32 ip = IPToUInt32(IPValue); while (true) { //首先初始化本轮查找的区间 //区间头 Search_Set = IndexInfoAtPos(Search_Index_Set); //区间尾 Search_End = IndexInfoAtPos(Search_Index_End); //判断IP是否在区间头内 if (ip >= Search_Set.IpSet && ip <= Search_Set.IpEnd) return ReadAddressInfoAtOffset(Search_Set.Offset); //判断IP是否在区间尾内 if (ip >= Search_End.IpSet && ip <= Search_End.IpEnd) return ReadAddressInfoAtOffset(Search_End.Offset); //计算出区间中点 Search_Mid = IndexInfoAtPos((Search_Index_End + Search_Index_Set) / 2); //判断IP是否在中点 if (ip >= Search_Mid.IpSet && ip <= Search_Mid.IpEnd) return ReadAddressInfoAtOffset(Search_Mid.Offset); //本轮没有找到,准备下一轮 if (ip < Search_Mid.IpSet) //IP比区间中点要小,将区间尾设为现在的中点,将区间缩小1倍。 Search_Index_End = (Search_Index_End + Search_Index_Set) / 2; else //IP比区间中点要大,将区间头设为现在的中点,将区间缩小1倍。 Search_Index_Set = (Search_Index_End + Search_Index_Set) / 2; } return " "; } private string ReadAddressInfoAtOffset(UInt32 Offset) { string country = " "; //string area = " "; UInt32 country_Offset = 0; byte Tag = 0; //跳过4字节,因这4个字节是该索引的IP区间上限。 FileStrm.Seek(Offset + 4, SeekOrigin.Begin); //读取一个字节,得到描述国家信息的“寻址方式” Tag = GetTag(); if (Tag == 0x01) { //模式0x01,表示接下来的3个字节是表示偏移位置 FileStrm.Seek(GetOffset(), SeekOrigin.Begin); //继续检查“寻址方式” Tag = GetTag(); if (Tag == 0x02) { //模式0x02,表示接下来的3个字节代表国家信息的偏移位置 //先将这个偏移位置保存起来,因为我们要读取它后面的地区信息。 country_Offset = GetOffset(); //读取地区信息(注:按照Luma的说明,好像没有这么多种可能性,但在测试过程中好像有些情况没有考虑到, //所以写了个ReadArea()来读取。 //area = ReadArea(); //读取国家信息 FileStrm.Seek(country_Offset, SeekOrigin.Begin); country = ReadString(); } else { //这种模式说明接下来就是保存的国家和地区信息了,以 '\0 '代表结束。 FileStrm.Seek(-1, SeekOrigin.Current); country = ReadString(); // area = ReadArea(); } } else if (Tag == 0x02) { //模式0x02,说明国家信息是一个偏移位置 country_Offset = GetOffset(); //先读取地区信息 // area = ReadArea(); //读取国家信息 FileStrm.Seek(country_Offset, SeekOrigin.Begin); country = ReadString(); } else { //这种模式最简单了,直接读取国家和地区就OK了 FileStrm.Seek(-1, SeekOrigin.Current); country = ReadString(); // area = ReadArea(); } return country; //string Address = country + " " + area; // return Address; } private UInt32 GetOffset() { byte[] TempByte4 = new byte[4]; TempByte4[0] = (byte)FileStrm.ReadByte(); TempByte4[1] = (byte)FileStrm.ReadByte(); TempByte4[2] = (byte)FileStrm.ReadByte(); TempByte4[3] = 0; return BitConverter.ToUInt32(TempByte4, 0); } protected string ReadArea() { byte Tag = GetTag(); if (Tag == 0x01 || Tag == 0x02) { FileStrm.Seek(GetOffset(), SeekOrigin.Begin); return ReadString(); } else { FileStrm.Seek(-1, SeekOrigin.Current); return ReadString(); } } protected string ReadString() { UInt32 Offset = 0; byte[] TempByteArray = new byte[256]; TempByteArray[Offset] = (byte)FileStrm.ReadByte(); while (TempByteArray[Offset] != 0x00) { Offset += 1; TempByteArray[Offset] = (byte)FileStrm.ReadByte(); } return System.Text.Encoding.Default.GetString(TempByteArray).TrimEnd('\0'); } protected byte GetTag() { return (byte)FileStrm.ReadByte(); } protected CZ_INDEX_INFO IndexInfoAtPos(UInt32 Index_Pos) { CZ_INDEX_INFO Index_Info = new CZ_INDEX_INFO(); //根据索引编号计算出在文件中在偏移位置 FileStrm.Seek(Index_Set + 7 * Index_Pos, SeekOrigin.Begin); Index_Info.IpSet = GetUInt32(); Index_Info.Offset = GetOffset(); FileStrm.Seek(Index_Info.Offset, SeekOrigin.Begin); Index_Info.IpEnd = GetUInt32(); return Index_Info; } public UInt32 IPToUInt32(string IpValue) { string[] IpByte = IpValue.Split('.'); Int32 nUpperBound = IpByte.GetUpperBound(0); if (nUpperBound != 3) { IpByte = new string[4]; for (Int32 i = 1; i <= 3 - nUpperBound; i++) IpByte[nUpperBound + i] = "0 "; } byte[] TempByte4 = new byte[4]; for (Int32 i = 0; i <= 3; i++) { // '如果是.Net 2.0可以支持TryParse。 // 'If Not (Byte.TryParse(IpByte(i), TempByte4(3 - i))) Then // ' TempByte4(3 - i) = &H0 // 'End If if (IsNumeric(IpByte[i])) TempByte4[3 - i] = (byte)(Convert.ToInt32(IpByte[i]) & 0xff); } return BitConverter.ToUInt32(TempByte4, 0); } protected bool IsNumeric(string str) { if (str != null && System.Text.RegularExpressions.Regex.IsMatch(str, @"^\d+$|^\d+\.\d+$")) return true; else return false; } protected UInt32 GetUInt32() { byte[] TempByte4 = new byte[4]; FileStrm.Read(TempByte4, 0, 4); return BitConverter.ToUInt32(TempByte4, 0); } }
http://www.fraudlabs.com/samplecode/IP2LocationWebServiceClientCSharp2005.zip IP2LocationWebService 输入IP查询该ip的详细信息: 查询出的信息: Address : 115.171.120.80 Country Code : CN Country Name : CHINA Region : BEIJING City : BEIJING Latitude : 39.9 Longitude : 116.413 Zip Code : ISP Name : CHINANET CDMA NETWORK Domain Name : Time Zone : +08:00 Net Speed : DSL Area Code : Weather Station : BEIJING Weather Station Code : CHXX0008
2,你不知道怎么获取客户端的IP地址?
3,你有了IP地址却不知道怎么和数据库里的内容进行关联?纯真数据库的作用是 根据客户端的IP地址判断IP地址的所属地区
用户登录的时候,你获取客户端IP存到数据库就行了,下次登录根据账号和时间查询出上次登录IP就行了。
如果需要归属地的话就需要IP库 了,那样才可以通过IP查询归属地
下次登录的时候查出上次登录IP,通过这个IP去你的Ip库查询归属地,就Ok了。如果还有需要加我 : [email protected]
/// PHCZIP 的摘要说明。
/// </summary>
///
//辅助类,用于保存IP索引信息
public class CZ_INDEX_INFO
{
public UInt32 IpSet;
public UInt32 IpEnd;
public UInt32 Offset; public CZ_INDEX_INFO()
{
IpSet = 0;
IpEnd = 0;
Offset = 0;
}
} //读取纯真IP数据库类
public class PHCZIP
{
protected bool bFilePathInitialized;
protected string FilePath;
protected FileStream FileStrm;
protected UInt32 Index_Set;
protected UInt32 Index_End;
protected UInt32 Index_Count;
protected UInt32 Search_Index_Set;
protected UInt32 Search_Index_End;
protected CZ_INDEX_INFO Search_Set;
protected CZ_INDEX_INFO Search_Mid;
protected CZ_INDEX_INFO Search_End;
public static string[] arr = new string[] { "广东", "江苏", "山东", "四川", "台湾", "浙江", "辽宁", "河南", "湖北", "福建", "河北", "湖南", "上海", "香港", "北京", "黑龙江", "天津", "重庆", "江西", "山西", "安徽", "陕西", "海南", "云南", "甘肃", "内蒙古", "贵州", "新疆", "西藏", "青海", "广西", "澳门", "宁夏", "吉林", "北京", "上海", "天津", "重庆" };
#region translation related
public string alanyze(string _location)
{
for (int i = 0; i < arr.Length - 1; i++)
{
if (_location.IndexOf(arr[i].ToString()) != -1)
{
_location += "(中国)";
}
}
if (_location.IndexOf("台湾") != -1)
{
_location = "台湾,中国";
}
return _location; }
public string GetGoogleTranslateJSONString(string strTranslateString,
string strRequestLanguage, string strResultLanguage)
{
WebRequest request = HttpWebRequest.Create(
"http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=" + strTranslateString + "&langpair=" + strRequestLanguage + "%7C" + strResultLanguage); request.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse response =
(HttpWebResponse)request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();
return responseFromServer;
}
public class TranslateString
{
private TranslatedText responsedata;
public TranslatedText responseData
{
get { return responsedata; }
set { responsedata = value; }
}
private string responsedetails;
public string responseDetails
{
get { return responsedetails; }
set { responsedetails = value; }
}
private int responsestatus;
public int responseStatus
{
get { return responsestatus; }
set { responsestatus = value; }
} /**/
/// <summary>
/// 译文内容
/// </summary>
public class TranslatedText
{
private string translatedtext;
public string translatedText
{
get { return translatedtext; }
set { translatedtext = value; }
}
}
}
public string MultiLanguageTranslate(string strTranslateString, string
strRequestLanguage, string strResultLanguage)
{
try
{
if (!string.IsNullOrEmpty(strTranslateString))
{
TranslateString transtring =
(TranslateString)
Newtonsoft.Json.JavaScriptConvert.DeserializeObject(
GetGoogleTranslateJSONString(strTranslateString,
strRequestLanguage, strResultLanguage),
typeof(TranslateString));
if (transtring.responseStatus == 200)
return transtring.responseData.translatedText;
else
return "There was an error.";
}
else
{
return strTranslateString;
}
}
catch (Exception e)
{
return e.Message;
}
}
#endregion
public PHCZIP()
{
bFilePathInitialized = false;
} public PHCZIP(string dbFilePath)
{
bFilePathInitialized = false;
SetDbFilePath(dbFilePath);
} //使用二分法查找索引区,初始化查找区间
public void Initialize()
{
Search_Index_Set = 0;
Search_Index_End = Index_Count - 1;
} //关闭文件
public void Dispose()
{
if (bFilePathInitialized)
{
bFilePathInitialized = false;
FileStrm.Close();
//FileStrm.Dispose();
} }
public bool SetDbFilePath(string dbFilePath)
{
if (dbFilePath == " ")
{
return false;
} try
{
FileStrm = new FileStream(dbFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
}
catch
{
return false;
}
//检查文件长度
if (FileStrm.Length < 8)
{
FileStrm.Close();
//FileStrm.Dispose();
return false;
}
//得到第一条索引的绝对偏移和最后一条索引的绝对偏移
FileStrm.Seek(0, SeekOrigin.Begin);
Index_Set = GetUInt32();
Index_End = GetUInt32(); //得到总索引条数
Index_Count = (Index_End - Index_Set) / 7 + 1;
bFilePathInitialized = true; return true; }
{
if (!bFilePathInitialized)
return " "; Initialize(); UInt32 ip = IPToUInt32(IPValue); while (true)
{ //首先初始化本轮查找的区间 //区间头
Search_Set = IndexInfoAtPos(Search_Index_Set);
//区间尾
Search_End = IndexInfoAtPos(Search_Index_End); //判断IP是否在区间头内
if (ip >= Search_Set.IpSet && ip <= Search_Set.IpEnd)
return ReadAddressInfoAtOffset(Search_Set.Offset);
//判断IP是否在区间尾内
if (ip >= Search_End.IpSet && ip <= Search_End.IpEnd)
return ReadAddressInfoAtOffset(Search_End.Offset); //计算出区间中点
Search_Mid = IndexInfoAtPos((Search_Index_End + Search_Index_Set) / 2); //判断IP是否在中点
if (ip >= Search_Mid.IpSet && ip <= Search_Mid.IpEnd)
return ReadAddressInfoAtOffset(Search_Mid.Offset); //本轮没有找到,准备下一轮
if (ip < Search_Mid.IpSet)
//IP比区间中点要小,将区间尾设为现在的中点,将区间缩小1倍。
Search_Index_End = (Search_Index_End + Search_Index_Set) / 2;
else
//IP比区间中点要大,将区间头设为现在的中点,将区间缩小1倍。
Search_Index_Set = (Search_Index_End + Search_Index_Set) / 2;
}
return " "; } private string ReadAddressInfoAtOffset(UInt32 Offset)
{
string country = " ";
//string area = " ";
UInt32 country_Offset = 0;
byte Tag = 0;
//跳过4字节,因这4个字节是该索引的IP区间上限。
FileStrm.Seek(Offset + 4, SeekOrigin.Begin); //读取一个字节,得到描述国家信息的“寻址方式”
Tag = GetTag(); if (Tag == 0x01)
{ //模式0x01,表示接下来的3个字节是表示偏移位置
FileStrm.Seek(GetOffset(), SeekOrigin.Begin); //继续检查“寻址方式”
Tag = GetTag();
if (Tag == 0x02)
{
//模式0x02,表示接下来的3个字节代表国家信息的偏移位置
//先将这个偏移位置保存起来,因为我们要读取它后面的地区信息。
country_Offset = GetOffset();
//读取地区信息(注:按照Luma的说明,好像没有这么多种可能性,但在测试过程中好像有些情况没有考虑到,
//所以写了个ReadArea()来读取。
//area = ReadArea();
//读取国家信息
FileStrm.Seek(country_Offset, SeekOrigin.Begin);
country = ReadString();
}
else
{
//这种模式说明接下来就是保存的国家和地区信息了,以 '\0 '代表结束。
FileStrm.Seek(-1, SeekOrigin.Current);
country = ReadString();
// area = ReadArea(); }
}
else if (Tag == 0x02)
{
//模式0x02,说明国家信息是一个偏移位置
country_Offset = GetOffset();
//先读取地区信息
// area = ReadArea();
//读取国家信息
FileStrm.Seek(country_Offset, SeekOrigin.Begin);
country = ReadString();
}
else
{
//这种模式最简单了,直接读取国家和地区就OK了
FileStrm.Seek(-1, SeekOrigin.Current);
country = ReadString();
// area = ReadArea(); }
return country;
//string Address = country + " " + area;
// return Address; } private UInt32 GetOffset()
{
byte[] TempByte4 = new byte[4];
TempByte4[0] = (byte)FileStrm.ReadByte();
TempByte4[1] = (byte)FileStrm.ReadByte();
TempByte4[2] = (byte)FileStrm.ReadByte();
TempByte4[3] = 0;
return BitConverter.ToUInt32(TempByte4, 0);
} protected string ReadArea()
{
byte Tag = GetTag(); if (Tag == 0x01 || Tag == 0x02)
{
FileStrm.Seek(GetOffset(), SeekOrigin.Begin);
return ReadString();
}
else
{
FileStrm.Seek(-1, SeekOrigin.Current);
return ReadString();
}
} protected string ReadString()
{
UInt32 Offset = 0;
byte[] TempByteArray = new byte[256];
TempByteArray[Offset] = (byte)FileStrm.ReadByte();
while (TempByteArray[Offset] != 0x00)
{
Offset += 1;
TempByteArray[Offset] = (byte)FileStrm.ReadByte();
}
return System.Text.Encoding.Default.GetString(TempByteArray).TrimEnd('\0');
} protected byte GetTag()
{
return (byte)FileStrm.ReadByte();
} protected CZ_INDEX_INFO IndexInfoAtPos(UInt32 Index_Pos)
{
CZ_INDEX_INFO Index_Info = new CZ_INDEX_INFO();
//根据索引编号计算出在文件中在偏移位置
FileStrm.Seek(Index_Set + 7 * Index_Pos, SeekOrigin.Begin);
Index_Info.IpSet = GetUInt32();
Index_Info.Offset = GetOffset();
FileStrm.Seek(Index_Info.Offset, SeekOrigin.Begin);
Index_Info.IpEnd = GetUInt32(); return Index_Info;
} public UInt32 IPToUInt32(string IpValue)
{
string[] IpByte = IpValue.Split('.');
Int32 nUpperBound = IpByte.GetUpperBound(0);
if (nUpperBound != 3)
{
IpByte = new string[4];
for (Int32 i = 1; i <= 3 - nUpperBound; i++)
IpByte[nUpperBound + i] = "0 ";
} byte[] TempByte4 = new byte[4];
for (Int32 i = 0; i <= 3; i++)
{
// '如果是.Net 2.0可以支持TryParse。
// 'If Not (Byte.TryParse(IpByte(i), TempByte4(3 - i))) Then
// ' TempByte4(3 - i) = &H0
// 'End If
if (IsNumeric(IpByte[i]))
TempByte4[3 - i] = (byte)(Convert.ToInt32(IpByte[i]) & 0xff);
} return BitConverter.ToUInt32(TempByte4, 0);
} protected bool IsNumeric(string str)
{
if (str != null && System.Text.RegularExpressions.Regex.IsMatch(str, @"^\d+$|^\d+\.\d+$")) return true; else return false;
} protected UInt32 GetUInt32()
{
byte[] TempByte4 = new byte[4];
FileStrm.Read(TempByte4, 0, 4);
return BitConverter.ToUInt32(TempByte4, 0);
}
}
IP2LocationWebService 输入IP查询该ip的详细信息:
查询出的信息:
Address : 115.171.120.80
Country Code : CN
Country Name : CHINA
Region : BEIJING
City : BEIJING
Latitude : 39.9
Longitude : 116.413
Zip Code :
ISP Name : CHINANET CDMA NETWORK
Domain Name :
Time Zone : +08:00
Net Speed : DSL
Area Code :
Weather Station : BEIJING
Weather Station Code : CHXX0008