如何使用asp.net检查网址是否存在? [英] How to check if a web url exists using asp.net?

查看:111
本文介绍了如何使用asp.net检查网址是否存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用asp.net检查网址是否存在?



如何准确使用以下代码段?



How to check if a web url exists, using asp.net?

how to use below snippet accurately?

public bool urlExists(string url)
{
   HttpWebRequest req  = (HttpWebRequest)WebRequest.Create(url);
   req.AllowRedirect = false;
   HttpWebResponse res = (HttpWebResponse)req.GetResponse();

   //How to check if url exists - code??

   if(/*exists code*/)
      return true;
   else
      return false;
} 

推荐答案

检查HttpStatus。它是Response对象的属性。



最好的问候,



MRB
Check the HttpStatus. It is a property of the Response object.

Best Regards,

MRB






我尝试了一些完美的代码如果有url它会显示结果否则

显示msgs。



Hi,

I tried some code it works perfect If there is url it shows results otherwise
shows msgs.

   WebRequest req = WebRequest.Create("http://www.codeproject.com");
        WebResponse res;
        try
        {
           res = req.GetResponse();
        }
        catch (Exception ed)
        {
            goto sd;
        }
        Stream sg  = res.GetResponseStream();
     
        StreamReader sr = new StreamReader(sg);
        resdiv.InnerHtml = sr.ReadToEnd();
goto fg;
    sd:
        resdiv.InnerHtml = "Sorry no such url exists ";
        ;  
fg:;







上述代码的含义是如果url存在则存在如果它不存在就没有例外它会提升webnull Execption。



我们处理这个例子,因为如果url不存在我们写下我们要做的事情。





我希望你能理解我说的话。





一切顺利




The meaning of above code is if url has exists there is no exeception if it not exists it''ll raise webnull Execption.

We handle that execption in that we write what we''ve to do if url not exists.


I hope you''ll understand what i said.


All the best


您好,



您可以查看网址是否存在或者不是关注网址。



public bool urlExists(string url)

{

HttpWebRequest req =(HttpWebRequest)WebRequest.Create( url);

req.AllowAutoRedirect = false;

HttpWebResponse res =(HttpWebResponse)req.GetResponse();



if(res.StatusCode == HttpStatusCode.OK)

返回true;

else

返回false;

}
Hello,

You can check the web url is exist or not on following url.

public bool urlExists(string url)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.AllowAutoRedirect = false;
HttpWebResponse res = (HttpWebResponse)req.GetResponse();

if (res.StatusCode == HttpStatusCode.OK)
return true;
else
return false;
}


这篇关于如何使用asp.net检查网址是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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