0

I have an array of images URLs. I want to know which of these URLs are correct and which not, without using try-catch, and I want to do that as fast as possible.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vahan
  • 3,016
  • 3
  • 27
  • 43
  • 1
    Why would you want to specifically avoid try/catch? – Rag Aug 04 '11 at 17:05
  • i take imgs urls from img tags from html page, if url startswith http i take it, if it startswith / i add this url to the route, if startwith // i add it afetr '?' or '&', but sometimes i get wrong urls, i want to know is yrl which i got correct or not – Vahan Aug 04 '11 at 17:06
  • How do you define "correct"? Returns http code 200? – CodesInChaos Aug 04 '11 at 17:07
  • @Brian Gordon because i work wits asp.net mvc, and i could have 200 and more imgs, and in that case my application works very slow, because it prints all exceptins which it cathces – Vahan Aug 04 '11 at 17:08
  • @CodeInChaos i meant that if i open this url in browser it brings me a picture not the error or smth like that – Vahan Aug 04 '11 at 17:10
  • 1
    @Vahan, to determine that you must open each URL in turn, which is certainly not going to be mistaken as *fast*. So I think the try/catch is the least of your performance issues. – Kirk Woll Aug 04 '11 at 17:16
  • 1
    By "image urls correct" do you mean image files exist? if so you can check for File.Exists(imgUrl) foreach element in the array. – zero7 Aug 04 '11 at 17:22
  • @Kirk Woll I tried with try/catch and it takes very slowly, i need faster way – Vahan Aug 05 '11 at 06:54
  • @zero7 I triid File.Exist and it returns false even on correct urls – Vahan Aug 05 '11 at 06:57
  • @Vahan, this is from msdn: true if the caller has the required permissions and path contains the name of an existing file; otherwise, false. This method also returns false if path is null, an invalid path, or a zero-length string. If the caller does not have sufficient permissions to read the specified file, no exception is thrown and the method returns false regardless of the existence of path. – zero7 Aug 05 '11 at 08:54
  • @zero7 it returns true when file exits on your computer ?this image urls isn't from my computer it is from some web page – Vahan Aug 05 '11 at 09:52

1 Answers1

1

I would think the only way for you to know which urls are correct is to just make an HTTP request to the URL. If you have a lot of pictures, this will always take time. You can minimize that time by just making a HEAD HTTP request (as opposed to a GET and downloading the whole response), and checking the status code of the response. If the status code is 200, you might assume that you get the picture you're looking for, if it is 404, you know the url is incorrect.

Code might be something like:

HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("http://example.com/");
req.Method = "HEAD";
HttpWebResponse resp = (HttpWebResponse)(req.GetResponse());
HttpStatusCode statuscode = resp.StatusCode;

A note on getting a 200 response: If you get a 200 repsonse, you cannot be sure that you are actually getting image you want. You might be getting something else, e.g. a redirect from the image url.

Maple
  • 350
  • 1
  • 8
  • when the url is incorrect, i can't get response from request because it gives exception, and i had to use try/catch which i don't want – Vahan Aug 05 '11 at 07:01
  • Ah ok so you have a malformed url then? I really don't think your biggest problem is a try/catch. I suppose you could check the url against a regular expression to see if it is valid. See for example this thread on this topic: http://stackoverflow.com/questions/161738/what-is-the-best-regular-expression-to-check-if-a-string-is-a-valid-url – Maple Aug 05 '11 at 09:52
  • I am working with asp.net mvc, and when it prints all catched exceptions in console, my application stoped. The url format is valid, but it no matter can be a not existing url – Vahan Aug 05 '11 at 09:57
  • Then you would get a different HTTP code, e.g. 502. Also, if you're printing everything out, that might be your problem. Instead of printing out on every catch, you could collect the exceptions in an array, and print them out when you're done testing the urls. – Maple Aug 05 '11 at 10:05
  • I didn't print anything, my catch block is empty, it automatically prints – Vahan Aug 05 '11 at 10:08