代码
/// <summary>
/// 获取当前域名
/// </summary>
/// <returns></returns>
public static string GetCurrentDomain()
{
string domain = System.Web.HttpContext.Current.Request.ServerVariables["SERVER_NAME"].ToString();
int index = domain.IndexOf('.') + 1;
return domain.Substring(index, domain.Length - index);
//return "pifazhinan.com";
}
/// <summary>
/// 根据域名获取IP
/// </summary>
/// <param name="ToEmail"></param>
/// <param name="subject"></param>
/// <param name="body"></param>
/// <returns></returns>
public static string GetIPByHost(string host)
{
host = GetDomainByHost(host);
//Dns.GetHostAddresses(host);
IPHostEntry dnstoip = new IPHostEntry();
dnstoip = Dns.Resolve(host);
string ip = dnstoip.AddressList[0].ToString();
return ip;
}
/// <summary>
/// 获取用户真实IP地址
/// </summary>
/// <returns>返回用户真实IP</returns>
public static string GetUserRealIp()
{
string user_IP = "";
if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
{
user_IP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
}
else
{
user_IP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
}
return user_IP;
}
/// 获取当前域名
/// </summary>
/// <returns></returns>
public static string GetCurrentDomain()
{
string domain = System.Web.HttpContext.Current.Request.ServerVariables["SERVER_NAME"].ToString();
int index = domain.IndexOf('.') + 1;
return domain.Substring(index, domain.Length - index);
//return "pifazhinan.com";
}
/// <summary>
/// 根据域名获取IP
/// </summary>
/// <param name="ToEmail"></param>
/// <param name="subject"></param>
/// <param name="body"></param>
/// <returns></returns>
public static string GetIPByHost(string host)
{
host = GetDomainByHost(host);
//Dns.GetHostAddresses(host);
IPHostEntry dnstoip = new IPHostEntry();
dnstoip = Dns.Resolve(host);
string ip = dnstoip.AddressList[0].ToString();
return ip;
}
/// <summary>
/// 获取用户真实IP地址
/// </summary>
/// <returns>返回用户真实IP</returns>
public static string GetUserRealIp()
{
string user_IP = "";
if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
{
user_IP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
}
else
{
user_IP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
}
return user_IP;
}