.net core mvc获取IP地址和IP所在地(其实是百度的)


   演示地址:亚丁号---IP地址查询

获取IP就得链接互联网

String ip = Request.HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString();

获取ip所在地因此玩了个技巧

        public static string getIpAddress(String ip) {
            string url ="https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query=" ip "&co=&resource_id=6006&t=1551419352431&ie=utf8&oe=gbk&cb=op_aladdin_callback&format=json&tn=baidu&cb=jQuery110200929818675063574_1551418734860&_=1551418734868";
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
            req.Method = "GET";
            using (WebResponse wrp = req.GetResponse())
            {
                //本地局域网","titlecont":"IP地址查询","origip":"192.168.107.125","origipquery":"192.168.107.125","showlamp":"1","showLikeShare":1,"shareImage":1,"ExtendedLocation":"","OriginQuery":"192.168.107.125","tplt":"ip","resourceid":"6006","fetchkey":"192.168.107.125","appinfo":"","role_id":0,"disp_type":0}]});
                string html = new StreamReader(wrp.GetResponseStream(), Encoding.GetEncoding("gb2312")).ReadToEnd();
                string ipaddress= html.Split("location":"")[1];
                string aaa = ipaddress.Split(",")[0];
                //在这里对接收到的页面内容进行处理.Split('location":"')[1].split('","titlecont')[0]
                return aaa.Replace(""", "");
            }

        }

演示地址:亚丁号---IP地址查询

   演示地址:亚丁号---IP地址查询

获取IP就得链接互联网

String ip = Request.HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString();

获取ip所在地因此玩了个技巧

        public static string getIpAddress(String ip) {
            string url ="https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query=" ip "&co=&resource_id=6006&t=1551419352431&ie=utf8&oe=gbk&cb=op_aladdin_callback&format=json&tn=baidu&cb=jQuery110200929818675063574_1551418734860&_=1551418734868";
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
            req.Method = "GET";
            using (WebResponse wrp = req.GetResponse())
            {
                //本地局域网","titlecont":"IP地址查询","origip":"192.168.107.125","origipquery":"192.168.107.125","showlamp":"1","showLikeShare":1,"shareImage":1,"ExtendedLocation":"","OriginQuery":"192.168.107.125","tplt":"ip","resourceid":"6006","fetchkey":"192.168.107.125","appinfo":"","role_id":0,"disp_type":0}]});
                string html = new StreamReader(wrp.GetResponseStream(), Encoding.GetEncoding("gb2312")).ReadToEnd();
                string ipaddress= html.Split("location":"")[1];
                string aaa = ipaddress.Split(",")[0];
                //在这里对接收到的页面内容进行处理.Split('location":"')[1].split('","titlecont')[0]
                return aaa.Replace(""", "");
            }

        }

演示地址:亚丁号---IP地址查询

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在