开发者

C#: Get IP Address from Domain Name?

开发者 https://www.devze.com 2022-12-23 00:16 出处:网络
How can I get an IP address, giv开发者_Go百科en a domain name? For example: www.test.com You can use the System.Net.Dns class:

How can I get an IP address, giv开发者_Go百科en a domain name? For example: www.test.com


You can use the System.Net.Dns class:

Dns.GetHostAddresses("www.test.com");


You could use the GetHostAddresses method:

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. 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

精彩评论

暂无评论...
验证码 换一张
取 消