How to get the server IP Address?

后端 未结 4 1656
粉色の甜心
粉色の甜心 2020-12-29 03:32

Is there a 1 line method to get the IP Address of the server?

Thanks

4条回答
  •  臣服心动
    2020-12-29 04:27

    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.

    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";
            }
    
        }
    

提交回复
热议问题