我在另一篇文章中看到了这个问题,但解决方案无法正常工作。 我用:
System.Net.Dns.GetHostEntry(HttpContext.Current.Request.ServerVariables.Item("REMOTE_HOST")).HostName
这在本地主机上工作正常,但在服务器上有问题并返回空字符串。 有什么想法吗?
我发现 VS2010 中“GetHostByAddress”已被弃用。 相反,请使用:
System.Net.Dns.GetHostEntry(HttpContext.Current.Request.ServerVariables["REMOTE_HOST"])
请考虑到
HttpContext.Current.Request.ServerVariables.Item("REMOTE_HOST"
) 将返回发出请求的主机的名称,而不是地址。
尝试做
System.Net.Dns.GetHostByAddress(Request.ServerVariables.Item("REMOTE_HOST")).HostName
来代替。
您可以在 Microsoft 的 MSDN 网站找到服务器变量列表。
获取客户端IP地址使用
HttpContext.Current.Request.UserHostAddress.ToString
或
HttpContext.Current.Request.UserHostName
获取客户端浏览器使用
HttpContext.Current.Request.Browser.Browser