一、获取客户端IP
public string getIP() { string result = String.Empty; result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; if (null == result || result == String.Empty) { result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; } if (null == result || result == String.Empty) { result = HttpContext.Current.Request.UserHostAddress; } if (null == result || result == String.Empty) { return "0.0.0.0"; } return result; }
二、获取服务端IP
private string GetIP() { string StrIp=""; System.Net.IPAddress[] AddressList = System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName()).AddressList; for (int i = 0; i < AddressList.Length; i++) { StrIp += AddressList[i].ToString() + " "; } return StrIp; }
原文引用地址:https://www.iteye.com/blog/cpine-474163