现在的位置: 首页 > 综合 > 正文

希网动态域名 客户端(系统服务)

2013年10月11日 ⁄ 综合 ⁄ 共 3531字 ⁄ 字号 评论关闭
希网动态域名开放更新协议,官方提供的客户端不支持 以服务方式更新IP, 所以写了个.NET的系统服务. 
希望大家喜欢

把下面源码存为  cn99.cs 修改对应的帐号, 再用下面的命令行编译,安装为服务,就行了.
  1. using System;
  2. using System.Net;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Diagnostics;
  6. using System.ServiceProcess;
  7. using System.Configuration.Install;
  8. using System.Text;
  9. using System.Timers;
  10. namespace Shelly
  11. {
  12.     // 应用程序
  13.     public class cn99Update : System.ServiceProcess.ServiceBase
  14.     {
  15.         // 定时器
  16.         Timer timer = new Timer();
  17.         string URL;
  18.         HttpWebRequest request = null;
  19.         HttpWebResponse res = null;
  20.         // 域 需要 urlencode
  21.         string domain = "domain%2E3322%2Eorg";
  22.         // 帐号
  23.         string user = "user";
  24.         // 密码
  25.         string pass = "pass";
  26.         // 更新时间 10 分钟
  27.         int upts = 600000;
  28.         public cn99Update()
  29.         {
  30.             this.CanPauseAndContinue = true;
  31.             this.CanShutdown = true;
  32.             this.CanHandleSessionChangeEvent = false;
  33.             this.ServiceName = "cn99Update";
  34.         }
  35.         private void updateIPAddress(object s, ElapsedEventArgs e){
  36.             try{
  37.                 URL = "http://members.3322.org/dyndns/update?system=dyndns&hostname="+domain+"&wildcard=ON&mx=&backmx=NO&offline=NO";
  38.                 request = HttpWebRequest.Create(URL) as HttpWebRequest;
  39.                 request.AllowAutoRedirect = false;
  40.                 request.Timeout = 20000;
  41.                 request.Headers.Add("Authorization""Basic "+Convert.ToBase64String(Encoding.GetEncoding("utf-8").GetBytes(user+":"+pass)));
  42.                 res = (HttpWebResponse)request.GetResponse();
  43.                 res.Close();
  44.                 request = null;
  45.                 res = null;
  46.             }catch(WebException){
  47.             }
  48.         }
  49.         protected override void OnStart(string[] args){
  50.             timer.Elapsed += new ElapsedEventHandler(updateIPAddress);
  51.             timer.Interval = upts;
  52.             timer.Enabled = true;
  53.             base.OnStart( args );
  54.         }
  55.         protected override void OnStop(){
  56.             timer.Enabled = false;
  57.         }
  58.         protected override void OnContinue(){
  59.         }
  60.         //启动
  61.         public static void Main()
  62.         {
  63.             cn99Update insCn99 = new cn99Update();
  64.             ServiceBase[] servicesToRun = new ServiceBase[] {insCn99};
  65.             ServiceBase.Run(servicesToRun);
  66.         }
  67.     }
  68.  
  69.     //安装
  70.     [RunInstaller(true)]
  71.     public class ProjectInstaller : System.Configuration.Install.Installer
  72.     {
  73.         private ServiceProcessInstaller myServiceProcessInstaller;
  74.         private ServiceInstaller myServiceInstaller;
  75.  
  76.         public ProjectInstaller()
  77.         {
  78.             this.myServiceProcessInstaller = new ServiceProcessInstaller();
  79.             this.myServiceInstaller = new ServiceInstaller();
  80.  
  81.             // 安装
  82.             // 用户名 和 密码
  83.             this.myServiceProcessInstaller.Account = ServiceAccount.LocalSystem;
  84.             this.myServiceProcessInstaller.Username = null;
  85.             this.myServiceProcessInstaller.Password = null;
  86.  
  87.             // 服务名称,这样可以在net stop XX 里面使用了
  88.             // 启动类型
  89.             this.myServiceInstaller.ServiceName = "cn99Update";
  90.             this.myServiceInstaller.StartType = ServiceStartMode.Automatic;
  91.  
  92.             // 加入
  93.             this.Installers.AddRange(new Installer[] {this.myServiceProcessInstaller, this.myServiceInstaller});
  94.         }
  95.     }
  96. }
  97. /*
  98. 附: 编译批处理
  99. @echo off
  100. path=%path%;c:/WINDOWS/Microsoft.NET/Framework/v2.0.50727
  101. csc /t:winexe /out:cn99.exe cn99.cs 
  102. rem  installutil cn99.exe
  103. */
  104. /*
  105. 安装服务 
  106. installutil cn99.exe
  107. 卸载服务
  108. installutil /u cn99.exe
  109. */

如果觉得麻烦,点击 这里下载

抱歉!评论已关闭.