public HttpResponseMessage SaveData([FromUri] EnquirySavaQuery query) { string host_ip = Request.GetClientIpAddress(); string basePath = System.AppDomain.CurrentDomain.BaseDirectory;//纯真IP数据文件路径.. QQWry.NET.QQWryLocator2 qqWry2 = new QQWry.NET.QQWryLocator2(basePath + "\\Models\\QQWry.dat"); QQWry.NET.IPLocation ip2 = qqWry2.Query(host_ip); //查询一个IP地址 string country = ip2.Country; string email = query.email; string content = query.content; string productUrl = query.productUrl; //信息的来源页面,直接抓取 string productName = query.productName; //可更改的产品地址 string yourName = query.yourName; string company = query.company; string tel = query.tel; string msn = query.msn; string language = query.language; //访问语言 string recievedId = query.recievedId; //接受者 EnquiryService.EnquirySave(host_ip, email, content, productUrl, productName, yourName, company, tel, msn, language, country, recievedId); List <string> retList = new List <string>(); var returnObj = new ResultObject <List <string> >(); retList.Add("leave message success!"); returnObj.ReturnData = retList; returnObj.Status = ServerStatus.SaveSuccess; return(Request.CreateResponse <ResultObject <List <string> > >(HttpStatusCode.OK, returnObj)); }
/// <summary> /// IP地址转换为地理位置 /// </summary> /// <param name="ipstr"></param> /// <returns></returns> public static string GetIPQQWryLocator(string ipstr) { string result = string.Empty; try { QQWry.NET.QQWryLocator qqWry = new QQWry.NET.QQWryLocator(SimonUtils.GetMapPath("/App_Data/qqwry.dat")); QQWry.NET.IPLocation ipl = qqWry.Query(ipstr); result = string.Format("{0} {1} {2}", ipl.IP, ipl.Country, ipl.Local); } catch { result = "error"; } return(result); }
static void Main(string[] args) { try { QQWryLocator qqWry = new QQWryLocator("qqwry.dat"); //初始化数据库文件,并获得IP记录数,通过Count可以获得 QQWry.NET.IPLocation ip = qqWry.Query("183.11.15.137"); //查询一个IP地址 Console.WriteLine("{0} {1} {2}", ip.IP, ip.Country, ip.Local); Stopwatch stopwatch = new Stopwatch(); List <string> ips = new List <string> { "218.5.3.128", "120.67.217.7", "125.78.67.175", "220.250.64.23", "218.5.3.128", "120.67.217.7", "125.78.67.175", "183.11.15.137" }; stopwatch.Start(); for (int i = 0; i < 100; i++) { foreach (string item in ips) { ip = qqWry.Query(item); // Console.WriteLine("{0} {1} {2}", ip.IP, ip.Country, ip.Local); } } stopwatch.Stop(); Console.WriteLine("查询了800次IP,QQWryLocator 花了{0} ms", stopwatch.ElapsedMilliseconds); stopwatch.Reset(); stopwatch.Start(); for (int i = 0; i < 100; i++) { foreach (string item in ips) { string s = IPLocation.IPLocation.IPLocate("qqwry.dat", item); // Console.WriteLine(s); } } stopwatch.Stop(); Console.WriteLine("查询了800次IP,IPLocation 花了{0} ms", stopwatch.ElapsedMilliseconds); } catch (Exception e) { Console.WriteLine(e.Message); } Console.Read(); }
public IPLocation Query(string ip) { if (!regex.Match(ip).Success) { throw new ArgumentException("IP格式错误"); } IPLocation ipLocation = new 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); }
public IPLocation Query(string ip) { IPAddress address = IPAddress.Parse(ip); if (address.AddressFamily != AddressFamily.InterNetwork) { //throw new ArgumentException("不支持非IPV4的地址"); return(new IPLocation() { IP = ip, Country = "未知", Local = string.Empty }); } if (IPAddress.IsLoopback(address)) { return(new IPLocation() { IP = ip, Country = "本机内部环回地址", Local = string.Empty }); } uint intIP = (uint)IPAddress.HostToNetworkOrder((int)address.Address); //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")))) //if (intIP <= 50331647 || (intIP >= 1073741824 && intIP <= 2130706431) || (intIP >= 973078528 && intIP <= 1023410175)) //{ // return new IPLocation() { IP = ip, Country = "网络保留地址", Local = string.Empty }; //} IPLocation ipLocation = new IPLocation() { IP = ip }; uint right = (uint)ipCount; uint left = 0; uint middle = 0; uint startIp = 0; uint endIpOff = 0; uint endIp = 0; int countryFlag = 0; while (left < (right - 1)) { middle = (right + left) / 2; 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 = string.Empty; } return(ipLocation); }
public IPLocation Query(string ip) { if (!regex.Match(ip).Success) { throw new ArgumentException("IP格式错误"); } IPLocation ipLocation = new 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; }