急!如何根据ip地址取得所在城市的信息?
获得ip地址以后,如何取得该ip地址的信息呢?是通过ip库对照呢,还是有其他的办法,如果是ip库,请提供一个,谢谢!
环境,winform和asp。net页面
------解决方案--------------------帮你顶
------解决方案--------------------using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace ShowIP
{
class IPReader
{
static private string _dataPath = "./QQWry.Dat ";
static private string country;
static private string local;
static private long firstStartIp=0;
static private long lastStartIp=0;
static private FileStream objfs = null;
static private long startIp=0;
static private long endIp=0;
static private int countryFlag=0;
static private long endIpOff=0;
#region 搜索匹配数据
static private int QQwry(string ip)
{
string pattern = @ "(((\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])) ";
Regex objRe = new Regex(pattern);
Match objMa = objRe.Match(ip);
if(!objMa.Success)
{
return 4;
}
long ip_Int = IpToInt(ip);
int nRet=0;
if(ip_Int> =IpToInt( "127.0.0.0 ")&&ip_Int <=IpToInt( "127.255.255.255 "))
{
country= "本机内部环回地址 ";
local= " ";
nRet=1;
}
else if((ip_Int> =IpToInt( "0.0.0.0 ")&&ip_Int <=IpToInt( "2.255.255.255 "))||(ip_Int> =IpToInt( "64.0.0.0 ")&&ip_Int <=IpToInt( "126.255.255.255 "))||(ip_Int> =IpToInt( "58.0.0.0 ")&&ip_Int <=IpToInt( "60.255.255.255 ")))
{
country= "网络保留地址 ";
local= " ";
nRet=1;
}
try
{
objfs = new FileStream(_dataPath, FileMode.Open, FileAccess.Read);
//objfs.Seek(0,SeekOrigin.Begin);
objfs.Position=0;
byte[] buff = new Byte[8] ;
objfs.Read(buff,0,8);
firstStartIp=buff[0]+buff[1]*256+buff[2]*256*256+buff[3]*256*256*256;
lastStartIp=buff[4]*1+buff[5]*256+buff[6]*256*256+buff[7]*256*256*256;
long recordCount=Convert.ToInt64((lastStartIp-firstStartIp)/7.0);
if(recordCount <=1)
{
country= "FileDataError ";
objfs.Close();
return 2;
}
long rangE=recordCount;
long rangB=0;
long recNO=0;
while(rangB <rangE-1)
{
recNO=(rangE+rangB)/2;
GetStartIp(recNO);
if(ip_Int==startIp)
{
rangB = recNO;
break;
}