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

ContractedBlock.gif ExpandedBlockStart.gif 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;
        }
    } 

}

转载于:https://www.cnblogs.com/pwqzc/archive/2008/11/12/1332136.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值