C# 404上的WebRequest和System.Net.WebException是否慢?
我使用WebRequest检查网页或媒体(图像)是否存在。在GetResponse上,我得到一个System.Net.WebException异常。我浏览了100个链接,感觉速度比应该的慢。有没有办法不获取此异常或更优雅地处理此异常C# 404上的WebRequest和System.Net.WebException是否慢?,c#,httpwebrequest,C#,Httpwebrequest,我使用WebRequest检查网页或媒体(图像)是否存在。在GetResponse上,我得到一个System.Net.WebException异常。我浏览了100个链接,感觉速度比应该的慢。有没有办法不获取此异常或更优雅地处理此异常 static public bool CheckExist(string url) { HttpWebRequest wreq = null; HttpWebResponse wresp = null;
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”行之后。如果您正在调用的Web服务器足够智能,这将告诉它不要返回任何正文内容,这将节省一些时间。我似乎无法使其正常工作,这仍然会引发异常:var req=(HttpWebRequest)WebRequest.Create(“);req.Method=“Head”;using(HttpWebResponse=(HttpWebResponse)req.GetResponse(){}这是经过设计的。如果服务器返回404,那么.Net将引发WebException。好的,这篇文章是从另一篇文章链接而来的,说设置head将导致它不会引发异常。无需担心,然后:)