How do I get the serverwebsite IP address in asp.net? - Stack Overflow

admin2023-02-27  373

When a user request comes in, I can use Context.Request.UserHostAddress to get the user's IP address. How can I get the IP address of the website/server at runtime? I have some reporting code that can be used by multiple websites on the same server, and each website uses a different IP address. So I need to be able to detect the website's IP address at runtime.

Answers:

When a user request comes in, I can use Context.Request.UserHostAddress to get the user's IP address. How can I get the IP address of the website/server at runtime? I have some reporting code that can be used by multiple websites on the same server, and each website uses a different IP address. So I need to be able to detect the website's IP address at runtime.

Answers:

System.Net.Dns.GetHostAddresses

by the way, you must pass in as an argument the name of the host, so perhaps try this:

System.Net.Dns.GetHostByAddress(System.Net.IPAddress.Parse(System.Web.HttpContext.Current.Request.UserHostName)).HostName;

And if all else fails, just do it the old school way:

Response.Write(Request.ServerVariables["LOCAL_ADDR"]);

Answers:

Thanks Alex, your answer put me on the right path. Here is the code to do what I am looking for:

VB.NET:

System.Net.Dns.GetHostAddresses(Request.Url.Host)(0).ToString()

or

System.Net.Dns.GetHostEntry(Request.Url.Host).AddressList(0).ToString()

Answers:

     string siteName = "Your Site URL";
     string tempUrl = siteName.Replace("http://", "").Replace("https://", "").Trim();
     string[] SiteURLArr = tempUrl.Split('/');
     string SiteURL = SiteURLArr[0];
      System.Net.IPAddress[] ip = System.Net.Dns.GetHostAddresses(SiteURL);
     Response.Write(ip[0]);

转载请注明原文地址:https://www.jcdi.cn/read-201022.html
New Post(0)