IPSearch.ashx (IHttpHandler) 接收 IP 參數,調用 QQWryLocator 查詢,返回純文本「國家 地區」。public class QQWryLocator
{
private readonly byte[] _data;
private readonly int _firstOffset, _lastOffset, _count;
private static readonly Encoding Gb2312 = Encoding.GetEncoding("GB2312");
public QQWryLocator(string path)
{
_data = File.ReadAllBytes(path);
_firstOffset = ReadInt32(0);
_lastOffset = ReadInt32(4);
_count = (_lastOffset - _firstOffset) / 7 + 1;
}
public IPLocation Query(string ip)
{
uint ipNum = IpToUInt(ip);
// ... 二分查找邏輯同上 ...
return new IPLocation { IP = ip, Country = country, Local = local };
}
// 輔助方法:ReadInt32, IpToUInt, ReadString, ParseRecord 等
}
public class IPSearch : IHttpHandler
{
private static readonly QQWryLocator Locator = new QQWryLocator(HttpContext.Current.Server.MapPath("~/App_Data/QQWry.dat"));
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain; charset=utf-8";
string ip = context.Request["ip"];
if (string.IsNullOrEmpty(ip)) { context.Response.Write("參數錯誤"); return; }
var loc = Locator.Query(ip);
context.Response.Write($"{loc.Country} {loc.Local}");
}
public bool IsReusable => true; // 無狀態,可復用
}
<table id="userTable">
<thead><tr><th>用戶</th><th>金額</th><th>IP</th></tr></thead>
<tbody>
<tr><td>用戶A</td><td>100</td><td class="ip-cell">122.229.191.8</td></tr>
<tr><td>用戶B</td><td>200</td><td class="ip-cell">110.87.98.30</td></tr>
</tbody>
</table>
<script src="jquery-3.7.1.min.js"></script>
<script>
$(function () {
$(".ip-cell").each(function () {
var $cell = $(this);
var ip = $cell.text().trim();
$.get("IPSearch.ashx", { ip: ip }, function (data) {
$cell.text(data).attr("title", ip); // 滑鼠懸停顯示原 IP
});
});
});
</script>
BatchSearch.ashx,返回 { "ip1": "loc1", "ip2": "loc2" },減少 HTTP 請求數。localStorage 緩存 IP→地址映射,下次直接讀取。MemoryCache 緩存熱點 IP 查詢結果,TTL 1 天。結合 QQWry.NET 高性能解析組件與 IHttpHandler 輕量級端點,配合 jQuery 異步渲染,可快速為舊系統添加 IP 歸屬地顯示功能。新系統建議採用批量 API + 緩存策略,或直接在服務端渲染完成。