At home, I have a DSL connection which gets a different IP address every time I connect so I sometimes use the free dynamic DNS service. To update your registered DynDNS hostname with your current IP address, you need to send it a simple HTTP request conforming to their published specs.
I’ve been using a small tool I’d written to automate this task, using Delphi (of course!) and Indy with OpenSSL.
Today I tried to write a similar tool in C# and I was surprised how easy it was:
IPHostEntry hostInfo = Dns.Resolve(Dns.GetHostName());
if (hostInfo.AddressList.Length > 0)
{
string ipAddress = hostInfo.AddressList[0].ToString();
string url = "https://members.dyndns.org/nic/update" +
"?system=dyndns" +
"&hostname=" + userName + "." + hostName +
"&myip=" + ipAddress +
"&wildcard=OFF" +
"&mx=" +
"&backmx=NO" +
"&offline=NO";
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);
request.Credentials = new NetworkCredential(userName, password);
request.UserAgent = "tondrej dyndns test/1.0 tondrej@plonk.net";
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
Console.WriteLine(response.StatusCode);
}
SSL is used automagically just by using the "https://" prefix!
Just out of curiosity, I also tried it in Mono 1.0 on Linux (I use SuSE Linux 9.1). Unfortunately, the above code to retrieve my current IP address didn’t work. Dns.Resolve() method seems to work differently in Mono: when I passed it my hostname it returned 127.0.0.2; when I passed it “localhost” it returned 127.0.0.1 (BTW, see this cool t-shirt).
I’ll try to come up with code which works in Mono, however, this short experience showed me that it was probably naive to expect it to be fully compatible with Microsoft’s .NET. For cross-platform code, you would probably get better results using just Mono on all target platforms (Mono runs on Windows, too).