Bespoke.DynamicDnsUpdater.Client.DnsOMatic.DnsOMaticClient.UpdateHostname C# (CSharp) Method

UpdateHostname() public method

Updates the specified hostname via DNS-O-Matic to the IP Address that is given.
public UpdateHostname ( string hostname, string ipAddress ) : bool
hostname string The hostname to update.
ipAddress string The IP address to use.
return bool
        public override bool UpdateHostname(string hostname, string ipAddress)
        {
            try
            {
                if (HasIpAddresssChanged(hostname, ipAddress) == false) return true; // No change, no need to update

                if (IsValidIpAddress(ipAddress) == false)
                {
                    logger.Error(string.Format("Invalid IP Address provided: {0}", ipAddress));
                    return false;
                }

                var request = CreateUpdateHostnameRequest(hostname, ipAddress);

                using(var response = request.GetResponse())
                {
                    string responseBody = string.Empty;
                    using (var streamReader = new StreamReader(response.GetResponseStream()))
                    {
                        responseBody = streamReader.ReadToEnd();

                        logger.Info(string.Format("DNS-O-Matic update response for hostname {0}: {1}", hostname, responseBody));

                        var regex = new Regex(@"\w+");
                        var match = regex.Match(responseBody);

                        var statusCode = match.Success ? UpdateStatusCodeConverter.GetUpdateStatusCode(match.Value) : UpdateStatusCode.Unknown;

                        UpdateStatusCodes[hostname] = statusCode;

                        if (statusCode == UpdateStatusCode.Good || statusCode == UpdateStatusCode.NoChange)
                        {
                            LastUpdateIpAddresses[hostname] = ipAddress;
                            return true;
                        }

                        return false;
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);

                return false;
            }
        }