DynDns Update with c# client

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • André Heuer

    DynDns Update with c# client

    Hi,

    i want to write a .net class that can update my ip address at the dyndns
    service (http://www.dyndns.org).

    When I open the page
    https://username:passwo rd@members.dynd ns.org/nic/update?system=d yndns&hostname= MyHostNameHere& myip=MyCurrentI pHere&wildcard= on

    I get the response nochg 80.146.122.175 which means "No change". IP hasn´t changed. Fine.

    I wrote this code in my c# class:
    Code:
    public void UpdateIP(IPAddress ip)
    {
        const string UPDATE_URL =
    "https://<USER>:<PWD>@members.dyndns.org/nic/update?system=dyndns&hostname=<
    HOST>&myip=<IP>&wildcard=<WILDCARD>";
    
        string url = UPDATE_URL;
        url = url.Replace("<USER>", mUsername);
        url = url.Replace("<PWD>", mPassword);
        url = url.Replace("<HOST>", mDomain);
        url = url.Replace("<IP>", ip.ToString());
        url = url.Replace("<WILDCARD>", mWildcard ? "on" : "off");
    
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    
        request.Headers.Add("Cache-control", "no-cache");
        request.Headers.Add("Pragma","no-cache");
    
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        string text = new StreamReader(response.GetResponseStream()).ReadToEnd();
    
    }
    I get the following error on the line
    Code:
    "HttpWebResponse response =
    (HttpWebResponse)request.GetResponse();":
    
    'System.Net.WebException' in system.dll
    Remote Server returned an error (403) Unzulässig.
    What is the difference between the browser and my code? Any ideas?

    Tia

    André Heuer
    Last edited by Frinavale; Jun 12 '13, 05:23 PM. Reason: Added code tags and formating
  • André Heuer

    #2
    Re: DynDns Update with c# client

    Additional information. This code in vbs works fine.....

    Function UpdateDynDnsSer vice(user,passw ord,hostname,ip address,wildcar d)
    Dim xmlhttp,url,sta rtPos,endPos,te mpCmd

    set xmlhttp = createobject("m icrosoft.xmlhtt p")
    tempCmd = DYNDNSUPDATECMD
    tempCmd = replace(DYNDNSU PDATECMD,"<USER >",user)
    tempCmd = replace(tempCmd ,"<PWD>",passwo rd)
    tempCmd = replace(tempCmd ,"<HOST>",hostn ame)
    tempCmd = replace(tempCmd ,"<IP>",ipaddre ss)
    tempCmd = replace(tempCmd ,"<WILDCARD>",w ildcard)
    url = tempCmd
    xmlhttp.open "get",url,f alse

    wscript.echo tempcmd

    xmlhttp.setrequ estheader "Pragma","n o-cache"
    xmlhttp.setrequ estheader "Cache-control","no-cache"
    On Error Resume Next
    xmlhttp.send
    If Err.Number<>0 Then
    UpdateDynDnsSer vice = "Fail"
    Exit Function
    End If
    On Error Goto 0
    If (xmlhttp.Status = 200) then
    UpdateDynDnsSer vice = CStr(xmlhttp.re sponsetext)
    Else
    If (xmlhttp.Status = 401) then
    UpdateDynDnsSer vice = "badauth"
    Else
    UpdateDynDnsSer vice = "Fail"
    End If
    End If
    End Function


    Comment

    • André Heuer

      #3
      Re: DynDns Update with c# client

      i solved the problem:

      public DynDnsUpdateRes ult UpdateIP(IPAddr ess ip)
      {
      byte[] data = new byte[1024];
      string response = "";
      int count;

      // Syntax zum Aufruf: http://www.dyndns.org/developers/specs/syntax.html
      Socket socket = new Socket(AddressF amily.InterNetw ork,
      SocketType.Stre am,ProtocolType .Tcp);
      IPHostEntry host = System.Net.Dns. Resolve("member s.dyndns.org");
      socket.Connect( (EndPoint)(new IPEndPoint(host .AddressList[0], 80)));

      if (!socket.Connec ted)
      throw new Exception("Can´ t connect to dyndns service");

      string request = "GET /nic/update?" +
      "system=dyn dns" +
      "&hostname= " + mDomain +
      "&myip=" + ip.ToString() +
      "&wildcard= " + (mWildcard ? "ON" : "OFF") +
      "&offline=N O " +
      "HTTP/1.1\r\n" +
      "Host: members.dyndns. org\r\n" +
      "Authorizat ion: Basic " +
      System.Convert. ToBase64String( ASCIIEncoding.A SCII.GetBytes(m Username + ":" +
      mPassword)) + "\r\n" +
      "User-Agent: .net dyndns client\r\n\r\n" ;

      count = socket.Send(Sys tem.Text.Unicod eEncoding.ASCII .GetBytes(reque st));

      while((count = socket.Receive( data)) != 0) // Antwort von Server
      response += System.Text.ASC IIEncoding.ASCI I.GetString(dat a, 0, count);

      socket.Shutdown (SocketShutdown .Both);
      socket.Close();

      response = response.Substr ing(response.In dexOf("\r\n\r\n ") + 4); // Html
      Header entfernen

      switch (response.Subst ring(0, response.IndexO f(" ")).ToLower ())
      {
      case "good":
      // The update was successful, and the hostname is now updated
      return DynDnsUpdateRes ult.UpdatedIp;
      case "nochg":
      // The update changed no settings, and is considered abusive.
      Additional nochg updates will cause the hostname to become blocked
      return DynDnsUpdateRes ult.NoUpdateSam eIp;
      case "badsys":
      throw new Exception("The system parameter given is not valid. Valid
      system parameters are dyndns, statdns and custom");
      case "badagent":
      throw new Exception("The user agent that was sent has been blocked for
      not following these specifications or no user agent was specified");
      case "badauth":
      throw new Exception("The username or password specified are
      incorrect");
      case "!donator":
      throw new Exception("An option available only to credited users (such
      as offline URL) was specified, but the user is not a credited user");
      case "notfqdn":
      throw new Exception("The hostname specified is not a fully-qualified
      domain name (not in the form hostname.dyndns .org or domain.com)");
      case "nohost":
      throw new Exception("The hostname specified does not exist (or is not
      in the service specified in the system parameter)");
      case "!yours":
      throw new Exception("The hostname specified exists, but not under the
      username specified");
      case "abuse":
      throw new Exception("The hostname specified is blocked for update
      abuse");
      case "numhost":
      throw new Exception("Too many or too few hosts found");
      case "dnserr":
      throw new Exception("DNS error encountered");
      case "911":
      throw new Exception("Ther e is a serious problem on our side, such as a
      database or DNS server failure. The client should stop updating until
      notified via the status page that the service is back up.");
      default:
      throw new Exception("Unkn own result from dyndns service");
      }
      #endregion
      }

      "André Heuer" <aheuer@t-online.de> schrieb im Newsbeitrag
      news:uaU$ND1qDH A.2480@TK2MSFTN GP10.phx.gbl...[color=blue]
      > Hi,
      >
      > i want to write a .net class that can update my ip address at the dyndns
      > service (http://www.dyndns.org).
      >
      > When I open the page
      >
      >[/color]
      https://username:password@members.dy...re&wildcard=on[color=blue]
      >
      > i get the response
      >
      > nochg 80.146.122.175
      >
      > which means "No change". IP hasn´t changed. Fine.
      >
      > I wrote this code in my c# class:
      >
      > public void UpdateIP(IPAddr ess ip)
      > {
      > const string UPDATE_URL =
      >[/color]
      "https://<USER>:<PWD>@me mbers.dyndns.or g/nic/update?system=d yndns&hostname= <[color=blue]
      > HOST>&myip=<IP> &wildcard=<WILD CARD>";
      >
      > string url = UPDATE_URL;
      > url = url.Replace("<U SER>", mUsername);
      > url = url.Replace("<P WD>", mPassword);
      > url = url.Replace("<H OST>", mDomain);
      > url = url.Replace("<I P>", ip.ToString());
      > url = url.Replace("<W ILDCARD>", mWildcard ? "on" : "off");
      >
      > HttpWebRequest request = (HttpWebRequest )WebRequest.Cre ate(url);
      >
      > request.Headers .Add("Cache-control", "no-cache");
      > request.Headers .Add("Pragma"," no-cache");
      >
      > HttpWebResponse response = (HttpWebRespons e)request.GetRe sponse();
      > string text = new[/color]
      StreamReader(re sponse.GetRespo nseStream()).Re adToEnd();[color=blue]
      >
      > }
      >
      > I get the following error on the line "HttpWebRespons e response =
      > (HttpWebRespons e)request.GetRe sponse();":
      >
      > 'System.Net.Web Exception' in system.dll
      > Remote Server returned an error (403) Unzulässig.
      >
      > What is the difference betwenn the browser and my code? Any ideas?
      >
      > Tia
      >
      > André Heuer
      >
      >[/color]


      Comment

      Working...