404上的WebRequest和System.Net.WebException,慢吗?

时间:2009-04-15 21:51:53

标签: c# httpwebrequest

我正在使用WebRequest来检查是否存在网页或媒体(图像)。在GetResponse上,我得到一个System.Net.WebException异常。我跑了100个链接,感觉就像它应该慢一点。有没有办法不得到这个例外或更优雅地处理它?<​​/ p>

    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;
    }

1 个答案:

答案 0 :(得分:2)

尝试设置

wreq.Method = "Head";

在“KeepAlive”行之后。如果您正在呼叫的网络服务器足够智能,那将告诉它不要返回任何可以节省一些时间的正文内容。

相关问题