• 前端后端获取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 ){
    //移动设备
    }
                     }
  • 相关阅读:
    FXBlurView用法
    NSAttributedString用法
    SDWebImage的使用
    UITextField 对输入金额的约束
    Delphi获取计算机名
    delphi下实现控制其它窗体中的控件[转]
    根据版本的不同整理所有的绿色SQL Server[转]
    点击TWebBrowser中的超级链接不在新的IE窗口打开
    Delphi 快速获取本机的IP地址[转]
    Delphi下如何使程序在Win7/Vista上用管理员权限运行[转]
  • 原文地址:https://www.cnblogs.com/wanglao/p/3540735.html
Copyright © 2020-2023  润新知