C#: Get IP Address from Domain Name?

后端 未结 4 410
醉话见心
醉话见心 2020-12-16 10:31

How can I get an IP address, given a domain name? For example: www.test.com

相关标签:
4条回答
  • 2020-12-16 10:59

    My answer could be more the same above answers, but here i get the current web app hosted URL / domain name using code and obtained the IP address and from that. I used the code in my C# MVC Web app and its working fine.

    Uri myUri = new Uri(((System.Web.HttpContextWrapper)HttpContext).Request.Url.ToString());
    var ipAddress = Dns.GetHostAddresses(myUri.Host).FirstOrDefault().ToString();
    
    0 讨论(0)
  • 2020-12-16 11:01

    You can get the same results by using:

    Dns.GetHostAddresses("yahoo.com");
    

    or

    await Dns.GetHostAddressesAsync("yahoo.com");
    
    0 讨论(0)
  • 2020-12-16 11:05

    You could use the GetHostAddresses method:

    var address = Dns.GetHostAddresses("www.test.com")[0];
    
    0 讨论(0)
  • 2020-12-16 11:15
    Dns.GetHostAddresses
    
    0 讨论(0)
提交回复
热议问题