始终获取本地IP地址,本地回环地址

 

直接调用一下方法就行:

public static string GetIP(HttpContextBase req)
{
string result = req.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if(string.IsNullOrEmpty(result))
{
result = req.Request.ServerVariables["REMOTE_ADDR"];
}
if(string.IsNullOrEmpty(result))
{
result = req.Request.UserHostAddress;
}
if(string.IsNullOrEmpty(result))
{
result = "0.0.0.0";
}
if(result=="::1")
{
result = "127.0.0.1";
}
return result;
}

 

天行健,君子以自强不息!

 

 

posted @ 2019-12-01 11:20  超级酸的柠檬丶  阅读(866)  评论(0编辑  收藏  举报