根据ip定位到你的城市

 if(!string.IsNullOrEmpty(HttpContext.Request.ServerVariables["HTTP_VIA"]))

IsNullOrEmpty:判断字符串是空引用,或者值为空。

HTTP_VIA:代理服务器

HttpContext.Request.ServerVariables: https://blog.csdn.net/linshichen/article/details/52786701
      {
        ip=HttpContext.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();

HTTP_X_FORWARDED_FOR:获取到的ip地址    https://blog.csdn.net/ccfxue/article/details/73693477
      }else if(!string.IsNullOrEmpty(HttpContext.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]))
      {
         ip=HttpContext.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
      }
      else{
        ip = HttpContext.Request.ServerVariables["REMOTE_ADDR"].ToString();

REMOTE_ADDR:  您的ip;


      }
//根据ip地址定位当前市
     function  newip(){
        var platBaseEntity = PlatBaseRepository.Entities.FirstOrDefault();
        url = string.Empty;
        if(ip != "::1" &&ip!="127.0.0.1"){
            url = "http://apis.map.qq.com/ws/location/v1/ip?ip=" + ip + "&key" + platBaseEntity.LbsKey +"";
        }else{
            url = "http://apis.map.qq.com/ws/location/v1/ip?key=" + platBaseEntity.LbsKey +"";
        }
          strResponse = strResponse.Substring(strResponse.IndexOf("\"city\":")+9).Split(new char[] {'\"'})[0];
          return strResponse;

     }
 

猜你喜欢

转载自blog.csdn.net/xunhuanxiaogongzhu/article/details/82152488
今日推荐