永发信息网

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

答案:2  悬赏:10  手机版
解决时间 2021-01-27 14:15
  • 提问者网友:謫仙
  • 2021-01-26 23:17
用C#如何做一个检测网站的URL是否连接正常的小工具,有下载地址更好
最佳答案
  • 五星知识达人网友:渡鹤影
  • 2021-01-27 00:46
先获取到页面上所有的超链接(c#获取页面源代码,正则获取超链接),然后再根据超链接去获取源代码,获取成功则超链接有效,否则失效。
全部回答
  • 1楼网友:过活
  • 2021-01-27 01:11
首先写一个类 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;
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯