用C#重启水星无线路由一体机MD898N

本代码用于远程重启水星无线路由一体机MD898N,硬件版本v2,固件版本0.8.0 1.0 v1003.0 Build 140216。

由于需要,需要用C#实现对这台机器的定时重启,根据chrome开发者工具的抓包结果,发现新版的固件不支持直接的base认证,但是原理一样,只是把用base64加密过的密码存在cookies中了(今年开始更新后的固件都是这种登录方式),发送http请求时候带上这个cookies即可通过认证!

具体代码如下:

	string passwd = "12345678";   // 路由器登录密码
	string routeIP = "192.168.1.1";  // 路由器IP
	byte[] bytes = Encoding.Default.GetBytes(passwd);
	string base64str = Convert.ToBase64String(bytes);
 
	string url = "http://" + ip + "/cgi?7";
	HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
	req.Referer = "http://" + ip + "/";
	req.Method = "POST";
	req.CookieContainer = new CookieContainer();
	req.CookieContainer.Add(new Uri(url), new Cookie("Authorization", "Basic " + base64str));
	req.ContentType = "text/plain";
	string payload = "[ACT_REBOOT#0,0,0,0,0,0#0,0,0,0,0,0]0,0\r\n";  // 抓包后发现在请求中携带这个字符串即可让路由器重启
	byte[] pl = Encoding.UTF8.GetBytes(payload);
	req.ContentLength = pl.Length;
	Stream s = req.GetRequestStream();
	s.Write(pl, 0, pl.Length);
	s.Close();
	req.GetResponse();