0

I'm trying to check is a given URL is Valid and downloadable. I wrote this code and it does work but i'm wondering if it can be achieved without the ugly try-catch

public static bool IsUrlValid(string url)
{
    try
    {
        WebClient webClient = new WebClient();
        var stream = webClient.OpenRead(url);
        return true;
    }
    catch (Exception ex)
    {
        return false;
    }
}
15
  • What you mean "ugly"? It seems ok to me. Commented May 14, 2018 at 11:00
  • There is really no way i can think of apart from suck-and-see... The problem is web servers dont give a list of valid routes and or working apis Commented May 14, 2018 at 11:01
  • return Uri.TryCreate(Url); Commented May 14, 2018 at 11:03
  • 4
    And how do you define downloadable? You can for example issue a HEAD request and ensure that server responds with success status code. Though exceptions are still possible, since for example url can point to unresolvable domain. Commented May 14, 2018 at 11:28
  • 1
    There is no answer, the magic method that you talk about, does the same that you actually wrote. Commented May 14, 2018 at 11:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.