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;
    }
}
Marco Salerno
  • 5,131
  • 2
  • 12
  • 32
Kfir Irani
  • 47
  • 6
  • What you mean _"ugly"_? It seems ok to me. – SᴇM May 14 '18 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 – TheGeneral May 14 '18 at 11:01
  • `return Uri.TryCreate(Url);` – fubo May 14 '18 at 11:03
  • Well I don't think that the this duplicate mark is correct one, I think it's more like [this problem](https://stackoverflow.com/questions/153451/how-to-check-if-system-net-webclient-downloaddata-is-downloading-a-binary-file#156750). The OP said that he want to check if URL _"valid and downloadable"_, so I assume, he means that returning file content is binary. – SᴇM May 14 '18 at 11:03
  • If you look at the .NET code even [`OpenRead`](https://referencesource.microsoft.com/#System/net/System/Net/webclient.cs,ea41c9388029aa78,references) needed a `try-catch`, why you think you can do it without? – Tim Schmelter May 14 '18 at 11:03
  • Try-Catch is in most cases the best possible action for building a error tolerant softare, so no shame! But be carefull when using it around big compex algorithm, since it could make it slower. – Cataklysim May 14 '18 at 11:03
  • `I'm trying to check is a given URL is Valid and downloadable` there is no way to check apart from starting the download some way or another – TheGeneral May 14 '18 at 11:04
  • Possible duplicate of [How to check if System.Net.WebClient.DownloadData is downloading a binary file?](https://stackoverflow.com/questions/153451/how-to-check-if-system-net-webclient-downloaddata-is-downloading-a-binary-file) – SᴇM May 14 '18 at 11:06
  • Maybe try wtih `webClient.DownloadString(url)` – syp_dino May 14 '18 at 11:09
  • Don't think it's duplicated question because they were dealing with whether the URL is well formatted and i want to check if the URL is reachable and downloadable. – Kfir Irani May 14 '18 at 11:26
  • 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. – Evk May 14 '18 at 11:28
  • Generally i want to avoid using try-catch when not needed, i just looking for the "magic" method that can tell me if there is something behind the socket. – Kfir Irani May 14 '18 at 11:31
  • Then create a new Magic method handling try catch in it and the method itself returning true or false, and then use the same magic method everywhere. – Ipsit Gaur May 14 '18 at 11:42
  • 1
    There is no answer, the magic method that you talk about, does the same that you actually wrote. – Marco Salerno May 14 '18 at 11:46
  • I think that best solution is to to do HEAD request + exception handling – Kfir Irani May 14 '18 at 13:53

0 Answers0