抓ip的方法
判所 client 端是否有設定代理伺服器
Request.ServerVariables["HTTP_VIA"]
取得 client端 ip
Request.ServerVariables["REMOTE_ADDR"]
===================================================================================
string clientIP = GetClientIP();
/// <summary> /// 取得正確的Client端IP /// </summary> /// <returns></returns> protected string GetClientIP() { //判所client端是否有設定代理伺服器 if (Request.ServerVariables["HTTP_VIA"] == null) return Request.ServerVariables["REMOTE_ADDR"].ToString(); else return Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString(); }
===================================================================================
private static string RetrieveIP(HttpRequest request) { string ip = request.ServerVariables["HTTP_X_FORWARDED_FOR"]; if (ip == null || ip.Trim() == string.Empty) { ip = request.ServerVariables["REMOTE_ADDR"]; } return ip; }
沒有留言:
張貼留言