获取客户端的ip地址

 

//获取客户端的ip地址
    public string GetClientIP()
    
{
        
string result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
        
if (null == result || result == String.Empty)
        
{
            result 
= HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
        }

        
if (null == result || result == String.Empty)
        
{
            result 
= HttpContext.Current.Request.UserHostAddress;
        }

        
return result;
    }

    
//将IP 地址转化为数字
    public long IPtoNum(string Ip)
    
{
        
string[] stringip = new string[4];
        stringip 
= Ip.Split('.');
        
long ipnum = Convert.ToInt64((stringip[0])) * 16777216 + Convert.ToInt64(stringip[1]) * 65536 + Convert.ToInt64(stringip[2]) * 256 + Convert.ToInt64(stringip[3]);
        
return ipnum;
    }

    
protected void Page_Load(object sender, EventArgs e)
    
{
        Response.Write(GetIP());
    }
posted @ 2008-03-01 09:30  melody&bobo  阅读(186)  评论(0)    收藏  举报