得到IP地址的来源,数据库动网纯真ip数据库mdb

Code
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.Data.SqlClient;
using System.Data.OleDb;

namespace hzxltz
{
    
public class ip
    {
        
//根据IP地址获得对应的城市
        public static string GetIpRealWorldAddress(string ipAddress)
        {
            
if (!IpAddressAvailable(ipAddress))
            {
                
return "ip地址有问题";
            }
            
//long value = GetIPCount(ipAddress);
            long value = Dot2LongIP(ipAddress);
            
string ret = "";
            
string Sql = "select top 1 country,city from dv_address where ip1<=@startid and ip2 >=@startid";
            
string connString = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=";
            connString 
+= System.Web.HttpContext.Current.Server.MapPath("~/ipaddress.mdb");
            OleDbConnection conn 
= new OleDbConnection(connString);
            OleDbCommand comm 
= new OleDbCommand(Sql, conn);
            comm.Parameters.AddWithValue(
"@startid",Convert.ToDouble(value));
            conn.Open();
            OleDbDataReader dr 
= comm.ExecuteReader();
            
if (dr.HasRows)
            {
                dr.Read();
                ret 
= dr["country"].ToString()+dr["city"].ToString();
            }
            
else
            {
                ret 
= "不可识别的IP";
            }
            conn.Close();
            
return ret;
        }
        
        
//取得ip的long值 3.254.255.255 = 3*256^3 + 254 *256^2 
        public static long GetIPCount(string ipAddress)
        {
            ipAddress 
= ipAddress.Trim();
            
string[] ipSecs = ipAddress.Split('.');
            
long value = 0;
            
for (int i = 0; i < 4; i++)
            {
                
int ipSecDec = int.Parse(ipSecs[i]);
                
int power = 3-i;
                
long ipSecValue = (long)( ipSecDec * Math.Pow(256, power));
                value 
= value + ipSecValue;
            }
            
//value = value + 1;
            return value;
        }

        
public static long Dot2LongIP(string dotIP)
        {
            
string[] subIP = dotIP.Split('.');
            
//IP Address = w.x.y.z 
            
//IP Number = 16777216 * w + 65536 * x + 256 * y + z  
            long ip = 16777216 * Convert.ToInt64(subIP[0]) + 65536 * Convert.ToInt64(subIP[1]) + 256 * Convert.ToInt64(subIP[2]) + Convert.ToInt64(subIP[3]);
            
return ip;
        }


        
/// <summary>
        
/// 判断ip地址是否有问题  1 地址段数, 地址段数里面是否是数字,数字是否在 0-255范围内
        
/// 从以上三个方面监测
        
/// </summary>
        
/// <param name="ipAddress"></param>
        
/// <returns></returns>
        private static bool IpAddressAvailable(string ipAddress)
        {
            ipAddress 
= ipAddress.Trim();
            
string[] ipSecs =  ipAddress.Split('.');
            
if (ipSecs.Length != 4return false;
         
            
//如果每个段都可以转为int则返回真
            for (int i = 0; i < ipSecs.Length; i++)
            {
                
try
                {
                    
int ipSecDec = int.Parse(ipSecs[i]);
                    
if (ipSecDec < 0 || ipSecDec > 255)
                    {
                        
return false;
                    }
                }
                
catch
                {
                    
return false;
                }
             }
            
return true;
        }
    } 

}

你可能感兴趣的:(得到IP地址的来源,数据库动网纯真ip数据库mdb)