C# 重启路由器问题TL-WR842N型号的路由器

huangyongjin_1982 2015-06-18 01:57:54
HttpWebRequest request = null;
HttpWebResponse response = null;
string gethost = string.Empty;
CookieContainer cc = new CookieContainer();
string Cookiesstr = string.Empty;
try
{
gethost = "http://192.168.1.1/userRpm/SysRebootRpm.htm?Reboot=%D6%D8%C6%F4%C2%B7%D3%C9%C6%F7"; //路由器重启的地址
request = (HttpWebRequest)WebRequest.Create(gethost);
request.Method = "GET";
request.KeepAlive = true;
request.Headers.Add("Authorization", "Basic YWRtaW46YWRtaW4=" );//授权的请求
request.CookieContainer = cc;
request.AllowAutoRedirect = false;
response = (HttpWebResponse)request.GetResponse();
//设置cookie
Cookiesstr = request.CookieContainer.GetCookieHeader(request.RequestUri); //取再次跳转链接
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("gb2312"));
string content = sr.ReadToEnd();
request.Abort();
sr.Close();
response.Close();
}
catch (Exception e)
{
Console.WriteLine("路由器重启失败!");
}

我这样重启不了啊
...全文
811 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
by_封爱 版主 2015-06-18
  • 打赏
  • 举报
回复
先抓包.. 然后 看以下代码..

            string name = txt_name.Text;
            string pwd = txt_pass.Text;
            string url = txt_ip.Text;
            CookieContainer cookie = new CookieContainer();
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req = (HttpWebRequest)WebRequest.Create(url + "/advanced.htm?wifi_mode=2&wifi_ssid=MXCHIP_42524A&security_mode=2&wifi_key=&wifi_ssid1=dosoft&security_mode1=0&wifi_key1=&wifi_ssid2=&security_mode2=0&wifi_key2=&wifi_ssid3=&security_mode3=0&wifi_key3=&wifi_ssid4=&security_mode4=0&wifi_key4=&uap_ssid=&uap_secmode=1&uap_key=&socket_mode=0&dhcp_enalbe=0&local_ip_addr=192.168.1.1&netmask=255.255.255.0&gateway_ip_addr=192.168.1.1&dns_server=192.168.1.1&remote_server_mode=0&remote_dns=114.91.236.155&rport=8080&lport=8080&udp_enalbe=0&estype=4&esaddr=&esrport=0&eslport=0&baudrate=0&parity=0&data_length=0&stop_bits=0&cts_rts_enalbe=0&dma_buffer_size=0&uart_trans_mode=4&device_num=0&ps_enalbe=0&tx_power=31&keepalive_num=4&keepalive_time=120&socks_type=0&socks_addr=0.0.0.0&socks_port=0&socks_user=&socks_pass=&socks_1=0&socks_2=0&web_user=admin&web_pass=admin&device_name=EMW_3162+%2842524A%29&roam_val=75&udp_enable=0&save=Save");
            req.Method = "POST";
            req.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; qdesk 2.5.1269.201; Windows NT 6.1; Trident/5.0)";
            req.CookieContainer = cookie;
            req.KeepAlive = true;
            req.Accept = "text/html, application/xhtml+xml, */*";
            req.PreAuthenticate = true;
            CredentialCache cache = new CredentialCache();
            cache.Add(new Uri(url), "Basic", new NetworkCredential(name, pwd));
            req.Credentials = cache;
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            res.Cookies = cookie.GetCookies(req.RequestUri);

            using (var io = new System.IO.StreamReader(res.GetResponseStream(), System.Text.Encoding.Default))
            {
                var str = io.ReadToEnd();
                Console.WriteLine(str);
                io.Close();
            }
            res.Close();
其中

            req.PreAuthenticate = true;
            CredentialCache cache = new CredentialCache();
            cache.Add(new Uri(url), "Basic", new NetworkCredential(name, pwd));
            req.Credentials = cache;
是比较重要的代码了.也就是模拟弹出对话框那种登陆...
huangyongjin_1982 2015-06-18
  • 打赏
  • 举报
回复
有没有什么好的方法可以重启路由器吗?
於黾 2015-06-18
  • 打赏
  • 举报
回复
路由器需要登录的

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧