检查是否网址可以访问 - 帮助优化类 [英] Check if a url is reachable - Help in optimizing a Class

查看:133
本文介绍了检查是否网址可以访问 - 帮助优化类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

净4和C#。

我需要能够返回一个布尔值一类如果乌里(字符串)返回 HTTP状态$ C $ 200 CS

目前,我有这个code(其工作中使用尝试,看看是否可以连接到URI),但我会等来实现的HTTPStatus code.OK代替。

我尝试过不同的时间,但我不能做到这一点。


  • 您能够解决它,并提供code的样本?

  • 或者你知道一个更好的计算策略?

感谢您的帮助。

 公共静态布尔IsReachableUri(字符串uriInput)
        {
            //可变退换货
            布尔testStatus;
            //创建用于URL的请求。
            WebRequest的请求= WebRequest.Create(uriInput);
            request.Timeout = 15000; // 15秒            WebResponse的响应;
            尝试
            {
                响应= request.GetResponse();
                testStatus =真; //乌里确实存在
                response.Close();
            }
            赶上(例外)
            {
                testStatus = FALSE; //乌里不存在
            }
            //结果
            返回testStatus;
        }


解决方案

嗯,首先这将是更好的为您的响应使用语句,而不是只调用关闭 - 在这种情况下,有没有太大的区别,但一般使用语句是要走的路。

至于测试结果状态 - 只投给 HttpWebResponse 的响应,然后使用<$c$c>Status$c$c属性。事情是这样的:

  HttpWebRequest的要求=(HttpWebRequest的)WebRequest.Create(URL);
request.Timeout = 15000;
request.Method =HEAD; //按拉塞的评论
尝试
{
    使用(HttpWebResponse响应=(HttpWebResponse)request.GetResponse())
    {
        返回response.Status code ==的HTTPStatus code.OK;
    }
}
赶上(引发WebException)
{
    返回false;
}

net 4 and c#.

I need a Class able to return a Bool value if an Uri (string) return HTTP status codes 200.

At the moment I have this code (it work using try to see if it is possible connect to the Uri) but I would like implemented with "HttpStatusCode.OK" instead.

I tried different time but I am not able to do it.

  • Are you able to solve it and provide a sample of code?
  • Or do you know a better approch?

Thanks for your help..

public static bool IsReachableUri(string uriInput)
        {
            // Variable to Return
            bool testStatus;
            // Create a request for the URL.
            WebRequest request = WebRequest.Create(uriInput);
            request.Timeout = 15000; // 15 Sec

            WebResponse response;
            try
            {
                response = request.GetResponse();
                testStatus = true; // Uri does exist                 
                response.Close();
            }
            catch (Exception)
            {
                testStatus = false; // Uri does not exist
            }
            // Result
            return testStatus;
        }

解决方案

Well, firstly it would be better to have a using statement for your response instead of just calling Close - in this case there's not much difference, but in general using statements are the way to go.

As for testing the result status - just cast the response to HttpWebResponse and then use the StatusCode property. Something like this:

HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);
request.Timeout = 15000;
request.Method = "HEAD"; // As per Lasse's comment
try
{
    using (HttpWebResponse response = (HttpWebResponse) request.GetResponse())
    {
        return response.StatusCode == HttpStatusCode.OK;
    }
}
catch (WebException)
{
    return false;
}

这篇关于检查是否网址可以访问 - 帮助优化类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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