我在另一篇帖子中看到了这个问题,但解决方案没有正确地工作。
我使用:
System.Net.Dns.GetHostEntry(HttpContext.Current.Request.ServerVariables.Item("REMOTE_HOST")).HostName
这在本地环境下是正常的,但在服务器上出现问题并返回空字符串。有什么想法吗?
HttpContext.Current.Request.UserHostAddress.ToString
或者
HttpContext.Current.Request.UserHostName
HttpContext.Current.Request.Browser.Browser
需要考虑两件事情:
HttpContext.Current.Request.ServerVariables.Item("REMOTE_HOST"
) 将返回发出请求的主机名,而不是地址。System.Net.Dns.GetHostByAddress(Request.ServerVariables.Item("REMOTE_HOST")).HostName
替代。您可以在 Microsoft 的 MSDN 网站上找到服务器变量列表。
希望这能帮到您,
System.Net.Dns.GetHostEntry(HttpContext.Current.Request.ServerVariables["REMOTE_HOST"])
。