• 根据IP获取省市 .


    public class IPAddress
        {
            /// <summary>       
            /// 得到真实IP以及所在地详细信息(Porschev)       
            /// </summary>       
            /// <returns></returns>       
            public string GetIpDetails()
            {
                string url = "http://www.ip138.com/ips8.asp";   //设置获取IP地址和国家源码的网址         
                string regStr = "(?<=<td\s*align=\"center\">)[^<]*?(?=<br/><br/></td>)";
                string ipRegStr = "((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)";    //IP正则                  
                string ip = string.Empty;   //IP地址           
                string country = string.Empty;  //国家           
                string adr = string.Empty;   //省市           
                string html = GetHtml(url);       //得到网页源码           
                Regex reg = new Regex(regStr, RegexOptions.None);
                Match ma = reg.Match(html); html = ma.Value;
                Regex ipReg = new Regex(ipRegStr, RegexOptions.None);
                ma = ipReg.Match(html);
                ip = ma.Value;     //得到IP           
                int index = html.LastIndexOf("") + 1;
                country = html.Substring(index);                   //得到国家           
                adr = GetAdrByIp(ip);
                return "IP:" + ip + "  国家:" + country + "  省市:" + adr;
            }
            /// <summary>       
            /// 通过IP得到IP所在地省市(Porschev)       
            /// </summary>       
            /// <param name="ip"></param>       
            /// <returns></returns>       
            public string GetAdrByIp(string ip)
            {
                string url = "http://www.cz88.net/ip/?ip=" + ip;
                string regStr = "(?<=<span\s*id=\"cz_addr\">).*?(?=</span>)";
                string html = GetHtml(url);       //得到网页源码           
                Regex reg = new Regex(regStr, RegexOptions.None);
                Match ma = reg.Match(html);
                html = ma.Value;
                string[] arr = html.Split(' ');
                return arr[0];
            }
    
         
            /// <summary>       
            /// 获取HTML源码信息(Porschev)       
            /// </summary>       
            /// <param name="url">获取地址</param>       
            /// <returns>HTML源码</returns>       
            public string GetHtml(string url)
            {
                string str = "";
                try
                {
                    Uri uri = new Uri(url);
                    WebRequest wr = WebRequest.Create(uri);
                    Stream s = wr.GetResponse().GetResponseStream();
                    StreamReader sr = new StreamReader(s, Encoding.Default);
                    str = sr.ReadToEnd();
                }
                catch (Exception e)
                {
                }
                return str;
            }
        }
  • 相关阅读:
    数据库用户管理
    数据库(视图,事件,触发器,函数,存储,变量)
    我的博客即将同步至腾讯云+社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=3cp8ng15g94wc
    Python3安装与使用urllib2包之小坑
    移动端的vw px rem之间换算
    H5移动端开发
    实现拖拽复制和可排序的react.js组件
    vue2实现搜索结果中的搜索关键字高亮
    来,了解一下Java内存模型(JMM)
    video标签学习使用
  • 原文地址:https://www.cnblogs.com/jkyweb/p/3446074.html
Copyright © 2020-2023  润新知