4

I'm writing a program to perform checks on websites to test they're current status, e.g. 200 OK. I'm using Indy HTTP shipped with Delphi 2010 for this task and have the current code.

procedure TCheckSiteIndividual.Execute;
var
http : TIdhttp;

begin
  try
    try
      http := Tidhttp.create(nil);  //Create indy
      http.Get(CSiteURL,nil);  //Send Request To Check Site
    except
      on E: EIdHTTPProtocolException do
       status := http.ResponseText; // or: code := E.ErrorCode;
    end;

    status := http.ResponseText;   //Return Status of Site
    synchronize(updateform);   //Update data to form
  finally
   http.Free;
  end;
end;

The CSiteURL variable is set in the Thread Constructor, and the status variable is a variable to the entire thread.

The try, except parts of this code came from Remy Lebeau - TeamB here which was a question I asked when initially fiddling around with this idea. The problem I'm having is that this code only works if the site returns a 200 OK code. Anything else causes an exception, including if I disable the internet. This also causes exceptions on sites that redirect e.g. www.google.com causes an exception during debugging I presume causes it's redirecting to www.google.co.uk for me, however if I continue past the exception then it returns a 301 FOUND code with no problems.

The final result I'm looking for it for it to give me a return based on whether the site is online or having error (e.g. HTML RETURN CODE) additionally giving feedback when the site doesn't exist (e.g. a non-registered URL) or just can't be contacted.

The other peices of advice I'm looking for are into how to get some more information back from the request. Specifically at the moment I'm looking to also get the IP of the site that was checked.

It would also be nice to have it work without needing to have http:// before any URL, so for example www.google.com will work which at the moment it doesn't because of unknown protocol.

Basically it would be great if someone had a link to some nice documentation for IndyHTTP because it seems to be sparse,

Community
  • 1
  • 1
Flatlyn
  • 2,040
  • 6
  • 40
  • 69
  • It looks like your status assignment after the exception handler is misplaced... also you're ignoring @Remy advise to use the head method instead of an entire get just to know the status returned by the server. Also, you're using the ResponseText to check the *status* of the site, be aware that INDY parse this for you and gives you the nice ResponseCode integer property to check for the response. Finally, you say a Exception is raised, but you do not tell us which exception, so it is harder to figure out what's going on here just by reading the code. – jachguate Mar 14 '11 at 17:02
  • @jachguate I have now updated the code to reflect the .head method. I had changed it as was toying with downloading the site's page, however have shelfed that just now. In what way is the exception handler misplaced? Also if I change to using the ResponseCode method what happens in the case of Socket Errors, e.g. site can't be reached. The problem with the exceptions beign raised as they vary dramatically, some being Socket Errors, some raising exceptions on the return e.g. an Exception of HTTP/1.1 302 Found – Flatlyn Mar 17 '11 at 02:08
  • the exception handler is not misplaced, the status assignment you're doing after the exception handler is what not makes sense to me. About the variety of exceptions raised here... it is not too huge... you can gain a better knowledge of the variety of failing reasons by inspecting that, and you fine tune your software to manage and report this accordingly as you gain that knowledge. – jachguate Mar 18 '11 at 17:15

1 Answers1

7

Yes, of course it causes an exception. That's how Indy works, and that's why Remy advised you to catch exceptions.

What you're confused by now is that even though you have code to catch exceptions, the Delphi debugger still intercepts them and pauses execution of your program to notify you that an exception occurred. I've written about that phenomenon before.

Since you're apparently not interested in knowing about non-status-200 exceptions while you're debugging — because your program is designed to handle all those errors itself — your best option is probably to add EIdHTTPProtocolException to the debugger's list of ignored exceptions. The easiest way to do that is to choose "Ignore this exception type" the next time the debugger notifies you.

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467