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

荒凉一梦 提交于 2019-12-23 03:19:15

问题


I am using a WebRequest to check if a web page or media (image) exist. On GetResponse i get a System.Net.WebException exception. I ran through 100 links and it feels like its going slower then it should. Is there a way to not get this exception or handle this more gracefully?

    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:


Try setting

wreq.Method = "Head";

after the "KeepAlive" line. If the webserver you are calling is smart enough, that will tell it not to return any body contents which should save some time.



来源:https://stackoverflow.com/questions/753877/webrequest-and-system-net-webexception-on-404-slow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!