C# 獲取Ip地址
阿新 • • 發佈:2018-11-12
獲取Ip地址,程式碼如下:
public static string GetIp() { string ip = string.Empty; HttpContext context = HttpContext.Current; if (context != null) { HttpRequest request = context.Request; ip = request.Headers["HTTP_X_FORWARDED_FOR"];//微信相關特殊處理,@劉俊 if (string.IsNullOrEmpty(ip) || (ip.ToLower() == "unknown")) { ip = request.ServerVariables["HTTP_X_FORWARDED_FOR"]; if (string.IsNullOrEmpty(ip) || (ip.ToLower() == "unknown")) { ip = request.ServerVariables["HTTP_X_REAL_IP"]; if (string.IsNullOrEmpty(ip)) { ip = request.ServerVariables["REMOTE_ADDR"]; } } } } return GetShortIp(ip); }