C#控制TP路由器自动断线、重连
2014-04-07 19:12
531 查看
using guami.route; using System; using System.ComponentModel; using System.Drawing; using System.Text.RegularExpressions; using System.Threading; using System.Windows.Forms; namespace guami { public class setRoute : Form { private delegate void Delegate_do(); private int reLinkJG = 5; private Thread thread = null; private setRoute.Delegate_do delegate_do; private IContainer components = null; private Label label1; private GroupBox groupBox1; private TextBox tb_linkUrl; private Label label3; private Label label2; private TextBox tb_count; private TextBox tb_pwd; private GroupBox groupBox2; private Button btn_reLink; private GroupBox groupBox3; private Label label4; private TextBox tb_reLinkJG; private TextBox tb_autoJG; private Label label7; private Label label6; private Label label5; private Label lb_msg; private CheckBox cb_auto; private GroupBox groupBox4; private Label label8; public setRoute() { this.InitializeComponent(); } private void btn_login_Click(object sender, EventArgs e) { this.startLogin(); } private void btn_reLink_Click(object sender, EventArgs e) { if (this.tb_reLinkJG.Text.Trim() == "") { MessageBox.Show("重新连接时间间隔必需大于5秒!"); this.tb_reLinkJG.Text = "5"; } else { if (Regex.IsMatch(this.tb_reLinkJG.Text.Trim(), "^\\d*$")) { this.reLinkJG = int.Parse(this.tb_reLinkJG.Text.Trim()); if (this.reLinkJG < 5) { MessageBox.Show("重新连接时间间隔必需大于等于5秒!"); this.tb_reLinkJG.Text = "5"; } else { this.startReLine(); } } else { MessageBox.Show("重新连接时间间隔只能为数字!"); } } } private void startLogin() { this.thread = new Thread(new ThreadStart(this.doSomethingForLogin)); this.thread.Start(); } public void doSomethingForLogin() { try { Control.CheckForIllegalCrossThreadCalls = false; this.delegate_do = new setRoute.Delegate_do(this.realLogin); IAsyncResult asyncResult = this.delegate_do.BeginInvoke(null, null); while (!asyncResult.IsCompleted) { Thread.Sleep(1000); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void startReLine() { this.thread = new Thread(new ThreadStart(this.doSomethingForReLine)); this.thread.Start(); } public void doSomethingForReLine() { try { Control.CheckForIllegalCrossThreadCalls = false; this.delegate_do = new setRoute.Delegate_do(this.realReLine); IAsyncResult asyncResult = this.delegate_do.BeginInvoke(null, null); while (!asyncResult.IsCompleted) { Thread.Sleep(1000); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } public void realLogin() { SetRoute.ip = this.tb_linkUrl.Text.Trim(); SetRoute.uid = this.tb_count.Text.Trim(); SetRoute.pwd = this.tb_pwd.Text.Trim(); if (SetRoute.loginTen()) { } } public void realReLine() { SetRoute.ip = this.tb_linkUrl.Text.Trim(); SetRoute.uid = this.tb_count.Text.Trim(); SetRoute.pwd = this.tb_pwd.Text.Trim(); if (SetRoute.breakDown()) { int i = 0; this.lb_msg.Text = "断开成功,等待重新链接(1)...."; while (i < this.reLinkJG) { Thread.Sleep(1000); this.lb_msg.Text = "断开成功,等待重新链接(" + (i + 1) + ")...."; i++; } this.lb_msg.Text = "正在重新链接..."; if (SetRoute.reLink()) { for (int j = 0; j < 4; j++) { this.lb_msg.Text = "正在重新链接(" + (j + 1) + ")...."; Thread.Sleep(1000); } this.lb_msg.Text = "重新连接成功!"; } else { this.setMsg("重新连接失败,请检查登录用户名和密码!", "red"); } } else { this.setMsg("断开连接失败,请检查登录用户名和密码!", "red"); } } private void setMsg(string title, string color) { this.lb_msg.Text = title; if (color == "red") { this.lb_msg.ForeColor = Color.Red; } else { this.lb_msg.ForeColor = Color.Black; } } private void cb_auto_CheckedChanged(object sender, EventArgs e) { if (this.cb_auto.Checked) { this.tb_autoJG.Enabled = true; } else { this.tb_autoJG.Enabled = false; } } protected override void Dispose(bool disposing) { if (disposing && this.components != null) { this.components.Dispose(); } base.Dispose(disposing); } private void InitializeComponent() { this.label1 = new Label(); this.groupBox1 = new GroupBox(); this.tb_pwd = new TextBox(); this.tb_count = new TextBox(); this.label3 = new Label(); this.label2 = new Label(); this.tb_linkUrl = new TextBox(); this.groupBox2 = new GroupBox(); this.btn_reLink = new Button(); this.groupBox3 = new GroupBox(); this.cb_auto = new CheckBox(); this.tb_reLinkJG = new TextBox(); this.tb_autoJG = new TextBox(); this.label7 = new Label(); this.label6 = new Label(); this.label5 = new Label(); this.label4 = new Label(); this.lb_msg = new Label(); this.groupBox4 = new GroupBox(); this.label8 = new Label(); this.groupBox1.SuspendLayout(); this.groupBox2.SuspendLayout(); this.groupBox3.SuspendLayout(); this.groupBox4.SuspendLayout(); base.SuspendLayout(); // this.label1.AutoSize = true; this.label1.Location = new Point(20, 43); this.label1.Name = "label1"; this.label1.Size = new Size(65, 12); this.label1.TabIndex = 0; this.label1.Text = "链接地址:"; this.groupBox1.Controls.Add(this.tb_pwd); this.groupBox1.Controls.Add(this.tb_count); this.groupBox1.Controls.Add(this.label3); this.groupBox1.Controls.Add(this.label2); this.groupBox1.Controls.Add(this.tb_linkUrl); this.groupBox1.Controls.Add(this.label1); this.groupBox1.Location = new Point(17, 29); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new Size(218, 169); this.groupBox1.TabIndex = 1; this.groupBox1.TabStop = false; this.groupBox1.Text = "连接路由器"; this.tb_pwd.Location = new Point(91, 105); this.tb_pwd.Name = "tb_pwd"; this.tb_pwd.Size = new Size(95, 21); this.tb_pwd.TabIndex = 5; this.tb_pwd.Text = "admin"; this.tb_count.Location = new Point(91, 73); this.tb_count.Name = "tb_count"; this.tb_count.Size = new Size(95, 21); this.tb_count.TabIndex = 4; this.tb_count.Text = "admin"; this.label3.AutoSize = true; this.label3.Location = new Point(20, 108); this.label3.Name = "label3"; this.label3.Size = new Size(65, 12); this.label3.TabIndex = 3; this.label3.Text = "登录密码:"; this.label2.AutoSize = true; this.label2.Location = new Point(20, 76); this.label2.Name = "label2"; this.label2.Size = new Size(65, 12); this.label2.TabIndex = 2; this.label2.Text = "登录帐号:"; this.tb_linkUrl.Location = new Point(91, 40); this.tb_linkUrl.Name = "tb_linkUrl"; this.tb_linkUrl.Size = new Size(95, 21); this.tb_linkUrl.TabIndex = 1; this.tb_linkUrl.Text = "192.168.1.1"; this.groupBox2.Controls.Add(this.btn_reLink); this.groupBox2.Location = new Point(17, 220); this.groupBox2.Name = "groupBox2"; this.groupBox2.Size = new Size(514, 139); this.groupBox2.TabIndex = 2; this.groupBox2.TabStop = false; this.groupBox2.Text = "手动操作"; this.btn_reLink.Location = new Point(178, 43); this.btn_reLink.Name = "btn_reLink"; this.btn_reLink.Size = new Size(149, 52); this.btn_reLink.TabIndex = 0; this.btn_reLink.Text = "断开重连"; this.btn_reLink.UseVisualStyleBackColor = true; this.btn_reLink.Click += new EventHandler(this.btn_reLink_Click); this.groupBox3.Controls.Add(this.cb_auto); this.groupBox3.Controls.Add(this.tb_reLinkJG); this.groupBox3.Controls.Add(this.tb_autoJG); this.groupBox3.Controls.Add(this.label7); this.groupBox3.Controls.Add(this.label6); this.groupBox3.Controls.Add(this.label5); this.groupBox3.Controls.Add(this.label4); this.groupBox3.Location = new Point(276, 29); this.groupBox3.Name = "groupBox3"; this.groupBox3.Size = new Size(255, 169); this.groupBox3.TabIndex = 3; this.groupBox3.TabStop = false; this.groupBox3.Text = "连接选项"; this.cb_auto.AutoSize = true; this.cb_auto.Location = new Point(22, 39); this.cb_auto.Name = "cb_auto"; this.cb_auto.Size = new Size(15, 14); this.cb_auto.TabIndex = 8; this.cb_auto.UseVisualStyleBackColor = true; this.cb_auto.Visible = false; this.cb_auto.CheckedChanged += new EventHandler(this.cb_auto_CheckedChanged); this.tb_reLinkJG.Location = new Point(84, 76); this.tb_reLinkJG.Name = "tb_reLinkJG"; this.tb_reLinkJG.Size = new Size(42, 21); this.tb_reLinkJG.TabIndex = 7; this.tb_reLinkJG.Text = "5"; this.tb_autoJG.Enabled = false; this.tb_autoJG.Location = new Point(74, 37); this.tb_autoJG.Name = "tb_autoJG"; this.tb_autoJG.Size = new Size(42, 21); this.tb_autoJG.TabIndex = 6; this.tb_autoJG.Visible = false; this.label7.AutoSize = true; this.label7.Location = new Point(132, 79); this.label7.Name = "label7"; this.label7.Size = new Size(77, 12); this.label7.TabIndex = 5; this.label7.Text = "秒后重新连接"; this.label6.AutoSize = true; this.label6.Location = new Point(122, 40); this.label6.Name = "label6"; this.label6.Size = new Size(89, 12); this.label6.TabIndex = 4; this.label6.Text = "秒自动断开重连"; this.label6.Visible = false; this.label5.AutoSize = true; this.label5.Location = new Point(39, 40); this.label5.Name = "label5"; this.label5.Size = new Size(29, 12); this.label5.TabIndex = 3; this.label5.Text = "每隔"; this.label5.Visible = false; this.label4.AutoSize = true; this.label4.Location = new Point(49, 79); this.label4.Name = "label4"; this.label4.Size = new Size(29, 12); this.label4.TabIndex = 2; this.label4.Text = "断开"; this.lb_msg.AutoSize = true; this.lb_msg.Font = new Font("微软雅黑", 12f, FontStyle.Regular, GraphicsUnit.Point, 134); this.lb_msg.Location = new Point(87, 78); this.lb_msg.Name = "lb_msg"; this.lb_msg.Size = new Size(47, 21); this.lb_msg.TabIndex = 4; this.lb_msg.Text = "就绪!"; this.groupBox4.Controls.Add(this.lb_msg); this.groupBox4.Location = new Point(17, 384); this.groupBox4.Name = "groupBox4"; this.groupBox4.Size = new Size(514, 177); this.groupBox4.TabIndex = 5; this.groupBox4.TabStop = false; this.groupBox4.Text = "提示信息"; this.label8.ForeColor = Color.Red; this.label8.Location = new Point(38, 574); this.label8.Name = "label8"; this.label8.Size = new Size(460, 37); this.label8.TabIndex = 6; this.label8.Text = "说明:由于作者家中使用的是TP-LINK路由器,本功能只在TP-LINK路由器下测试可用 其它路由器是否可用请自行测试"; base.AutoScaleDimensions = new SizeF(6f, 12f); // base.AutoScaleMode = AutoScaleMode.Font; base.ClientSize = new Size(543, 620); base.Controls.Add(this.label8); base.Controls.Add(this.groupBox4); base.Controls.Add(this.groupBox3); base.Controls.Add(this.groupBox2); base.Controls.Add(this.groupBox1); base.Name = "setRoute"; this.Text = "设置路由器"; this.groupBox1.ResumeLayout(false); this.groupBox1.PerformLayout(); this.groupBox2.ResumeLayout(false); this.groupBox3.ResumeLayout(false); this.groupBox3.PerformLayout(); this.groupBox4.ResumeLayout(false); this.groupBox4.PerformLayout(); base.ResumeLayout(false); } } }
C#控制TP路由器自动断线、重连,只针对Tp,其它未测试。
类。
using DotNet4.Utilities; using System; using System.Net; using System.Text; namespace guami.route { internal class SetRoute { private static string userAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"; private static string contentType = "application/x-www-form-urlencoded"; private static bool allowautoredirect = false; private static int timeout = 200000; private static int readWriteTimeout = 30000; private static HttpHelper http = new HttpHelper(); private static CookieCollection cc_gb = new CookieCollection(); private static string cookie_gb = ""; private static string location = ""; public static string ip = ""; public static string uid = ""; public static string pwd = ""; private static bool isLogin = false; private static string postMethod_HttpRquest(string url, string referer, string postData) { HttpItem item = new HttpItem { URL = url, Method = "post", IsToLower = false, Referer = referer, Postdata = postData, Timeout = SetRoute.timeout, Encoding = Encoding.UTF8, ReadWriteTimeout = SetRoute.readWriteTimeout, UserAgent = SetRoute.userAgent, ContentType = SetRoute.contentType, Allowautoredirect = SetRoute.allowautoredirect, ResultCookieType = ResultCookieType.CookieCollection, CookieCollection = SetRoute.cc_gb }; HttpResult html = SetRoute.http.GetHtml(item); if (html.CookieCollection != null) { SetRoute.cc_gb.Add(html.CookieCollection); } return html.Html; } private static string getMethod_HttpRquest(string url, string referer) { HttpItem httpItem = new HttpItem { URL = url, Method = "get", IsToLower = false, Referer = referer, Timeout = SetRoute.timeout, ReadWriteTimeout = SetRoute.readWriteTimeout, UserAgent = SetRoute.userAgent, ContentType = SetRoute.contentType, Allowautoredirect = SetRoute.allowautoredirect, ResultCookieType = ResultCookieType.CookieCollection, CookieCollection = SetRoute.cc_gb }; httpItem.Header.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(SetRoute.uid + ":" + SetRoute.pwd))); HttpResult html = SetRoute.http.GetHtml(httpItem); if (html.CookieCollection != null) { SetRoute.cc_gb.Add(html.CookieCollection); } return html.Html; } public static bool loginTen() { string url = "http://192.168.1.1"; string referer = ""; string method_HttpRquest = SetRoute.getMethod_HttpRquest(url, referer); return !method_HttpRquest.Contains("You have no"); } public static bool breakDown() { string url = string.Concat(new string[] { "http://", SetRoute.ip, "/userRpm/StatusRpm.htm?Disconnect=", SetRoute.EscapeDataString("断 线"), "&wan=1" }); string referer = "http://" + SetRoute.ip + "/userRpm/StatusRpm.htm"; string method_HttpRquest = SetRoute.getMethod_HttpRquest(url, referer); return !method_HttpRquest.Contains("(401) 未经授权") && !method_HttpRquest.Contains("无法连接") && !method_HttpRquest.Contains("You have no"); } public static bool reLink() { string url = string.Concat(new string[] { "http://", SetRoute.ip, "/userRpm/StatusRpm.htm?Connect=", SetRoute.EscapeDataString("连 接"), "&wan=1" }); string referer = string.Concat(new string[] { "http://", SetRoute.ip, "/userRpm/StatusRpm.htm?Disconnect=", SetRoute.EscapeDataString("断 线"), "&wan=1" }); string method_HttpRquest = SetRoute.getMethod_HttpRquest(url, referer); return !method_HttpRquest.Contains("(401) 未经授权") && !method_HttpRquest.Contains("无法连接") && !method_HttpRquest.Contains("You have no"); } private static string EscapeDataString(string param) { return Uri.EscapeDataString(param); } } }使用时一定要用多线程,否则界面可能假死。
相关文章推荐
- TP-LINK路由器自动拨号上网和无线上网设置
- Python实现树莓派WiFi断线自动重连的实例代码
- JavaScript实现HTML5游戏断线自动重连的方法
- 在dd-wrt的路由器系统中实现pppoe空闲自动断线
- c#控制IE浏览器自动点击等事件WebBrowser,mshtml.IHTMLDocument2
- C#中使用Windows API控制阿里旺旺自动登录 的程序源代码
- Netty 4.0在Android 上的使用,长连接断线自动重连
- JavaScript实现H5游戏断线自动重连的技术
- c#控制IE浏览器自动点击等事件WebBrowser,mshtml.IHTMLDocument2
- TP-Link路由器桥接断电后无法重连问题分析与解决分享
- 【c#源码】基于TCP通信的客户端断线重连
- TP路由器断线工具
- .net C# ADSL 断线重连
- C# 判断Socket断线重连
- 自动登录TP-LINK路由器,获取所有信息,重启等等,实用方法 推荐
- 效控制C#中label输出文字的长度,自动换行
- C# 控制 TextBox 输入时若是英文自动转大写或转小写的方法
- android xmpp开发 asmack实现断线自动重连
- C#中使用Windows API控制阿里旺旺自动登录 的程序源代码
- c#控制IE浏览器自动点击等事件WebBrowser,mshtml.IHTMLDocument2