C# Web 获取客户端IP
2012-10-22 11:00
218 查看
#region 获取web客户端ip
/// <summary>
/// 获取web客户端ip
/// </summary>
/// <returns></returns>
public static string GetWebClientIp()
{
string userIP = "未获取用户IP";
try
{
if (System.Web.HttpContext.Current == null
|| System.Web.HttpContext.Current.Request == null
|| System.Web.HttpContext.Current.Request.ServerVariables == null)
return "";
string CustomerIP = "";
//CDN加速后取到的IP simone 090805
CustomerIP = System.Web.HttpContext.Current.Request.Headers["Cdn-Src-Ip"];
if (!string.IsNullOrEmpty(CustomerIP))
{
return CustomerIP;
}
CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!String.IsNullOrEmpty(CustomerIP))
return CustomerIP;
if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
{
CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (CustomerIP == null)
CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
else
{
CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
if (string.Compare(CustomerIP, "unknown", true) == 0)
return System.Web.HttpContext.Current.Request.UserHostAddress;
return CustomerIP;
}
catch { }
return userIP;
}
#endregion
/// <summary>
/// 获取web客户端ip
/// </summary>
/// <returns></returns>
public static string GetWebClientIp()
{
string userIP = "未获取用户IP";
try
{
if (System.Web.HttpContext.Current == null
|| System.Web.HttpContext.Current.Request == null
|| System.Web.HttpContext.Current.Request.ServerVariables == null)
return "";
string CustomerIP = "";
//CDN加速后取到的IP simone 090805
CustomerIP = System.Web.HttpContext.Current.Request.Headers["Cdn-Src-Ip"];
if (!string.IsNullOrEmpty(CustomerIP))
{
return CustomerIP;
}
CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!String.IsNullOrEmpty(CustomerIP))
return CustomerIP;
if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
{
CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (CustomerIP == null)
CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
else
{
CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
if (string.Compare(CustomerIP, "unknown", true) == 0)
return System.Web.HttpContext.Current.Request.UserHostAddress;
return CustomerIP;
}
catch { }
return userIP;
}
#endregion
相关文章推荐
- C#通过Web获取客户端的IP与Mac地址实现
- c#中获取服务器IP,客户端IP以及其它
- C# 获取客户端IP
- 如何获取访问web服务的客户端IP?客户端调用web服务时,对于有参函数能输入参数?
- c#中获取服务器IP,客户端IP以及Request.ServerVariables详细说明
- c#中获取服务器IP,客户端IP以及Request.ServerVariables详细说明
- c#中获取服务器IP,客户端IP以及Request.ServerVariables详细说明
- c#中获取服务器IP,客户端IP以及其它
- C# 获取客户端IP
- C# 获取客户端信息 /asp.net/WebService/WebForm
- Java Web 中通过request请求头获取客户端真实IP
- 如何获取客户端IP、WEB服务器IP
- C# 获取客户端i,客户端主机ip
- c# webform js文件获取客户端控件,后台cs获取前台客户端控件的值
- c# 获取客户端ip
- Java Web 获取客户端真实IP
- c#中获取服务器IP,客户端IP以及其它
- c#中获取服务器IP,客户端IP以及其它
- c#中获取服务器IP,客户端IP以及Request.ServerVariables详细说明
- [导入]【原】C#里获取客户端IP,端口号的简单示例