国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

asp.net DZ論壇中根據IP地址取得所在地的代碼

2024-07-10 12:39:40
字體:
來源:轉載
供稿:網友
使用方法: IpSearch.GetAddressWithIP("202.96.128.167")
CS類代碼
代碼如下:
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查找結果
/// </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數據庫類
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);
}

//使用二分法查找索引區,初始化查找區間
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();
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宝应县| 城口县| 手机| 襄樊市| 宿松县| 西城区| 沽源县| 资兴市| 噶尔县| 赤城县| 牙克石市| 吴川市| 尼玛县| 年辖:市辖区| 阳东县| 苏尼特右旗| 孝感市| 鸡东县| 千阳县| 崇仁县| 海口市| 米泉市| 长沙市| 西藏| 黎川县| 岱山县| 隆安县| 怀集县| 名山县| 罗定市| 都昌县| 鱼台县| 绥芬河市| 花莲县| 昌吉市| 郎溪县| 昌乐县| 伊春市| 新绛县| 青河县| 河间市|