希网动态域名开放更新协议,官方提供的客户端不支持 以服务方式更新IP, 所以写了个.NET的系统服务.
希望大家喜欢
希望大家喜欢
把下面源码存为 cn99.cs 修改对应的帐号, 再用下面的命令行编译,安装为服务,就行了.
- using System;
- using System.Net;
- using System.Collections;
- using System.ComponentModel;
- using System.Diagnostics;
- using System.ServiceProcess;
- using System.Configuration.Install;
- using System.Text;
- using System.Timers;
- namespace Shelly
- {
- // 应用程序
- public class cn99Update : System.ServiceProcess.ServiceBase
- {
- // 定时器
- Timer timer = new Timer();
- string URL;
- HttpWebRequest request = null;
- HttpWebResponse res = null;
- // 域 需要 urlencode
- string domain = "domain%2E3322%2Eorg";
- // 帐号
- string user = "user";
- // 密码
- string pass = "pass";
- // 更新时间 10 分钟
- int upts = 600000;
- public cn99Update()
- {
- this.CanPauseAndContinue = true;
- this.CanShutdown = true;
- this.CanHandleSessionChangeEvent = false;
- this.ServiceName = "cn99Update";
- }
- private void updateIPAddress(object s, ElapsedEventArgs e){
- try{
- URL = "http://members.3322.org/dyndns/update?system=dyndns&hostname="+domain+"&wildcard=ON&mx=&backmx=NO&offline=NO";
- request = HttpWebRequest.Create(URL) as HttpWebRequest;
- request.AllowAutoRedirect = false;
- request.Timeout = 20000;
- request.Headers.Add("Authorization", "Basic "+Convert.ToBase64String(Encoding.GetEncoding("utf-8").GetBytes(user+":"+pass)));
- res = (HttpWebResponse)request.GetResponse();
- res.Close();
- request = null;
- res = null;
- }catch(WebException){
- }
- }
- protected override void OnStart(string[] args){
- timer.Elapsed += new ElapsedEventHandler(updateIPAddress);
- timer.Interval = upts;
- timer.Enabled = true;
- base.OnStart( args );
- }
- protected override void OnStop(){
- timer.Enabled = false;
- }
- protected override void OnContinue(){
- }
- //启动
- public static void Main()
- {
- cn99Update insCn99 = new cn99Update();
- ServiceBase[] servicesToRun = new ServiceBase[] {insCn99};
- ServiceBase.Run(servicesToRun);
- }
- }
- //安装
- [RunInstaller(true)]
- public class ProjectInstaller : System.Configuration.Install.Installer
- {
- private ServiceProcessInstaller myServiceProcessInstaller;
- private ServiceInstaller myServiceInstaller;
- public ProjectInstaller()
- {
- this.myServiceProcessInstaller = new ServiceProcessInstaller();
- this.myServiceInstaller = new ServiceInstaller();
- // 安装
- // 用户名 和 密码
- this.myServiceProcessInstaller.Account = ServiceAccount.LocalSystem;
- this.myServiceProcessInstaller.Username = null;
- this.myServiceProcessInstaller.Password = null;
- // 服务名称,这样可以在net stop XX 里面使用了
- // 启动类型
- this.myServiceInstaller.ServiceName = "cn99Update";
- this.myServiceInstaller.StartType = ServiceStartMode.Automatic;
- // 加入
- this.Installers.AddRange(new Installer[] {this.myServiceProcessInstaller, this.myServiceInstaller});
- }
- }
- }
- /*
- 附: 编译批处理
- @echo off
- path=%path%;c:/WINDOWS/Microsoft.NET/Framework/v2.0.50727
- csc /t:winexe /out:cn99.exe cn99.cs
- rem installutil cn99.exe
- */
- /*
- 安装服务
- installutil cn99.exe
- 卸载服务
- installutil /u cn99.exe
- */
如果觉得麻烦,点击 这里下载