Is there a 1 line method to get the IP Address of the server?
是否有一行方法来获取服务器的IP地址?
Thanks
谢谢
4 个解决方案
#1
55
Request.ServerVariables["LOCAL_ADDR"];
From the docs:
来自文档:
Returns the server address on which the request came in. This is important on computers where there can be multiple IP addresses bound to the computer, and you want to find out which address the request used.
返回请求所在的服务器地址。这对于可能有多个绑定到计算机的IP地址的计算机很重要,并且您想要找出请求使用的地址。
This is distinct from the Remote addresses which relate to the client machine.
这与客户端计算机相关的远程地址不同。
#2
6
From searching the net I found following code: (I couldn't find a single line method there)
从搜索网上我发现以下代码:(我找不到单行方法)
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);
-> where index is the index of your ip address host (ie. network connection).
- >其中index是你的ip地址主机的索引(即网络连接)。
Code from: http://www.geekpedia.com/tutorial149_Get-the-IP-address-in-a-Windows-application.html
代码来自:http://www.geekpedia.com/tutorial149_Get-the-IP-address-in-a-Windows-application.html
#3
2
As other(s) have posted, System.Net.Dns.GetHostEntry
is the way to go. When you access the AddressList
property, you'll want to take the AddressFamily
property into account, as it could return both IPv4 AND IPv6 results.
正如其他人发布的那样,System.Net.Dns.GetHostEntry是可行的方法。当您访问AddressList属性时,您将需要考虑AddressFamily属性,因为它可以返回IPv4和IPv6结果。
#4
0
This method will return your machine public IP address when run this code on your PC and when you deploy your application on server will return Server IP address.
在PC上运行此代码时,此方法将返回您的计算机公共IP地址,当您在服务器上部署应用程序时,将返回服务器IP地址。
public static string Getpublicip()
{
try
{
string externalIP = "";
var request = (HttpWebRequest)WebRequest.Create("http://icanhazip.com.ipaddress.com/");
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
externalIP = new WebClient().DownloadString("http://icanhazip.com");
return externalIP;
}
catch (Exception e)
{
return "null";
}
}
#1
55
Request.ServerVariables["LOCAL_ADDR"];
From the docs:
来自文档:
Returns the server address on which the request came in. This is important on computers where there can be multiple IP addresses bound to the computer, and you want to find out which address the request used.
返回请求所在的服务器地址。这对于可能有多个绑定到计算机的IP地址的计算机很重要,并且您想要找出请求使用的地址。
This is distinct from the Remote addresses which relate to the client machine.
这与客户端计算机相关的远程地址不同。
#2
6
From searching the net I found following code: (I couldn't find a single line method there)
从搜索网上我发现以下代码:(我找不到单行方法)
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);
-> where index is the index of your ip address host (ie. network connection).
- >其中index是你的ip地址主机的索引(即网络连接)。
Code from: http://www.geekpedia.com/tutorial149_Get-the-IP-address-in-a-Windows-application.html
代码来自:http://www.geekpedia.com/tutorial149_Get-the-IP-address-in-a-Windows-application.html
#3
2
As other(s) have posted, System.Net.Dns.GetHostEntry
is the way to go. When you access the AddressList
property, you'll want to take the AddressFamily
property into account, as it could return both IPv4 AND IPv6 results.
正如其他人发布的那样,System.Net.Dns.GetHostEntry是可行的方法。当您访问AddressList属性时,您将需要考虑AddressFamily属性,因为它可以返回IPv4和IPv6结果。
#4
0
This method will return your machine public IP address when run this code on your PC and when you deploy your application on server will return Server IP address.
在PC上运行此代码时,此方法将返回您的计算机公共IP地址,当您在服务器上部署应用程序时,将返回服务器IP地址。
public static string Getpublicip()
{
try
{
string externalIP = "";
var request = (HttpWebRequest)WebRequest.Create("http://icanhazip.com.ipaddress.com/");
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
externalIP = new WebClient().DownloadString("http://icanhazip.com");
return externalIP;
}
catch (Exception e)
{
return "null";
}
}