星空网 > 软件开发 > ASP.net

【ASP.NET 进阶】根据IP地址返回对应位置信息

其实就是使用了百度的IP库的功能接口,然后处理下就行了,效果图如下:

【ASP.NET 进阶】根据IP地址返回对应位置信息

准备工作:

1.注册成为开度开发者,创建应用获得百度API调用的AK秘钥,百度开发中心地址:http://developer.baidu.com/

2.API 地址: http://developer.baidu.com/map/index.php?title=webapi/ip-api#.E4.BD.BF.E7.94.A8.E6.96.B9.E6.B3.95

3.准备开发咯,步骤如下:

(1)获取客户端IP

    /// <summary>获取客户端IP地址(无视代理)</summary>    /// <returns>若失败则返回回送地址</returns>    public static string GetHostAddress()    {      string userHostAddress = HttpContext.Current.Request.UserHostAddress;      if (string.IsNullOrEmpty(userHostAddress))      {        userHostAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];      }      //最后判断获取是否成功,并检查IP地址的格式(检查其格式非常重要)      if (!string.IsNullOrEmpty(userHostAddress) && IsIP(userHostAddress))      {        return userHostAddress;      }      return "127.0.0.1";    }    /// <summary>    /// 检查IP地址格式    /// </summary>    /// <param name="ip"></param>    /// <returns></returns>    public static bool IsIP(string ip)    {      return System.Text.RegularExpressions.Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");    

(2)获取编码服务地址

    private const string THE_KEY = "***************"; //你的AK密钥    /// <summary>返回UTF-8编码服务地址</summary>    /// <returns>服务地址</returns>    public string GetPostUrl(string theIP)    {      string postUrl = "http://api.map.baidu.com/location/ip?ak=" + THE_KEY + "&ip=" + theIP + "&coor=bd09ll";      return postUrl;    }

(3)通过服务地址,得到地址解析结果

    /// <summary> 返回结果(地址解析的结果) </summary>    public static string GetInfoByUrl(string url)    {      //调用时只需要把拼成的URL传给该函数即可。判断返回值即可      string strRet = null;      if (url == null || url.Trim().ToString() == "")      {        return strRet;      }      string targeturl = url.Trim().ToString();      try      {        HttpWebRequest hr = (HttpWebRequest)WebRequest.Create(targeturl);        hr.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";        hr.Method = "GET";        hr.Timeout = 30 * 60 * 1000;        WebResponse hs = hr.GetResponse();        Stream sr = hs.GetResponseStream();        StreamReader ser = new StreamReader(sr, Encoding.Default);        strRet = ser.ReadToEnd();      }      catch (Exception ex)      {        strRet = null;      }      return strRet;    }

(4)最后简单的调用上面的方法,然后转换下字符串中的转义字符

        string theIP = YMethod.GetHostAddress();        string msg = YMethod.GetInfoByUrl(GetPostUrl(theIP));        msg = System.Text.RegularExpressions.Regex.Unescape(msg);        Response.Write(msg);

 

 

测试地址: http://ycdoit.com/test/getiptoad.aspx




原标题:【ASP.NET 进阶】根据IP地址返回对应位置信息

关键词:ASP.NET

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

货物运输到越南:https://www.goluckyvip.com/tag/104648.html
中国到越南海运费:https://www.goluckyvip.com/tag/104649.html
Shopee知识局:https://www.goluckyvip.com/tag/10465.html
陆运物流到越南:https://www.goluckyvip.com/tag/104650.html
越南 货运:https://www.goluckyvip.com/tag/104651.html
越南散货船:https://www.goluckyvip.com/tag/104652.html
在古巴做游轮 古巴旅游项目:https://www.vstour.cn/a/363194.html
西藏旅游攻略自驾游需要多少天 去西藏旅游自驾游要多久时间:https://www.vstour.cn/a/363195.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流