国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

【ASP.NET進(jìn)階】根據(jù)IP地址返回對(duì)應(yīng)位置信息

2019-11-14 14:32:24
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

其實(shí)就是使用了百度的ip庫(kù)的功能接口,然后處理下就行了,效果圖如下:

準(zhǔn)備工作:

1.注冊(cè)成為開(kāi)度開(kāi)發(fā)者,創(chuàng)建應(yīng)用獲得百度API調(diào)用的AK秘鑰,百度開(kāi)發(fā)中心地址: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.準(zhǔn)備開(kāi)發(fā)咯,步驟如下:

(1)獲取客戶(hù)端IP

        /// <summary>獲取客戶(hù)端IP地址(無(wú)視代理)</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)獲取編碼服務(wù)地址

        private const string THE_KEY = "***************"; //你的AK密鑰        /// <summary>返回UTF-8編碼服務(wù)地址</summary>        /// <returns>服務(wù)地址</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)通過(guò)服務(wù)地址,得到地址解析結(jié)果

        /// <summary> 返回結(jié)果(地址解析的結(jié)果) </summary>        public static string GetInfoByUrl(string url)        {            //調(diào)用時(shí)只需要把拼成的URL傳給該函數(shù)即可。判斷返回值即可            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)最后簡(jiǎn)單的調(diào)用上面的方法,然后轉(zhuǎn)換下字符串中的轉(zhuǎn)義字符

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

 

 

測(cè)試地址: http://ycdoit.com/test/getiptoad.aspx


發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 闽清县| 吴堡县| 武城县| 南投县| 富阳市| 曲麻莱县| 绥芬河市| 重庆市| 西充县| 新乡县| 江西省| 合肥市| 视频| 刚察县| 特克斯县| 宁夏| 汝阳县| 宁陵县| 屏东县| 长丰县| 嵩明县| 栖霞市| 青海省| 阿拉尔市| 广水市| 郁南县| 平顺县| 元氏县| 涡阳县| 德格县| 出国| 温宿县| 营口市| 元氏县| 兴山县| 湖南省| 韶关市| 商南县| 南城县| 封丘县| 泰和县|