404上的WebRequest和System.Net.WebException,慢?
我正在使用WebRequest检查是否存在网页或媒体(图像)。 在GetResponse上,我得到一个System.Net.WebExceptionexception。 我跑了100个链接,感觉就像它应该慢一点。 有没有办法不得到这个例外或更优雅地处理这个?
static public bool CheckExist(string url) { HttpWebRequest wreq = null; HttpWebResponse wresp = null; bool ret = false; try { wreq = (HttpWebRequest)WebRequest.Create(url); wreq.KeepAlive = true; wresp = (HttpWebResponse)wreq.GetResponse(); ret = true; } catch (System.Net.WebException) { } finally { if (wresp != null) wresp.Close(); } return ret; }
尝试设置
wreq.Method = "Head";
在“KeepAlive”行之后。 如果您正在呼叫的网络服务器足够智能,那将告诉它不要返回任何应该节省一些时间的正文内容。