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

c#获取真实IP和代理IP

2009-11-25 15:34 405 查看
获取真实IP
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;
}

获取代理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;

}

Appendix

what proxy server are you using? if you are using ISA, take a look at:

http://www.isaserver.org/articles/Configuring_a_ISP_Colocated_WebSMTPISA_Server.html

basically, you need to set the proxy server in transparent mode
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: