To answer your main question, the only way you have to check if a particular URL is valid or not is to check against the web server and check what the server tells you.
With indy you can use the AIgnoreReplies
parameter of the Get and other methods to instruct the TIdHTTP instance not to raise an exception in case that status is returned by the web server, like this:
IdHTTP1.Get(Url, FS, [404]);
A exception will still be raised in case any status different than 200 and 400. There are other status codes that may don't raise an exception depending on various configurations of the component, for example status code 401 and authentication parameters, and others.
That said,
I find several problems in your code:
- The try/except block you have kills any exception, any exception regardless of what nature the exception may be. It treats the same a
EOutOfMemory
than a EIdSocketError
, EIdHTTPProtocolException
or even a EMayanWorldEnd
exception!
- You download the image twice... it happens you just ignore the first downloaded data and use it to try to determine if the resource exists or not. If you feel you must have to check if the resource exists or not, don't perform a
GET
command over it, perform a HEAD
one!
- Don't use Application.ProcessMessages, move your code to a Thread!
- Learn to handle in a proper way the different status codes you may get and other errors you may find. It is hard at the beginning, but is the way to go if you want to make it robust. Different errors may be:
- HTTP status codes, like:
- Request TimeOut (slow down and retry)
- HTTP Version Not Supported (well, try with another version)
- Etc.
- Network Failures
- Is the internet down
- Is the WebServer down
- Etc.
- As a general rule, let fly any other exception you don't know how to handle... or if you have no choice, eat them but log what's happening and read the logs, that way you will improve your knowledge and skills.