首页蓝耳朵|小小蓝耳朵广州图书馆外文室英语儿童读物资源介绍网佛教青年之友旧版收集永硕E盘Phonics Short Vowels Game 
获取客户端IP及所在位置
所属栏目:ASP.NET(webform)  时间:2011-07-15 10:30  作者:狼子

直接获取直接取客户端IP

private string GetIP()
    {
        string userIP = null;

        if (!String.IsNullOrEmpty(Request.ServerVariables["HTTP_X_FORWARDED_FOR"]))
            userIP = Request.ServerVariables["REMOTE_ADDR"];
        else
            userIP = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

        if (String.IsNullOrEmpty(userIP))
            userIP = Request.UserHostAddress;

        return userIP;
    }

通过分析源码从其他网站取客户端IP和所在位置

private string GetIPandAddr()
    {
        string sourceFileText = null;
        Uri uri = new Uri("http://www.ikaka.com/ip/index.asp");
        System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(uri);
        req.Method = "POST";
        req.ContentType = "application/x-www-form-urlencoded";
        req.ContentLength = 0;
        req.CookieContainer = new System.Net.CookieContainer();
        req.GetRequestStream().Write(new byte[0], 0, 0);
        using (System.Net.HttpWebResponse res = (System.Net.HttpWebResponse)(req.GetResponse()))
        {
            StreamReader rs = new StreamReader(res.GetResponseStream(), System.Text.Encoding.GetEncoding("GB18030"));
            sourceFileText = rs.ReadToEnd();
            rs.Close();
            res.Close();
        }
        req.Abort();

        //页面源码:你的IP:<span class="b">111.11.111.11</span>,来自:<span class="b">狼子省羊羔市电信</span>
        System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(sourceFileText, @"你的IP:<span class=""b"">(?<IP>[0-9\.]*)</span>,来自:<span class=""b"">(?<Addr>[\u4E00-\u9FFF]*)</span>");
        if (m.Success)
            return String.Format("你的IP:{0},来自:{1}", m.Groups["IP"].Value, m.Groups["Addr"].Value);
        return string.Empty;
    }

小楼宝宝的涂鸦花花(Imitater)的博客起名称骨测字