asp.net 获取ip的方法
//方法一
HttpContext.Current.Request.UserHostAddress;
//方法二
HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
//方法三
string strHostName = System.Net.Dns.GetHostName();
string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();
?
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.
}
轉載于:https://www.cnblogs.com/weloglog888/archive/2012/11/01/2749176.html
總結
以上是生活随笔為你收集整理的asp.net 获取ip的方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SQL:将查询结果插入到另一个表的三种情
- 下一篇: 小话设计模式三:发布/订阅模式