WebRequest and System.Net.WebException at 404, slow?

I use WebRequest to check for the presence of a web page or media (image). In GetResponse, I get a System.Net.WebException exception. I ran 100 links and it seems to me that it is going slower than necessary. Is there a way to not get this exception or handle it more elegantly?

    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
source share
1 answer

Try to install

wreq.Method = "Head";

after the line "KeepAlive". If the web server you are calling to is smart enough to not return body content, which should save some time.

+2
source

All Articles