[英]C#: Get IP Address from Domain Name?
How can I get an IP address, given a domain name?给定域名,如何获取 IP 地址? For example:
www.test.com
例如:
www.test.com
Dns.GetHostAddresses
您可以使用GetHostAddresses方法:
var address = Dns.GetHostAddresses("www.test.com")[0];
You can get the same results by using:您可以使用以下方法获得相同的结果:
Dns.GetHostAddresses("yahoo.com");
or或者
await Dns.GetHostAddressesAsync("yahoo.com");
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.我的答案可能与上述答案相同,但在这里我使用代码获取当前 Web 应用程序托管的 URL/域名并从中获取 IP 地址。 I used the code in my C# MVC Web app and its working fine.
我在我的C# MVC Web 应用程序中使用了代码并且它工作正常。
Uri myUri = new Uri(((System.Web.HttpContextWrapper)HttpContext).Request.Url.ToString());
var ipAddress = Dns.GetHostAddresses(myUri.Host).FirstOrDefault().ToString();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.