TidHTTP Error Handling

I am writing a program to perform checks on websites to check their current status, for example. 200 OK. I am 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 CSiteURL set in the stream designer, and the status variable is the variable for the entire stream.

Try, besides the parts of this code, came from Remy Lebeau - TeamB here , which was the question I asked when I was initially tinkering with this idea. The problem I am facing is that this code only works if the site returns 200 OK. Everything else raises an exception, including if I turn off the Internet. It also throws exceptions on sites that redirect, for example. www.google.com throws an exception during debugging, I assume that it redirects to www.google.co.uk for me, however, if I continue the exception, then it returns 301 FOUND code without problems.

The end result that I am looking for is to give me income based on whether the site is online or has an error (e.g. HTML RETURN CODE), additionally giving feedback when the site does not exist (e.g. registered URL) or simply impossible to contact.

Other tips I'm looking for is the question of how to get additional information from the request. In particular, at the moment I also want to get the IP address of the site being checked.

It would be nice if it worked without the need to have http: // to any URL, so, for example, www.google.com, which at the moment does not work due to an unknown protocol, will work.

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

+4
source share
1 answer

Yes, of course, this raises an exception. How Indy works, and why Remy advised you to catch exceptions.

Now you are confused by the fact that even if you have code to throw exceptions, the Delphi debugger still intercepts them and pauses your program to notify you that an exception has occurred. I already wrote about this phenomenon.

Since you are apparently not interested in knowing about exceptions other than status 200 while you are debugging - because your program is designed to handle all these errors - your best option is probably to add an EIdHTTPProtocolException to the debugger list ignored exceptions. The easiest way to do this is to select โ€œIgnore this type of exceptionโ€ the next time the debugger notifies you.

+7
source

Source: https://habr.com/ru/post/1343509/


All Articles