Bespoke.DynamicDnsUpdater.Client.IpAddressResolver.GetIpAddressForHostname C# (CSharp) Method

GetIpAddressForHostname() public method

Submits a dns request for the given hostname and returns the IP address that it resolves to.
public GetIpAddressForHostname ( string hostname ) : string
hostname string The hostname to get an IP address for.
return string
        public string GetIpAddressForHostname(string hostname)
        {
            try
            {
                var hostEntry = Dns.GetHostEntry(hostname);

                if (hostEntry.AddressList.Count() > 0)
                {
                    return hostEntry.AddressList.First().ToString();
                }

                return null;
            }
            catch (Exception)
            {
                return null;
            }
        }

Usage Example

        public void CanGetIpAddressForHostname()
        {
            var resolver = new IpAddressResolver();
            var ip = resolver.GetIpAddressForHostname("bespokeindustries.com");

            Assert.AreEqual("204.246.37.132", ip);
        }
All Usage Examples Of Bespoke.DynamicDnsUpdater.Client.IpAddressResolver::GetIpAddressForHostname