的WebRequest和System.Net.WebException上404,慢? [英] WebRequest and System.Net.WebException on 404, slow?

查看:279
本文介绍了的WebRequest和System.Net.WebException上404,慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是WebRequest的检查,如果存在一个网页或媒体(图像)。在我的GetResponse获得System.Net.WebException异常。我通过100链接跑,感觉像它会慢那么它应该。有没有办法不得到这个异常,或者这更妥善地处理?



 的静态公共BOOL CheckExist(字符串URL)
{
HttpWebRequest的WREQ = NULL;
HttpWebResponse wresp = NULL;
布尔RET = FALSE;

{
WREQ =(HttpWebRequest的)WebRequest.Create(URL);
wreq.KeepAlive = TRUE;
wresp =(HttpWebResponse)wreq.GetResponse();
RET = TRUE;
}
赶上(System.Net.WebException)
{
}
终于
{
如果(wresp!= NULL)
wresp.Close();
}
返回RET;
}


解决方案

尝试设置



  wreq.Method =头; 



的KeepAlive行之后。如果您呼叫的网络服务器是足够聪明,会告诉它不返回任何正文内容应该节省一些时间。


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

解决方案

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.

这篇关于的WebRequest和System.Net.WebException上404,慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆