获取客户端和服务器端IP地址

    using System;  
    using System.Data;  
    using System.Configuration;  
    using System.Web;  
    using System.Web.Security;  
    using System.Web.UI;  
    using System.Web.UI.WebControls;  
    using System.Web.UI.WebControls.WebParts;  
    using System.Web.UI.HtmlControls;  
    using System.Net;  
      
    public partial class _Default : System.Web.UI.Page   
    {  
        protected void Page_Load(object sender, EventArgs e)  
        {     
        }  
        protected void Button1_Click(object sender, EventArgs e)  
        {     
            //获取客户端的IP地址  
            //TextBox1.Text = Request.UserHostAddress;  
            TextBox1.Text=Request.ServerVariables ["REMOTE_ADDR"];  
            //获取服务器的主机名  
            string hostName = Dns.GetHostName();  
            //获取服务器的IP地址  
            IPAddress[] ips;  
            ips = Dns.GetHostAddresses(hostName);  
            foreach (IPAddress ip in ips)  
            {  
                TextBox2.Text = ip.ToString();  
            }        
        }    
    }  

 

posted @ 2013-03-11 12:30  天涯海角路  阅读(213)  评论(0)    收藏  举报