根据ip获取城市名

本文介绍了一种在ASP.NET应用中获取客户端真实IP地址的方法,并通过两个不同的在线服务解析IP地址来获取其对应的地理位置信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 protected void Page_Load(object sender, EventArgs e)
    {
        //获取ip
        if (Context.Request.ServerVariables["HTTP_VIA"] != null) // using proxy
        {
            ip.Text = Context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();  // Return real client IP.               
        }
        else// not using proxy or can't get the Client IP
        {
            ip.Text = Context.Request.ServerVariables["REMOTE_ADDR"].ToString(); //While it can't get the Client IP, it will return proxy IP.
        }
        //   GetIpAddress(ip.Text);
        GetIpAddress(ip.Text,0);
        GetCitycode(address.Text);
    }
    //根据ip获取所在的城市
    private void GetIpAddress(string ip)
    {
        string url = " http://www.ip138.com/ips138.asp?ip=" + ip + "&action=2";//210.51.45.99
        string pageAll = GetHtml(url, Encoding.Default);
        System.Text.RegularExpressions.Regex re = new System.Text.RegularExpressions.Regex("<li>本站主数据:(.*?)</li>", System.Text.RegularExpressions.RegexOptions.None);
        System.Text.RegularExpressions.MatchCollection mc = re.Matches(pageAll);
        string link = string.Empty;
        foreach (System.Text.RegularExpressions.Match ma in mc)
        {
            address .Text= ma.Groups[1].Value;
        }
    }
    //根据ip获取所在的城市
    private void GetIpAddress(string ip)
    {
        string url = " http://www.youdao.com/smartresult-xml/search.s?type=ip&q=" + "210.51.45.99";//210.51.45.99
        string pageAll = GetHtml(url, Encoding.Default);
        System.Text.RegularExpressions.Regex re = new System.Text.RegularExpressions.Regex("<location>(.*?)</location>", System.Text.RegularExpressions.RegexOptions.None);
        System.Text.RegularExpressions.MatchCollection mc = re.Matches(pageAll);
        string link = string.Empty;
        foreach (System.Text.RegularExpressions.Match ma in mc)
        {
            address.Text = ma.Groups[1].Value;
        }
    }
    private string GetHtml(string url, Encoding encoding)
    {
        System.Net.HttpWebResponse response = null;
        System.IO.Stream dataStream = null;
        System.IO.StreamReader reader = null;
        try
        {
            System.Net.WebRequest request = System.Net.WebRequest.Create(url);
            request.Credentials = System.Net.CredentialCache.DefaultCredentials;
            request.Timeout = 300000;
            response = (System.Net.HttpWebResponse)request.GetResponse();
            dataStream = response.GetResponseStream();
            reader = new System.IO.StreamReader(dataStream, encoding);
            return reader.ReadToEnd();
        }
        catch (Exception ex)
        {
            return "";
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值