DynDns Update IP Address

  • Thread starter Thread starter André Heuer
  • Start date Start date
A

André Heuer

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:[email protected]&myip=MyCurrentIpHere&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:

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 "HttpWebResponse response =
(HttpWebResponse)request.GetResponse();":

'System.Net.WebException' 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
 
Additional information. This code in vbs works fine.....

Function UpdateDynDnsService(user,password,hostname,ipaddress,wildcard)
Dim xmlhttp,url,startPos,endPos,tempCmd

set xmlhttp = createobject("microsoft.xmlhttp")
tempCmd = DYNDNSUPDATECMD
tempCmd = replace(DYNDNSUPDATECMD,"<USER>",user)
tempCmd = replace(tempCmd,"<PWD>",password)
tempCmd = replace(tempCmd,"<HOST>",hostname)
tempCmd = replace(tempCmd,"<IP>",ipaddress)
tempCmd = replace(tempCmd,"<WILDCARD>",wildcard)
url = tempCmd
xmlhttp.open "get",url,false

wscript.echo tempcmd

xmlhttp.setrequestheader "Pragma","no-cache"
xmlhttp.setrequestheader "Cache-control","no-cache"
On Error Resume Next
xmlhttp.send
If Err.Number<>0 Then
UpdateDynDnsService = "Fail"
Exit Function
End If
On Error Goto 0
If (xmlhttp.Status = 200) then
UpdateDynDnsService = CStr(xmlhttp.responsetext)
Else
If (xmlhttp.Status = 401) then
UpdateDynDnsService = "badauth"
Else
UpdateDynDnsService = "Fail"
End If
End If
End Function
 
have you tried to specify the credentials, not in the URL string, but in the
request? eg. (sorry, this is C#)

System.Net.WebRequest request= System.Net.WebRequest.Create(srcUrl) ;
request.Credentials = new
System.Net.NetworkCredential(UserName,Password);
request.PreAuthenticate= true; // I know this is going to require
authentication
System.IO.StreamReader sr = new System.IO.StreamReader
(request.GetResponse().GetResponseStream());
.....

-Dino
 
yes, i tried it with credentials... didn´t work :(

but i solved the problem:

public DynDnsUpdateResult UpdateIP(IPAddress 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(AddressFamily.InterNetwork,
SocketType.Stream,ProtocolType.Tcp);
IPHostEntry host = System.Net.Dns.Resolve("members.dyndns.org");
socket.Connect((EndPoint)(new IPEndPoint(host.AddressList[0], 80)));

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

string request = "GET /nic/update?" +
"system=dyndns" +
"&hostname=" + mDomain +
"&myip=" + ip.ToString() +
"&wildcard=" + (mWildcard ? "ON" : "OFF") +
"&offline=NO " +
"HTTP/1.1\r\n" +
"Host: members.dyndns.org\r\n" +
"Authorization: Basic " +
System.Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(mUsername + ":" +
mPassword)) + "\r\n" +
"User-Agent: .net dyndns client\r\n\r\n";

count = socket.Send(System.Text.UnicodeEncoding.ASCII.GetBytes(request));

while((count = socket.Receive(data)) != 0) // Antwort von Server
response += System.Text.ASCIIEncoding.ASCII.GetString(data, 0, count);

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

response = response.Substring(response.IndexOf("\r\n\r\n") + 4); // Html
Header entfernen

switch (response.Substring(0, response.IndexOf(" ")).ToLower())
{
case "good":
// The update was successful, and the hostname is now updated
return DynDnsUpdateResult.UpdatedIp;
case "nochg":
// The update changed no settings, and is considered abusive.
Additional nochg updates will cause the hostname to become blocked
return DynDnsUpdateResult.NoUpdateSameIp;
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("There 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("Unknown result from dyndns service");
}
#endregion
}
 
Back
Top