ip对应的区域查询(asp.net版)
2019独角兽企业重金招聘Python工程师标准>>>
源代码是一个类
IP的数据文件是从网上下载的,大家只要随便搜索下就有一大把
调用方式 Response.Write(IPSearch.GetAddressWithIP("222.221.164.172"));
http://www.leiyang.ccoo.cn/blog/article.asp?id=451104
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;
///
/// IPSearch 的摘要说明
///
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/QQWry.Dat " );
pcz.SetDbFilePath(filePath);
}
///
/// 返回IP查找结果
///
/// 要查找的IP地址
///
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 ;
}
}
}
///
/// 辅助类,用于保存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 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 ;
}
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();
}
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;
}
///
/// 从IP转换为Int32
///
///
///
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+$ " ))
return true ;
else
return false ;
}
protected UInt32 GetUInt32()
{
byte [] TempByte4 =
转载于:https://my.oschina.net/willman/blog/13929
ip对应的区域查询(asp.net版)相关推荐
- ip对应的区域查询(php版)(转)
<?php /** *@ Date 2010.12.21 *@ Author 王刚 *@ Email 373882774@qq.com *@ QQ 373882774 *@ Blog http: ...
- [置顶] C#中通过调用webService获取上网IP地址的区域的方法
最近在开发项目的过程中需要获取用户查询时的IP地址的区域,以用来做统计分析用.经过一番查阅资料,然后写了个测试程序,很快就搞定了这个功能. 现在分享下我的实现方法,大家如果有更好的实现,欢迎提出哦. ...
- 触摸查询系统服务器注册码,多媒体触摸查询系统旗舰版-用户手册(20161215.pdf
多媒体触摸查询系统旗舰版-用户手册(20161215 多媒体触摸查询系统旗舰版-用户手册 1.产品功能列表3 2.产品安装4 2.1服务器安装4 2.2Windows终端安装9 2.3 Android ...
- java根据IP获取当前区域天气信息
java根据IP获取当前区域天气信息 大致思路是客户端发起请求,我们首先根据请求获取到外网IP,然后再根据外网IP获取到用户所在城市,最后根据城市获取到天气信息 获取外网IP 万网获取外网IP地址: ...
- Qt quick-QML地图引擎之v3版本(新增高德/谷歌在线/离线预览/多线程离线裁剪下载/区域查询/位置搜索/路径规划)
在上个版本64.qt quick-qml使用高德地图插件实现V2版本(新增:位置搜索.路径规划.轨迹编辑等)_诺谦的博客-CSDN博客_qt高德地图插件基础下新增以下功能: 1.支持多线程请求.超时重 ...
- php域名解析查询系统网页版
域名解析信息查询工具网页版 可查域名解析的IP,以及A.CNAME.MX.NS.TXT.AAAA.SRV等解析记录. 是网站站长及用到域名的技术员必备的实用小工具. 推荐linux平台php5.4-5 ...
- 分享2020 几个好用的ip地址归属地查询
ip地址归属地查询接口 一.百度 大公司,不必说,肯定好用,我就一直用的他作为主要查询接口. url:http://opendata.baidu.com/api.php params:query=ip ...
- Web应用验证码方面总结(ASP.NET版)
目录 Web应用验证码方面总结(ASP.NET版) 1 目录 2 修订历史纪录 3 1 验证码在Web应用上的作用 4 2 验证码示例 5 2.1 5 2.2 ma ...
- 关于DNS区域查询,DNS字典爆破,以及查询文件问题
在查询DNS区域传输中所有的主机信息时候,使用dig中的axfr全区域查询或者使用host命令查询的效果是一样的,但是大部分的DNS服务器设置都比较健全,不会轻易将DNS主机记录反馈给我们,这时候可以 ...
最新文章
- 避坑指南:数据科学家新手常犯的13个错误(附工具、学习资源链接)
- vbscript调用WMI一键式式发布网站
- GitHub 宣布正式收购 npm | 云原生生态周报 Vol. 42
- C++面向对象模型初探
- 深入浅出JSONP--解决ajax跨域问题
- PAT天梯赛L3-004 肿瘤诊断
- 公司的一些SEO面试题
- 直通BAT必考题系列:深入详解JVM内存模型与JVM参数详细配置
- JAVA分析命令:jps、jstack、jmap、jhat
- mysql计算折纸_mysql数据库的创建和授权
- xilinx set up debug
- snort 错误 (CentOS 8)
- 如鹏网.Net三层架构 第四章代码生成器
- 输入法只能打拼音不能打汉字
- HTML常用标签总结 [建议收藏]
- WLog日志库:c++ 高拓展、高性能日志库
- C语言--指针之空指针(void *)
- iOS——SDWebImage解读
- 苹果屏幕自动变暗_苹果iOS 14震撼发布 全新功能对标安卓
- mac 移动硬盘装linux系统安装教程,移动硬盘上安装Ubuntu系统
热门文章
- 【RobotStudio学习笔记】(五)工具数据
- Linux文件内容查阅
- 接口怎么获取数组底下的数组_3分钟短文 | PHP数组获取最后一个元素,10个方式中哪个有错?...
- android java 同步_Android 中的同步
- js 连mysql 延后_详解MySQL 主从同步延迟
- cmd 生成wsdl文件_Spring Boot SOAP系列之WSDL是什么“Lese”
- JAVA秒杀mysql层实现_一文搞懂MySQL的Join,聊一聊秒杀架构设计
- linux服务器证书安装教程,linux服务器使用certbot免费安装ssl证书
- hp服务器pe系统安装win7系统安装系统安装,惠普Folio 1040 G1 u盘pe如何重装win7系统...
- python open函数encoding_关于python内open函数encoding编码问题