首页 > 代码库 > web获得IP 地址

web获得IP 地址

public static class Comm
 {
/// <summary> /// 获得IP地址 /// </summary> /// <param name="request"></param> /// <returns></returns> public static string GetIPstring(HttpRequest request) { // 穿过代理服务器取远程用户真实IP地址 string Ip = string.Empty; if (request.ServerVariables["HTTP_VIA"] != null) { if (request.ServerVariables["HTTP_X_FORWARDED_FOR"] == null) { if (request.ServerVariables["HTTP_CLIENT_IP"] != null) Ip = request.ServerVariables["HTTP_CLIENT_IP"]; else if (request.ServerVariables["REMOTE_ADDR"] != null) Ip = request.ServerVariables["REMOTE_ADDR"]; else Ip = "0.0.0.1"; } else Ip = request.ServerVariables["HTTP_X_FORWARDED_FOR"]; } else if (request.ServerVariables["REMOTE_ADDR"] != null) { Ip = request.ServerVariables["REMOTE_ADDR"]; } else { Ip = "0.0.0.1"; } return Ip; }
}

  在web页面中方法调用

 

string str_ip=Comm.GetIPstring(Request);