如何获取服务器IP地址?

JL.*_*JL. 28 c# asp.net

是否有一行方法来获取服务器的IP地址?

谢谢

Zha*_*uid 57

Request.ServerVariables["LOCAL_ADDR"];
Run Code Online (Sandbox Code Playgroud)

来自文档:

返回请求所在的服务器地址.这对于可能有多个绑定到计算机的IP地址的计算机很重要,并且您想要找出请求使用的地址.

这与客户端计算机相关的远程地址不同.

  • 完美的答案 - 从旧的ASP经典日,我应该记住这一个:) (2认同)

Fai*_* S. 6

从搜索网上我发现以下代码:(我找不到单行方法)

string myHost = System.Net.Dns.GetHostName();

// Show the hostname 

MessageBox.Show(myHost);

// Get the IP from the host name

string myIP = System.Net.Dns.GetHostEntry(myHost).AddressList[index].ToString();

// Show the IP 

MessageBox.Show(myIP);
Run Code Online (Sandbox Code Playgroud)

- >其中index是你的ip地址主机的索引(即网络连接).

代码来自:http://www.geekpedia.com/tutorial149_Get-the-IP-address-in-a-Windows-application.html