ASP.NET获取请求客户端IP与主机电脑名
Joe 于 2020-08-05 21:43:45 发布至 编程相关 累计 2611 次阅读
ASP.NET获取请求客户端IP与主机电脑名:
string hostName = "",clientIP ="";
try
{
    clientIP = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];//存在代理时获取真实IP,不存在代理则为空
    if (string.IsNullOrWhiteSpace(clientIP))
    {
        clientIP = HttpContext.Current.Request.UserHostAddress;//获取客户端的IP主机地址
    }
    hostName = System.Net.Dns.GetHostEntry(clientIP).HostName;//获取客户端主机名
}
catch (Exception ex) { }
                                                 
                         
                                                    