zoukankan      html  css  js  c++  java
  • 前端后端获取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 ){
    //移动设备
    }
                     }
  • 相关阅读:
    SpringMVC视图解析器
    JavaEE PO VO BO DTO POJO DAO 整理总结
    Android Studio JNI开发入门教程
    javah的使用
    右键“在此处打开命令行窗口”的一个小秘密
    URL和URI的区别
    自学使用
    Ribbon使用
    Eureka集群搭建
    ssm常见面试题
  • 原文地址:https://www.cnblogs.com/wanglao/p/3540735.html
Copyright © 2011-2022 走看看