22

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.

  • Do you know a better approach?

Thanks.

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;
        }
GibboK
  • 71,848
  • 143
  • 435
  • 658
  • 6
    At the very least I would use the HEAD method instead of a GET, that way you're not downloading the actual content. – Lasse V. Karlsen Mar 21 '11 at 13:47
  • Also you should set `request.UseDefaultCredentials = true;` to ensure that a request to sites using stored credentials will work and don't throw a WebException just stating authorization failed. – Oliver Sep 25 '14 at 09:39

1 Answers1

47

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;
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 4
    What if you testing webservice URL and target web service is configured to not allow HEAD method? You will get the response: 405 Method Not Allowed so your method will return false, does not mean URL is not reachable. The approach from question is correct. – Pawel Cioch Oct 09 '13 at 19:52
  • 1
    Could you explain why a `using` statement is the way to go? Do you mean in this situation or just a general good practice to follow? – ethane Jul 28 '17 at 06:40
  • 1
    @Ethan2Pants: it means the response is disposed even if an exception is thrown elsewhere. It's just a better habit to get into. – Jon Skeet Jul 28 '17 at 06:48
  • @JonSkeet I am trying to use this approach to check one of the hosted WCF service `https://domain/abc.svc`. I get error- 403. I has to set- `request.UseDefaultCredentials = true` as I was getting error- 401 wihout it. Do you what I am doing wrong? – Souvik Ghosh Aug 23 '17 at 09:10
  • @Souvik: That sounds more like "how do I authenticate with a WCF service" than "how do I test a URL.' – Jon Skeet Aug 23 '17 at 09:37
  • @JonSkeet I think I am authenticated already. I am just getting error- 403 (forbidden). I can access other endpoints within the same application though with same approach – Souvik Ghosh Aug 23 '17 at 09:44
  • 1
    @Souvik: No, because that's is a WCF question and I haven't done any WCF for ages. You should look for questions about WCF. – Jon Skeet Aug 23 '17 at 09:45