博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
获取用户IP 查找所在城市
阅读量:4552 次
发布时间:2019-06-08

本文共 3148 字,大约阅读时间需要 10 分钟。

一、获取IP常用方法

public class BaseAPIController : ApiController    {        ///  获取客户端访问IP        ///         protected string CurrentUserIP        {            get            {                string realRemoteIP = "";                if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)                {                    realRemoteIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].Split(',')[0];                }                if (string.IsNullOrEmpty(realRemoteIP))                {                    realRemoteIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];                }                if (string.IsNullOrEmpty(realRemoteIP))                {                    realRemoteIP = System.Web.HttpContext.Current.Request.UserHostAddress;                }                return realRemoteIP;            }        }    }
View Code

 

1、返回需要的参数类 position

[Serializable]    public class Position    {        public string address { get; set; }        public Content content { get; set; }        public int status { get; set; }    }    public class Content    {        public string address { get; set; }        public Address_detail address_detail { get; set; }        public Point point { get; set; }    }    public class Address_detail    {         public string city { get; set; }        public int city_code { get; set; }        public string district { get; set; }        public string province { get; set; }        public string street { get; set; }        public string street_number { get; set; }    }    public class Point    {        public string x { set; get; }        public string y { set; get; }    }
View Code

2、调取接口接受返回数值

WebClient wc = new WebClient();                string jsonStr = wc.DownloadString("http://api.map.baidu.com/location/ip?ak=8aa60008a5478bc272e4cb098567a382&ip=" + Common.Common.GetUserIp() + "");                Position p = new Position();                try                {                    JavaScriptSerializer jss = new JavaScriptSerializer();                    p = (Position)jss.Deserialize(jsonStr, typeof(Position));                }                catch (Exception e)                {                    ViewBag.ID = 3270;                    ViewBag.Name = "北京";                }                if (p.status != 0 || p.content.address_detail.city == "")//出错                {                    ViewBag.ID = 3270;                    ViewBag.Name = "北京";                }                else                {                    string cityName = p.content.address_detail.city;                    int cityIndex = Bu.AreaBLL.GetCityIndexByName(cityName,2);                    if (cityIndex == 0)//数据库没有该城市                    {                        ViewBag.ID = 3270;                        ViewBag.Name = "北京";                    }                    else                    {                        ViewBag.ID = cityIndex;                        ViewBag.Name = p.content.address_detail.city.Replace("市", "");                    }                }
View Code

转载于:https://www.cnblogs.com/eric-gms/p/4054075.html

你可能感兴趣的文章
android状态机机制StateMachine
查看>>
滚动条自适应宽度的问题
查看>>
第二次作业——个人项目实战
查看>>
HighCharts图表控件在ASP.NET WebForm中的使用
查看>>
C#汉字转拼音
查看>>
Remote Service 和 Local App的交互
查看>>
mysql删除重复数据
查看>>
文件下载工具类
查看>>
Python 定义自己的常量类
查看>>
C++读取文本文件
查看>>
Python 字典排序
查看>>
sql中写标量函数生成大写拼音首字母
查看>>
ASP.NET Core 2.1 : 十五.图解路由(2.1 or earler)
查看>>
服务器返回状态码说明
查看>>
GitHub for Windows提交失败“failed to sync this branch”
查看>>
linux 安装 git
查看>>
Margin
查看>>
完成登录与注册页面的前端
查看>>
centos 源码安装php7
查看>>
Log4j详细教程
查看>>