永发信息网

用C#如何做一个检测网站的URL是否连接正常的小工具,有下载地址更好

答案:2  悬赏:10  手机版
解决时间 2021-01-27 14:15
用C#如何做一个检测网站的URL是否连接正常的小工具,有下载地址更好
最佳答案
先获取到页面上所有的超链接(c#获取页面源代码,正则获取超链接),然后再根据超链接去获取源代码,获取成功则超链接有效,否则失效。
全部回答
首先写一个类 using system; using system.web; using system.net.sockets; using system.runtime.interopservices; using system.threading; namespace test { public class internet { #region 利用api方式获取网络链接状态 private static int network_alive_lan = 0x00000001; private static int network_alive_wan = 0x00000002; private static int network_alive_aol = 0x00000004; [dllimport("sensapi.dll")] private extern static bool isnetworkalive(ref int flags); [dllimport("sensapi.dll")] private extern static bool isdestinationreachable(string dest, intptr ptr); [dllimport("wininet.dll")] private extern static bool internetgetconnectedstate(out int connectiondescription, int reservedvalue); public internet() { } public static bool isconnected() { int desc = 0; bool state = internetgetconnectedstate(out desc, 0); return state; } public static bool islanalive() { return isnetworkalive(ref network_alive_lan); } public static bool iswanalive() { return isnetworkalive(ref network_alive_wan); } public static bool isaolalive() { return isnetworkalive(ref network_alive_aol); } public static bool isdestinationalive(string destination) { return (isdestinationreachable(destination, intptr.zero)); } #endregion /// <summary> /// 在指定时间内尝试连接指定主机上的指定端口。 (默认端口:80,默认链接超时:5000毫秒) /// </summary> /// <param name="hostnameorip">主机名称或者ip地址</param> /// <param name="port">端口</param> /// <param name="timeout">超时时间</param> /// <returns>返回布尔类型</returns> public static bool ishostalive(string hostnameorip, int? port, int? timeout) { tcpclient tc = new tcpclient(); tc.sendtimeout = timeout ?? 5000; tc.receivetimeout = timeout ?? 5000; bool isalive; try { tc.connect(hostnameorip, port ?? 80); isalive = true; } catch { isalive = false; } finally { tc.close(); } return isalive; } /// <summary> /// 在指定时间内尝试连接指定主机上的指定端口。 (默认端口:80,默认链接超时:5000毫秒) /// </summary> /// <param name= "hostname ">要连接到的远程主机的 dns 名。</param> /// <param name= "port ">要连接到的远程主机的端口号。 </param> /// <param name= "millisecondstimeout ">要等待的毫秒数,或 -1 表示无限期等待。</param> /// <returns>已连接的一个 tcpclient 实例。</returns> public static tcpclient connect(string hostname, int? port, int? millisecondstimeout) { try { connectorstate cs = new connectorstate(); cs.hostname = hostname; cs.port = port ?? 80; threadpool.queueuserworkitem(new waitcallback(connectthreaded), cs); if (cs.completed.waitone(millisecondstimeout ?? 5000, false)) { if (cs.tcpclient != null) return cs.tcpclient; return null; } else { cs.abort(); return null; } } catch { return null; } } private static void connectthreaded(object state) { connectorstate cs = (connectorstate)state; cs.thread = thread.currentthread; try { tcpclient tc = new tcpclient(cs.hostname, cs.port); if (cs.aborted) { try { tc.getstream().close(); } catch { } try { tc.close(); } catch { } } else { cs.tcpclient = tc; cs.completed.set(); } } catch (exception e) { cs.exception = e; cs.completed.set(); } } private class connectorstate { public string hostname; public int port; public volatile thread thread; public readonly manualresetevent completed = new manualresetevent(false); public volatile tcpclient tcpclient; public volatile exception exception; public volatile bool aborted; public void abort() { if (aborted != true) { aborted = true; try { thread.abort(); } catch { } } } } } } 使用方法 bool ishostalive;//表示是否正常 try { tcpclient tc = internet.connect("要检测的链接地址", null, 2000); //这里的2000 表示去连接地址2秒 如果2秒内不能访问到 表示认为不正常 //检测网络联通状态 if (tc != null) { tc.getstream().close(); tc.close(); ishostalive = true; } else { ishostalive = false; } } catch { ishostalive = false; } 命名空间 using system; using system.collections.generic; using system.windows.forms; using system.diagnostics; using system.reflection; using system.net.networkinformation; using system.web; using system.net.sockets;
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
电动车一开起来就停不了,要关钥匙才能停
用简便方法计算(2+4+6+......+2006)-(1+3+
佳美布庄地址在什么地方,想过去办事
顼冥的意思是什么啊?知道的请说下!
分式的值为0,则x等于A.x=2或x=-1B.x=-1C.x=
从山东济南到广州车程需多少时间?
北京现代ix35冷却风扇打开钥匙就转是什么原因
2002年春,上海市在进行取消统购粮后的城市新
视贝电工·照明地址在什么地方,想过去办
路过地址好找么,我有些事要过去
毞毲的意思是什么啊?知道的请说下!
汽车点火不行驶
柳树和水杉哪种更容易进行组织培养要有判断依
能解释一下其中的OD是什么意思吗
嵩县实验中学成绩单
推荐资讯
通风与空调工程施工程序中,风管系统安装之后
蹊岖的意思是什么啊?知道的请说下!
工业园区中段地址好找么,我有些事要过去
【小兔运南瓜的故事】小兔运南瓜(小故事)作文
泰乙的意思是什么啊?知道的请说下!
(1/2)给下列句子中括号里的字词注音并解释。
天博网苑地址好找么,我有些事要过去
长方形,长18米,宽比长短6米,求周长和面积
龙海市程溪镇东马村幼儿园地址在什么地方,我
B2证要多久可以开车上高速
这好像是个铜箭头。有价值吗?
宅心的意思是什么啊?知道的请说下!
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?