Search code examples
c#httpdyndns

Dyndns updater with http get


I need to update our dyndns zones with an application.

Their api documentation is located here

They say i need to make a get request like so:

GET /nic/update?    hostname=yourhostname&myip=ipaddress&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG HTTP/1.0
Host: members.dyndns.org
Authorization: Basic base-64-authorization
User-Agent: Company - Device - Version Number

How would I do this in c# ?

I have tried this:

String request = "/nic/update?hostname=yourhostname&myip=ipaddress&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG HTTP/1.0";
WebRequest webRequest = WebRequest.Create(request);
WebResponse webResp = webRequest.GetResponse();
Console.WriteLine(webResp.ToString()

But how do I do the hostname and all that?


Solution

  • I just wanted to post my code which I eventually got right, Incase other may need help with it one day!

    I have broken it up into some sub functions for simplicity. Don't let it scare you.

    /// <summary>
    /// Call this from another class to update a zone.
    /// </summary>
    /// <param name="host">The full name of the host</param>
    /// <returns></returns>
    public string Update(String host)
    {
        string url = BuildUrl(host, Ip);
        return PerformUpdate(url);
    }
    

    Here is A function to build the url

     /// <summary>
     /// //Constructs the url to send the get request to.
     /// </summary>
     /// <param name="hostname">the hostname </param>
     /// <param name="ip">the ipaddress</param>
     /// <returns>The complete String</returns>
     private string BuildUrl(String hostname, String ip)
     {
        return BaseUrl + "hostname=" + hostname + "&myip=" + ip;
     }
    

    Here is the function that does the update:

    /// <summary>
    /// Performs the actual request to the dyndns server to update the entity
    /// </summary>
    /// <param name="url">url to post</param>
    private String PerformUpdate(String url)
    {
       HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
       NetworkCredential creds = new NetworkCredential(Username, Password);
       request.UserAgent = Username + " - " + Password + " - " + "0.01";
       request.Credentials = creds;
       request.Method = "GET";
       HttpWebResponse response = request.GetResponse() as HttpWebResponse;
       Stream reply = response.GetResponseStream();
       StreamReader readReply = new StreamReader(reply);
       return readReply.ReadToEnd();
    }