public static string GetIP()
{
string result = String.Empty;
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;
/*这种都试过,还是不行.
if (Context.Request.ServerVariables["HTTP_VIA"] != null) // 服务器, using proxy
{
ip = Context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString(); // Return real client IP.
}
else//如果没有使用代理服务器或者得不到客户端的ip not using proxy or can‘t get the Client IP
{
得到服务端的地址
ip = Context.Request.ServerVariables["REMOTE_ADDR"].ToString(); //While it can‘t get the Client IP, it will return proxy IP.
}
*/
}
作者: learnJSee 发布时间: 2011-07-02
作者: vrhero 发布时间: 2011-07-02
作者: chen_ya_ping 发布时间: 2011-07-02
(C# webservice 获取客户端IP疑难杂症)---试过n种方法都失败,获取代理服务器,访问者客户端真实IP
原文:https://www.cnblogs.com/guanshan/p/guan2020-9-19_001.html