|
CS類代碼
復(fù)制代碼 代碼如下:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
//引入的命名空間
using System.IO;
/// <summary>
/// 判斷IP歸屬地類
/// </summary>
public class IpSearch
{
private static object lockHelper = new object();
static PHCZIP pcz = new PHCZIP();
static string filePath = "";
static bool fileIsExsit = true;
static IpSearch()
{
filePath = HttpContext.Current.Server.MapPath("~/ipdata.config");
pcz.SetDbFilePath(filePath);
}
/// <summary>
/// 返回IP查找結(jié)果
/// </summary>
/// <param name="IPValue">要查找的IP地址</param>
/// <returns></returns>
public static string GetAddressWithIP(string IPValue)
{
lock (lockHelper)
{
string result = pcz.GetAddressWithIP(IPValue.Trim());
if (fileIsExsit)
{
if (result.IndexOf("IANA") >= 0)
{
return "";
}
else
{
return result;
}
}
else
{
return null;
}
}
}
/// <summary>
/// 輔助類,用于保存IP索引信息
/// </summary>
///
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數(shù)據(jù)庫(kù)類
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 PHCZIP()
{
bFilePathInitialized = false;
}
public PHCZIP(string dbFilePath)
{
bFilePathInitialized = false;
SetDbFilePath(dbFilePath);
}
//使用二分法查找索引區(qū),初始化查找區(qū)間
public void Initialize()
{
Search_Index_Set = 0;
Search_Index_End = Index_Count - 1;
}
//關(guān)閉文件
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;
}
//檢查文件長(zhǎng)度
if (FileStrm.Length < 8)
{
FileStrm.Close();
//FileStrm.Dispose();
return false;
}
//得到第一條索引的絕對(duì)偏移和最后一條索引的絕對(duì)偏移
FileStrm.Seek(0, SeekOrigin.Begin);
Index_Set = GetUInt32();
Index_End = GetUInt32();
//得到總索引條數(shù)
Index_Count = (Index_End - Index_Set) / 7 + 1;
bFilePathInitialized = true;
return true;
}
public string GetAddressWithIP(string IPValue)
{
if (!bFilePathInitialized)
{
return "";
}
Initialize();
UInt32 ip = IPToUInt32(IPValue);
while (true)
{
//首先初始化本輪查找的區(qū)間
//區(qū)間頭
Search_Set = IndexInfoAtPos(Search_Index_Set);
//區(qū)間尾
Search_End = IndexInfoAtPos(Search_Index_End);
//判斷IP是否在區(qū)間頭內(nèi)
if (ip >= Search_Set.IpSet && ip <= Search_Set.IpEnd)
return ReadAddressInfoAtOffset(Search_Set.Offset);
//判斷IP是否在區(qū)間尾內(nèi)
if (ip >= Search_End.IpSet && ip <= Search_End.IpEnd)
return ReadAddressInfoAtOffset(Search_End.Offset);
//計(jì)算出區(qū)間中點(diǎn)
Search_Mid = IndexInfoAtPos((Search_Index_End + Search_Index_Set) / 2);
//判斷IP是否在中點(diǎn)
if (ip >= Search_Mid.IpSet && ip <= Search_Mid.IpEnd)
return ReadAddressInfoAtOffset(Search_Mid.Offset);
//本輪沒(méi)有找到,準(zhǔn)備下一輪
if (ip < Search_Mid.IpSet)
//IP比區(qū)間中點(diǎn)要小,將區(qū)間尾設(shè)為現(xiàn)在的中點(diǎn),將區(qū)間縮小1倍。
Search_Index_End = (Search_Index_End + Search_Index_Set) / 2;
else
//IP比區(qū)間中點(diǎn)要大,將區(qū)間頭設(shè)為現(xiàn)在的中點(diǎn),將區(qū)間縮小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;
//跳過(guò)4字節(jié),因這4個(gè)字節(jié)是該索引的IP區(qū)間上限。
FileStrm.Seek(Offset + 4, SeekOrigin.Begin);
//讀取一個(gè)字節(jié),得到描述國(guó)家信息的“尋址方式”
Tag = GetTag();
if (Tag == 0x01)
{
//模式0x01,表示接下來(lái)的3個(gè)字節(jié)是表示偏移位置
FileStrm.Seek(GetOffset(), SeekOrigin.Begin);
//繼續(xù)檢查“尋址方式”
Tag = GetTag();
if (Tag == 0x02)
{
//模式0x02,表示接下來(lái)的3個(gè)字節(jié)代表國(guó)家信息的偏移位置
//先將這個(gè)偏移位置保存起來(lái),因?yàn)槲覀円x取它后面的地區(qū)信息。
country_Offset = GetOffset();
//讀取地區(qū)信息(注:按照Luma的說(shuō)明,好像沒(méi)有這么多種可能性,但在測(cè)試過(guò)程中好像有些情況沒(méi)有考慮到,
//所以寫了個(gè)ReadArea()來(lái)讀取。
area = ReadArea();
//讀取國(guó)家信息
FileStrm.Seek(country_Offset, SeekOrigin.Begin);
country = ReadString();
}
else
{
//這種模式說(shuō)明接下來(lái)就是保存的國(guó)家和地區(qū)信息了,以'/0'代表結(jié)束。
FileStrm.Seek(-1, SeekOrigin.Current);
country = ReadString();
area = ReadArea();
}
}
else if (Tag == 0x02)
{
//模式0x02,說(shuō)明國(guó)家信息是一個(gè)偏移位置
country_Offset = GetOffset();
//先讀取地區(qū)信息
area = ReadArea();
//讀取國(guó)家信息
FileStrm.Seek(country_Offset, SeekOrigin.Begin);
country = ReadString();
}
else
{
//這種模式最簡(jiǎn)單了,直接讀取國(guó)家和地區(qū)就OK了
FileStrm.Seek(-1, SeekOrigin.Current);
country = ReadString();
area = ReadArea();
}
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();
//根據(jù)索引編號(hào)計(jì)算出在文件中在偏移位置
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;
}
/// <summary>
/// 從IP轉(zhuǎn)換為Int32
/// </summary>
/// <param name="IpValue"></param>
/// <returns></returns>
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);
}
/// <summary>
/// 判斷是否為數(shù)字
/// </summary>
/// <param name="str">待判斷字符串</param>
/// <returns></returns>
protected bool IsNumeric(string str)
{
if (str != null && System.Text.RegularExpressions.Regex.IsMatch(str, @"^-?/d+$"))
return true;
else
return false;
}
protected UInt32 GetUInt32()
{
byte[] TempByte4 = new byte[4];
FileStrm.Read(TempByte4, 0, 4);
return BitConverter.ToUInt32(TempByte4, 0);
}
}
}
需要用到IP數(shù)據(jù)庫(kù),在打包中有。打包下載http://xiazai.jb51.NET/200810/others/iptoaddress.rar
AspNet技術(shù):asp.net DZ論壇中根據(jù)IP地址取得所在地的代碼,轉(zhuǎn)載需保留來(lái)源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。