using DotNet.Utilities;
using System;using System.Collections.Generic;using System.Linq;using System.Net.NetworkInformation;using System.Text;using System.Threading;using System.Threading.Tasks;namespace reboot2
{ class Program { static void Main(string[] args) {Disconnect();
Thread.Sleep(100); connect(); while (true) { try { if (IsConnectedPing()) //if(IsConnectedToInternet()) { Console.WriteLine("\nConnectedToInternet ...\n"); break; } } catch { Console.WriteLine("\n\nerror ...\n"); } Thread.Sleep(1000); } ip(); // Console.ReadLine(); }private static void ip()
{//
var _UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0"; HttpHelper http = new HttpHelper(); var _Referer = "http://www.ip138.com"; HttpItem item = new HttpItem() { Encoding = Encoding.Default,//编码格式(utf-8,gb2312,gbk) 可选项 默认类会自动识别 URL = "http://1111.ip138.com/ic.asp",//URL这里都是测试 必需项 Method = "get",//URL 可选项 默认为Get UserAgent = _UserAgent, Referer = _Referer }; //得到HTML代码 HttpResult result = http.GetHtml(item); Console.WriteLine(result.Html); } private static void Disconnect() { var url = "http://192.168.1.1/userRpm/StatusRpm.htm?Disconnect=%B6%CF%20%CF%DF&wan=1"; var cookie = "Authorization=Basic%20YWRtaW46cWluaGFuMjAxNA%3D%3D; ChgPwdSubTag="; var _UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0"; var _Referer = "http://192.168.1.1/userRpm/StatusRpm.htm"; HttpHelper http = new HttpHelper(); HttpItem item = new HttpItem() { Cookie = cookie, Encoding = Encoding.Default,//编码格式(utf-8,gb2312,gbk) 可选项 默认类会自动识别 URL = url,//URL这里都是测试 必需项 Method = "get",//URL 可选项 默认为Get UserAgent = _UserAgent, Referer = _Referer }; //得到HTML代码 HttpResult result = http.GetHtml(item); Console.WriteLine("Disconnect...."); // Console.ReadLine();}
private static void connect() { var url = "http://192.168.1.1/userRpm/StatusRpm.htm?Connect=%C1%AC%20%BD%D3&wan=1"; var cookie = "Authorization=Basic%20YWRtaW46cWluaGFuMjAxNA%3D%3D; ChgPwdSubTag="; var _UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0"; var _Referer = "http://192.168.1.1/userRpm/StatusRpm.htm?Disconnect=%B6%CF%20%CF%DF&wan=1"; HttpHelper http = new HttpHelper(); HttpItem item = new HttpItem() { Cookie = cookie, Encoding = Encoding.Default,//编码格式(utf-8,gb2312,gbk) 可选项 默认类会自动识别 URL = url,//URL这里都是测试 必需项 Method = "get",//URL 可选项 默认为Get UserAgent = _UserAgent, Referer = _Referer }; //得到HTML代码 HttpResult result = http.GetHtml(item); Console.WriteLine("connect...."); // Console.WriteLine(result.Html); // Console.ReadLine();}
public static bool IsConnectedPing() { Console.WriteLine("weixin.sogou.com"); string hostNameOrAddress = "weixin.sogou.com"; Ping ping = new Ping(); PingReply pingReply = ping.Send(hostNameOrAddress); if (pingReply.Status == IPStatus.Success) { Console.WriteLine("网络连接成功, 执行下面任务..."); ping.Dispose(); return true; } int i = 0; while (i < 12) { Thread.Sleep(50); pingReply = ping.Send(hostNameOrAddress); Console.WriteLine(pingReply.Status); i++; if (pingReply.Status == IPStatus.Success) { ping.Dispose(); return true; } } Console.WriteLine("重新尝试连接超过12次,连接失败程序结束"); ping.Dispose(); return false; } }}