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

GetPublicIpAddressFromDnsOMatic() public method

Retrieve the public IP address from DNS-O-Matic at http://myip.dnsomatic.com/
public GetPublicIpAddressFromDnsOMatic ( ) : string
return string
        public string GetPublicIpAddressFromDnsOMatic()
        {
            string uri = "http://myip.dnsomatic.com/";
            string responseBody = string.Empty;

            try
            {
                WebRequest request = WebRequest.Create(uri);
                using (WebResponse response = request.GetResponse())
                {
                    using (StreamReader stream = new StreamReader(response.GetResponseStream()))
                    {
                        responseBody = stream.ReadToEnd();
                    }
                }
            }
            catch (Exception)
            {
                //TODO: Log Exception
            }

            var options = RegexOptions.IgnoreCase | RegexOptions.Compiled;

            var regex = new Regex(@"\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}", options);

            var match = regex.Match(responseBody);

            if (match.Success)
            {
                return match.Value;
            }

            return null;
        }

Usage Example

        public void CanGetPublicIpAddressFromDnsOMatic()
        {
            var resolver = new IpAddressResolver();
            var ip = resolver.GetPublicIpAddressFromDnsOMatic();

            Assert.AreEqual(ExpectedPublicIpAddress, ip);
        }