Fork me on GitHub
获取IP地址的三种方式
//方式一


        string clientIP;

        if (Context.Request.ServerVariables["HTTP_VIA"] != null)

        {

            clientIP = Context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();

        }

        else

        {

           clientIP = Context.Request.ServerVariables["REMOTE_ADDR"].ToString();

        }

        this.Label1.Text = clientIP;


        //方式二

        using System.Net; 

        IPHostEntry ipHostEntry = Dns.GetHostByName(Dns.GetHostName());

        //获得本机IP地址

        IPAddress addr = new IPAddress(ipHostEntry.AddressList[0].Address);


        this.Label1.Text = addr.ToString();


        //方式三


        this.Label1.Text=Request.Url.Host;

posted on 2010-04-29 16:27  HackerVirus  阅读(421)  评论(0编辑  收藏  举报