得到IP地址的來源,資料庫動網純真ip資料庫mdb

來源:互聯網
上載者:User
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 != 4) return 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;
        }
    } 

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.