您的位置:首页 > 编程语言 > C#

C# 获取客户端IP

2009-07-11 10:44 253 查看
public static string GetRealIP()

{

string ip;

try

{

HttpRequest request = HttpContext.Current.Request;

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

{

ip = request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString().Split(',')[0].Trim();

}

else

{

ip = request.UserHostAddress;

}

}

catch (Exception e)

{

throw e;

}

return ip;

}

public static string GetViaIP()

{

string viaIp = null;

try

{

HttpRequest request = HttpContext.Current.Request;

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

{

viaIp = request.UserHostAddress;

}

}

catch (Exception e)

{

throw e;

}

return viaIp;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: