前端后端獲取ip以及位置以及判斷客戶端


ip

后端獲取ip方法:

      private string GetIp()
        {
            string ip = "";
            
            if (Context.Request.ServerVariables["HTTP_VIA"] != null)// 服務器變量, using proxy
            {
                ip = Context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
            }
            if (ip == "" || ip == null)//如果沒有使用代理服務器或者得不到客戶端的ip  
            {                            //得到服務端的地址    

                ip = Context.Request.ServerVariables["REMOTE_ADDR"].ToString(); //While it can't get the Client IP, it will return proxy IP.


                if (ip == "" || ip == null)//如果沒有使用代理服務器或者得不到客戶端的ip  not using proxy or cant get the Client IP
                {
                    string strHostName = System.Net.Dns.GetHostName();

                    string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();
                }

                if (ip == "" || ip == null)
                {
                    ip = HttpContext.Current.Request.UserHostAddress;
                }

            }
            return ip;
        }    

前端就別瞎折騰了讀取ip實在麻煩直接調用新浪接口

http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js

使用得到的ip獲取位置信息

前端

 

 var taobaourl = 'http://ip.taobao.com/service/getIpInfo.php?ip=' + ip;

        $.getJSON(taobaourl, function (data) {

            alert("區域:" + data.data.area);
            alert("詳細:" + data.data.region + data.data.city);
            if (data.data.isp != null && data.data.isp!="") {
                alert("所屬運營商" + data.data.isp);
            }
          
        });

 

后端:

   private static data GetIpInfo(string ip)
        {
            // 填充Class1.cs相關的IpInfo數據
            IpInfo wInfo = null;
            try
            {
                // 訪問阿里ip分析站
                string analyzerUrl = "http://ip.taobao.com/service/getIpInfo.php?ip="+ip;

                //獲取返回的json數據。
                string jsonData = GetRequestData(analyzerUrl);

                // 根據返回的Json數據反系列化自定義好的對象
                JsonSerializer jsser=new JsonSerializer();

                wInfo= JsonConvert .DeserializeObject<IpInfo>(jsonData);
            
            }
            catch (Exception)
            {
            }
            return wInfo.data;
        }

這樣位置信息就在wInfo這個實例中了。

判斷客戶端:

前端

var user_agent = navigator.userAgent;

    var is_iPd = user_agent.match(/(iPad|iPod|iPhone)/i) != null;
    //經驗證
    var is_mobi = user_agent.toLowerCase().match(/(nokia|sony|samsung|lenovo|wap|mobile|android|iphone)/i) != null;

    var is_pc = user_agent.toLowerCase().match(/trident|windows/i) != null;
    if (is_pc) {
        alert('這是電腦');
}else{
        if(is_mobi )  {alert('這是手機');}
}

后端:

string osPat = "nokia|sony|samsung|lenovo|wap|mobile|android|iphone";

                string pcPat = "trident|windows";
                string uAgent = Request.ServerVariables["HTTP_USER_AGENT"].ToLower();
                Regex reg = new Regex(osPat);
                Regex regPc = new Regex(pcPat);
                if (regPc.IsMatch(uAgent)){
                //pc  
}else{
if(osPat ){
//移動設備
}
                 }

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM