ASP.NET根据IP获取省市地址

1、在网站的跟路径下面添加 QQWry.dat 文件,这个文件是IP数据库文件

2、添加以下一个类 IPScanner

C# 代码 复制

public class IPScanner
{
//私有成员#region 私有成员
private string dataPath;
private string ip;
private string country;
private string local;

private long firstStartIp = 0;
private long lastStartIp = 0;
private FileStream objfs = null;
private long startIp = 0;
private long endIp = 0;
private int countryFlag = 0;
private long endIpOff = 0;
private string errMsg = null;

//构造函数#region 构造函数
public IPScanner()
{
this.dataPath = System.Web.HttpContext.Current.Server.MapPath("/ipdata/QQWry.dat");
}

//公共属性#region 公共属性
public string DataPath
{
set { dataPath = value; }
}
public string IP
{
set { ip = value; }
}
public string Country
{
get { return country; }
}
public string Local
{
get { return local; }
}
public string ErrMsg
{
get { return errMsg; }
}

//搜索匹配数据#region 搜索匹配数据
private int QQwry()
{
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)
{
this.errMsg = "IP格式错误";
return 4;
}

long ip_Int = this.IpToInt(ip);
int nRet = 0;
if (ip_Int >= IpToInt("127.0.0.0") && ip_Int = IpToInt("0.0.0.0") && ip_Int = IpToInt("64.0.0.0") && ip_Int = IpToInt("58.0.0.0") && ip_Int this.startIp)
rangB = recNO;
else
rangE = recNO;
}
this.GetStartIp(rangB);
this.GetEndIp();
if (this.startIp = ip_Int)
{
this.GetCountry();
this.local = this.local.Replace("(我们一定要解放台湾!!!)", "");
}
else
{
nRet = 3;
this.country = "未知";
this.local = "";
}
objfs.Close();
return nRet;
}
catch
{
return 1;
}
}

// IP地址转换成Int数据#region IP地址转换成Int数据
private long IpToInt(string ip)
{
char[] dot = new char[] { ‘.‘ };
string[] ipArr = ip.Split(dot);
if (ipArr.Length == 3)
ip = ip + ".0";
ipArr = ip.Split(dot);

long ip_Int = 0;
long p1 = long.Parse(ipArr[0]) * 256 * 256 * 256;
long p2 = long.Parse(ipArr[1]) * 256 * 256;
long p3 = long.Parse(ipArr[2]) * 256;
long p4 = long.Parse(ipArr[3]);
ip_Int = p1 + p2 + p3 + p4;
return ip_Int;
}

//获取起始IP范围#region 获取起始IP范围
private long GetStartIp(long recNO)
{
long offSet = firstStartIp + recNO * 7;
//objfs.Seek(offSet,SeekOrigin.Begin);
objfs.Position = offSet;
byte[] buff = new Byte[7];
objfs.Read(buff, 0, 7);

endIpOff = Convert.ToInt64(buff[4].ToString()) + Convert.ToInt64(buff[5].ToString()) * 256 + Convert.ToInt64(buff[6].ToString()) * 256 * 256;
startIp = Convert.ToInt64(buff[0].ToString()) + Convert.ToInt64(buff[1].ToString()) * 256 + Convert.ToInt64(buff[2].ToString()) * 256 * 256 + Convert.ToInt64(buff[3].ToString()) * 256 * 256 * 256;
return startIp;
}
// 获取结束IP#region 获取结束IP
private long GetEndIp()
{
//objfs.Seek(endIpOff,SeekOrigin.Begin);
objfs.Position = endIpOff;
byte[] buff = new Byte[5];
objfs.Read(buff, 0, 5);
this.endIp = Convert.ToInt64(buff[0].ToString()) + Convert.ToInt64(buff[1].ToString()) * 256 + Convert.ToInt64(buff[2].ToString()) * 256 * 256 + Convert.ToInt64(buff[3].ToString()) * 256 * 256 * 256;
this.countryFlag = buff[4];
return this.endIp;
}
//获取国家/区域偏移量#region 获取国家/区域偏移量
private string GetCountry()
{
switch (this.countryFlag)
{
case 1:
case 2:
this.country = GetFlagStr(this.endIpOff + 4);
this.local = (1 == this.countryFlag) ? " " : this.GetFlagStr(this.endIpOff + 8);
break;
default:
this.country = this.GetFlagStr(this.endIpOff + 4);
this.local = this.GetFlagStr(objfs.Position);
break;
}
return " ";
}
//获取国家/区域字符串#region 获取国家/区域字符串
private string GetFlagStr(long offSet)
{
int flag = 0;
byte[] buff = new Byte[3];
while (1 == 1)
{
objfs.Position = offSet;
flag = objfs.ReadByte();
if (flag == 1 || flag == 2)
{
objfs.Read(buff, 0, 3);
if (flag == 2)
{
this.countryFlag = 2;
this.endIpOff = offSet - 4;
}
offSet = Convert.ToInt64(buff[0].ToString()) + Convert.ToInt64(buff[1].ToString()) * 256 + Convert.ToInt64(buff[2].ToString()) * 256 * 256;
}
else
{
break;
}
}
if (offSet 127)
{
upC = (byte)objfs.ReadByte();
buff[0] = lowC;
buff[1] = upC;
System.Text.Encoding enc = System.Text.Encoding.GetEncoding("GB2312");
str += enc.GetString(buff);
}
else
{
str += (char)lowC;
}
}
return str;
}

public string IPLocation(string ip)
{
this.ip = ip;
this.QQwry();
if (this.country.Trim().Contains(this.local.Trim()))
return this.country.Trim();
return this.country.Trim() + this.local.Trim();
}

}

3、ASP.NET根据IP获取省市地址的调用

C# 代码 复制

string ip="123.81.176.99";
IPScanner ipscaner = new IPScanner();
ipscaner.IPLocation(ip);
string strIPAddress=ipscaner.Country;

时间: 2024-10-13 11:28:01

ASP.NET根据IP获取省市地址的相关文章

C# 根据IP获取省市

/// <summary> /// 根据IP获取省市 /// </summary> public void GetAddressByIp() { string ip = "115.193.217.249"; string PostUrl = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip=" + ip; string res = GetDataByPost(PostUrl);//该

百度地图服务通过ip获取具体地址

//获取经纬度 var get="http://api.map.baidu.com/location/ip?ip=你的ip&ak=你的key&coor=bd09ll" 通过上面的接口可以获取IP的经纬度 //获取地址 var getaddres="http://api.map.baidu.com/geocoder/v2/?callback=renderReverse&location=上面的到的经纬度&output=json&pois=

java根据本地Ip获取mac地址

1 import java.net.InetAddress; 2 import java.net.NetworkInterface; 3 import java.net.SocketException; 4 import java.net.UnknownHostException; 5 6 public class MacAddress { 7 8 /** 9 * @param args 10 * @throws UnknownHostException 11 * @throws SocketE

asp微信公众号获取用户地址信息源码,可以获取省市县及坐标

============================================= 互相学习电话微信:18611436777(加微信注明事由) <!--#include file="Function.asp" --><% timestamp = Datediff("s","1970-01-01 00:00:00",now) nonceStr = MakeRandStr(32) signature = Get_Sign()

ASP.NET MVC中获取URL地址参数的两种写法

一.url地址传参的第一种写法 1.通过mvc中默认的url地址书写格式:控制器/方法名/参数 2.实例:http://localhost:39270/RequestDemo/Index/88,默认参数名为id所以名称为id. 如果使用其他名称,后台是无法读取的会报错 二.url地址传参的第二种写法 1.使用?加参数名=参数值的写法,如果有多个参数使用&来连接 http://localhost:39270/RequestDemo/Index?id=88&name=%E5%BC%A0%E4%

如何通过IP获取MAC地址?如何通过mac来获取IP地址?

MAC的地址也可以修改的哦!!! 原文地址:https://blog.51cto.com/12131824/2356211

android获取Mac地址和IP地址

获取Mac地址实际项目中测试了如下几种方法:(1)设备开通Wifi连接,获取到网卡的MAC地址(但是不开通wifi,这种方法获取不到Mac地址,这种方法也是网络上使用的最多的方法) //根据Wifi信息获取本地Mac public static String getLocalMacAddressFromWifiInfo(Context context){ WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_

java获取mac地址-屏蔽ip封mac地址

首先要说的是:可以支持外网机器的mac地址获取.  以前弄了一个只能访问局域网. 有防火墙就访问不了, 但是这个不用担心了. 测试了百度的ip,已经可以获得mac地址 测试效果图: java通过ip获取mac地址-封ip封mac地址 版权声明:本文为博主http://www.zuiniusn.com原创文章,未经博主允许不得转载.

Android获取Ethernet、WIFI的ip和mac地址

/**  * 获取本地ip  * @return  */ private String getLocalIpAddress() { try { String ipv4 = null; List<NetworkInterface> nilist = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface ni : nilist) { List<InetAddress> ialis