| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
Blake36Q
9年前发布

c#获得服务器的外网ip,内网ip

如下代码可以获得服务器所在内网的内网ip

IPHostEntry host;  string localIP = "?";  host = Dns.GetHostEntry(Dns.GetHostName());  foreach (IPAddress ip in host.AddressList)  {      if (ip.AddressFamily.ToString() == "InterNetwork")      {          localIP = ip.ToString();          break;      }  }  return localIP;

如果要获得服务器的公网ip,可以使用下面代码:

public string GetPublicIP()  {      String direction = "";      WebRequest request = WebRequest.Create("http://checkip.dyndns.org/");      using (WebResponse response = request.GetResponse())      using (StreamReader stream = new StreamReader(response.GetResponseStream()))      {          direction = stream.ReadToEnd();      }        //Search for the ip in the html      int first = direction.IndexOf("Address: ") + 9;      int last = direction.LastIndexOf("</body>");      direction = direction.Substring(first, last - first);        return direction;  }