asp.net 纯真ip库取得所在地实现代码_实用技巧

有关纯真IP库的相关知识:查看( pdf格式)
纯真IP库官方下载地址:http://www.cz88.net/
使用的类(已经经过调试.net 2.0,已对错误做了修改):

复制代码 代码如下:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace QQWry.NET
{

public class IPLocation
{
private string _IP;
private string _Country;
private string _Local;
public string IP
{ get{return _IP;}
set{_IP = value;}
}
public string Country
{ get{return _Country;}
set{_Country = value;}
}
public string Local
{ get{return _Local;}
set{_Local = value;}
}
}
public class QQWryLocator
{
private byte[] data;
Regex regex = new Regex(@"(((\d{1,2})|(1\d{2})|(2[0-4]\d)|(25[0-5]))\.){3}((\d{1,2})|(1\d{2})|(2[0-4]\d)|(25[0-5]))");
long firstStartIpOffset;
long lastStartIpOffset;
long ipCount;
public long Count { get { return ipCount; } }
public QQWryLocator(string dataPath)
{
using (FileStream fs = new FileStream(dataPath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
data = new byte[fs.Length];
fs.Read(data, 0, data.Length);
}
byte[] buffer = new byte[8];
Array.Copy(data, 0, buffer, 0, 8);
firstStartIpOffset = ((buffer[0] + (buffer[1] * 0x100)) + ((buffer[2] * 0x100) * 0x100)) + (((buffer[3] * 0x100) * 0x100) * 0x100);
lastStartIpOffset = ((buffer[4] + (buffer[5] * 0x100)) + ((buffer[6] * 0x100) * 0x100)) + (((buffer[7] * 0x100) * 0x100) * 0x100);
ipCount = Convert.ToInt64((double)(((double)(lastStartIpOffset - firstStartIpOffset)) / 7.0));

if (ipCount <= 1L)
{
throw new ArgumentException("ip FileDataError");
}
}
private static long IpToInt(string ip)
{
char[] separator = new char[] { '.' };
if (ip.Split(separator).Length == 3)
{
ip = ip + ".0";
}
string[] strArray = ip.Split(separator);
long num2 = ((long.Parse(strArray[0]) * 0x100L) * 0x100L) * 0x100L;
long num3 = (long.Parse(strArray[1]) * 0x100L) * 0x100L;
long num4 = long.Parse(strArray[2]) * 0x100L;
long num5 = long.Parse(strArray[3]);
return (((num2 + num3) + num4) + num5);
}
private static string IntToIP(long ip_Int)
{
long num = (long)((ip_Int & 0xff000000L) >> 0x18);
if (num < 0L)
{
num += 0x100L;
}
long num2 = (ip_Int & 0xff0000L) >> 0x10;
if (num2 < 0L)
{
num2 += 0x100L;
}
long num3 = (ip_Int & 0xff00L) >> 8;
if (num3 < 0L)
{
num3 += 0x100L;
}
long num4 = ip_Int & 0xffL;
if (num4 < 0L)
{
num4 += 0x100L;
}
return (num.ToString() + "." + num2.ToString() + "." + num3.ToString() + "." + num4.ToString());
}
public IPLocation Query(string ip)
{
if (!regex.Match(ip).Success)
{
throw new ArgumentException("IP格式错误");
}
IPLocation ipLocation = new IPLocation();
ipLocation.IP = ip;
long intIP = IpToInt(ip);
if ((intIP >= IpToInt("127.0.0.1") && (intIP <= IpToInt("127.255.255.255"))))
{
ipLocation.Country = "本机内部环回地址";
ipLocation.Local = "";
}
else
{
if ((((intIP >= IpToInt("0.0.0.0")) && (intIP <= IpToInt("2.255.255.255"))) || ((intIP >= IpToInt("64.0.0.0")) && (intIP <= IpToInt("126.255.255.255")))) ||
((intIP >= IpToInt("58.0.0.0")) && (intIP <= IpToInt("60.255.255.255"))))
{
ipLocation.Country = "网络保留地址";
ipLocation.Local = "";
}
}
long right = ipCount;
long left = 0L;
long middle = 0L;
long startIp = 0L;
long endIpOff = 0L;
long endIp = 0L;
int countryFlag = 0;
while (left < (right - 1L))
{
middle = (right + left) / 2L;
startIp = GetStartIp(middle, out endIpOff);
if (intIP == startIp)
{
left = middle;
break;
}
if (intIP > startIp)
{
left = middle;
}
else
{
right = middle;
}
}
startIp = GetStartIp(left, out endIpOff);
endIp = GetEndIp(endIpOff, out countryFlag);
if ((startIp <= intIP) && (endIp >= intIP))
{
string local;
ipLocation.Country = GetCountry(endIpOff, countryFlag, out local);
ipLocation.Local = local;
}
else
{
ipLocation.Country = "未知";
ipLocation.Local = "";
}
return ipLocation;
}
private long GetStartIp(long left, out long endIpOff)
{
long leftOffset = firstStartIpOffset + (left * 7L);
byte[] buffer = new byte[7];
Array.Copy(data, leftOffset, buffer, 0, 7);
endIpOff = (Convert.ToInt64(buffer[4].ToString()) + (Convert.ToInt64(buffer[5].ToString()) * 0x100L)) + ((Convert.ToInt64(buffer[6].ToString()) * 0x100L) * 0x100L);
return ((Convert.ToInt64(buffer[0].ToString()) + (Convert.ToInt64(buffer[1].ToString()) * 0x100L)) + ((Convert.ToInt64(buffer[2].ToString()) * 0x100L) * 0x100L)) + (((Convert.ToInt64(buffer[3].ToString()) * 0x100L) * 0x100L) * 0x100L);
}
private long GetEndIp(long endIpOff, out int countryFlag)
{
byte[] buffer = new byte[5];
Array.Copy(data, endIpOff, buffer, 0, 5);
countryFlag = buffer[4];
return ((Convert.ToInt64(buffer[0].ToString()) + (Convert.ToInt64(buffer[1].ToString()) * 0x100L)) + ((Convert.ToInt64(buffer[2].ToString()) * 0x100L) * 0x100L)) + (((Convert.ToInt64(buffer[3].ToString()) * 0x100L) * 0x100L) * 0x100L);
}
/// <summary>
/// Gets the country.
/// </summary>
/// <param name="endIpOff">The end ip off.</param>
/// <param name="countryFlag">The country flag.</param>
/// <param name="local">The local.</param>
/// <returns>country</returns>
private string GetCountry(long endIpOff, int countryFlag, out string local)
{
string country = "";
long offset = endIpOff + 4L;
switch (countryFlag)
{
case 1:
case 2:
country = GetFlagStr(ref offset, ref countryFlag, ref endIpOff);
offset = endIpOff + 8L;
local = (1 == countryFlag) ? "" : GetFlagStr(ref offset, ref countryFlag, ref endIpOff);
break;
default:
country = GetFlagStr(ref offset, ref countryFlag, ref endIpOff);
local = GetFlagStr(ref offset, ref countryFlag, ref endIpOff);
break;
}
return country;
}
private string GetFlagStr(ref long offset, ref int countryFlag, ref long endIpOff)
{
int flag = 0;
byte[] buffer = new byte[3];

while (true)
{
//用于向前累加偏移量
long forwardOffset = offset;
flag = data[forwardOffset++];
//没有重定向
if (flag != 1 && flag != 2)
{
break;
}
Array.Copy(data, forwardOffset, buffer, 0, 3);
forwardOffset += 3;
if (flag == 2)
{
countryFlag = 2;
endIpOff = offset - 4L;
}
offset = (Convert.ToInt64(buffer[0].ToString()) + (Convert.ToInt64(buffer[1].ToString()) * 0x100L)) + ((Convert.ToInt64(buffer[2].ToString()) * 0x100L) * 0x100L);
}
if (offset < 12L)
{
return "";
}
return GetStr(ref offset);
}
private string GetStr(ref long offset)
{
byte lowByte = 0;
byte highByte = 0;
StringBuilder stringBuilder = new StringBuilder();
byte[] bytes = new byte[2];
Encoding encoding = Encoding.GetEncoding("GB2312");
while (true)
{
lowByte = data[offset++];
if (lowByte == 0)
{
return stringBuilder.ToString();
}
if (lowByte > 0x7f)
{
highByte = data[offset++];
bytes[0] = lowByte;
bytes[1] = highByte;
if (highByte == 0)
{
return stringBuilder.ToString();
}
stringBuilder.Append(encoding.GetString(bytes));
}
else
{
stringBuilder.Append((char)lowByte);
}
}
}
}
}

使用:

复制代码 代码如下:

QQWry.NET.QQWryLocator qqWry = new QQWry.NET.QQWryLocator(Server.MapPath("QQWry.Dat"));
QQWry.NET.IPLocation ip = qqWry.Query("120.67.217.7");
Response.Write(ip.IP + "<br />");
Response.Write(ip.Country + "<br />");
Response.Write(ip.Local);

结果:
 

时间: 2024-09-10 23:19:08

asp.net 纯真ip库取得所在地实现代码_实用技巧的相关文章

asp.net DZ论坛中根据IP地址取得所在地的代码_实用技巧

使用方法: 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

asp.net 读取并修改config文件实现代码_实用技巧

1. 向项目添加app.config文件: 右击项目名称,选择"添加"→"添加新建项",在出现的"添加新项"对话框中,选择"添加应用程序配置文件":如果项目以前没有配置文件,则默认的文件名称为"app.config",单击"确定".出现在设计器视图中的app.config文件为: <?xmlversion="1.0"encoding="utf-8&qu

ASP.NET自动为URL加上超链接的代码_实用技巧

作为一个程序员,在完成设计后还要根据程序的情况以及用户的反映不断对程序进行改进,这样才能不断地完善自己的作品.笔者在制作完软件商务网的论坛后,发现人们总喜欢在帖子中加上各种有用的URL链接或Email地址.而笔者当初设计时没有考虑到这一点,使得这些URL链接或Email地址只能以文字的形式而并不是以超链接的形式显示,其它浏览帖子的人还必须把这些URL链接拷贝到浏览器中或把Email地址拷贝到Outlook中才能转到相应的链接地址或发送电子邮件到相应的Email地址. 发现这个问题后,笔者即着手进

在ASP.NET2.0中通过Gmail发送邮件的代码_实用技巧

    在这里我们主要是使用Gmail,究其原因,是因为,我在使用Gmail的邮箱发送邮件的时候,遇到一小小的困难,而使用163等邮箱的时候,没遇到这个问题.     在ASP.NET2.0中,发送邮件是很简单的,我们主要使用来自命名空间System.Net.Mail中的几个类,MailMessage和SmtpClient.     核心代码是很简洁的,如下:     复制代码 代码如下:  string to = "这里填写接收者的Email地址";      string from

asp.net下中文验证码,免费开源代码_实用技巧

using System; using System.Data; using System.Configuration; using System.Collections; 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.Ht

ASP.NET返回上一页面的实现代码_实用技巧

解决方法: 程序的相关介绍 主界面:未发货订单列表(http://localhost:18888/Order/UnfilledOrdersList.aspx) 子界面:订单详细信息(http://localhost:18888/Order/ViewOrderDetail.aspx?OrderId= id,其中id的值是在主界面中被选中的那条订单信息的id) 主页面为一个订单的主要信息,GridView中有一个HyperLink控件,通过它可以跳转到"订单详细信息"界面进而查看订单的详细

asp.net session的使用与过期实例代码_实用技巧

1.Session是一种Web会话中的常用状态之一. 2.Session提供了一种把信息保存在服务器内存中的方式.他能储存任何数据类型,包含自定义对象. 3.每个客户端的Seesion是独立存储的. 4.在整个会话过程中,只要SessionID的cookie不丢失,都会保存Session信息的. 5.Session不能跨进程访问,只能由该会话的用户访问.应为提取Session数据的id标识是以Cookie的方式保存到访问者浏览器的缓存里的. 6.当会话终止,或过期时,服务器就清除Session对

Asp.net实时显示文本框字数实现代码_实用技巧

复制代码 代码如下: <script language="javascript" type="text/javascript"> function countNum() { var count=document.getElementById("txtText").value.length; document.getElementById("lblCount").value=count; } </script&

Asp.NET 生成静态页面并分页的代码_实用技巧

1.静态模板页面 template.html,主要是定义了一些特殊字符,用来被替换. 复制代码 代码如下: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" co