public static string GetClientIP() { string clientIP = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; if (string.IsNullOrEmpty(clientIP)) clientIP = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; if (string.IsNullOrEmpty(clientIP)) clientIP = HttpContext.Current.Request.UserHostAddress; if (string.IsNullOrEmpty(clientIP)) clientIP = "0.0.0.0"; return clientIP; }
这个还是很全了
嗯,这个不错,谢啦!但要注意的是这获取的是外网IP在同一个局域网中的主机是获取不到的。
Request.UserHostAddress
Request.UserHostAddress
但现在的宽带基本上都是动态ip,路由重启一下就又变了ip