C#根据IP地址获取城市

来源:互联网 发布:讯飞输入法皮肤知乎 编辑:程序博客网 时间:2024/06/12 01:48
protected void Page_Load(object sender, EventArgs e)
        {
            string ipAddress = Request.UserHostAddress;
            string url = "http://www.ip138.com/ips138.asp?ip="+ipAddress+"&action=2";
            string pagecode = getPageCode(url, Encoding.GetEncoding("gb2312"));//getPageCode方法获取页面源码
            string str = "^.*</li><li>参考数据一:(?<content>.+?)</li></ul>.*$";//正则表达式
            Regex rgClass = new Regex(str, RegexOptions.Singleline);
            Match match = rgClass.Match(pagecode);
            string a = match.Groups["content"].Value;
            Response.Write(a); 

        }

getPageCode方法在上一个博客里。

原创粉丝点击