How to check if TIdHTTP exception is raised - delphi

I'm not sure if this is the best way to check if a TIdHTTP exception is raised, here is what I did:
HTTP := TIDHttp.Create(nil);
try
try
HTTP.Head(URL);
SizeF := HTTP.Response.ContentLength;
code := HTTP.ResponseCode;
except
on E: Exception do
begin
code := HTTP.ResponseCode;
ShowMessage(IntToStr(code));
end;
end;
finally
HTTP.Free;
end;
if code = 200 then
// go download the file using multiple threads.
What I want to achieve is raising an axception is case there is one (I guess I already did) otherwise the program keeps running and download the file.
So is this the correct way to do it? Thanks for your replies.

As a common rule: Only handle exceptions when you can and want to handle them.
Otherwise let them just flow to stop the current execution of your code. Without any interception you will just receive a dialog with the exception message (this is handled by TApplication).
In this case you can change your code to
HTTP := TIDHttp.Create(nil);
try
HTTP.Head(URL);
// if an exception is raised then the rest of the code will not be executed
// yes, the code in finally part will execute
SizeF := HTTP.Response.ContentLength;
code := HTTP.ResponseCode;
finally
HTTP.Free;
end;
// check if all conditions are met
if code <> 200 then
// if not, raise a custom exception as you like
raise Exception.Create( 'Not what I expected here' );
// go download the file using multiple threads.

Related

Delphi - TIdHTTP freezes when POSTing data

I have an issue developing an HTTP client using Indy's TIdHTTP component. I'm using Indy 10.5.9.0.
When I call the TIdHTTP.DoRequest() method, the component freezes, and raises an exception:
Connection closed gracefully
I already tried using the TIdHTTP.Post() method, but the same problem happens.
The problem doesn't happen using the TIdHTTP.Get() method.
Here is my code:
try
jsonLatestFirmware := TJSONObject.ParseJSONValue(httpClient.Get(strGetLatest)) as TJSONObject;
try
srv := 'http://' + currentAlive.IP + ':9335/upgrade?command=start';
sList := TStringList.Create;
sList.Text := jsonLatestFirmware.get('data').JsonValue.ToString;
fileName := ExtractFilePath(Application.ExeName) + 'finals\' + currentMACQR + '-' + currentAlive.IP + '.json';
sList.SaveToFile(fileName);
JsonRetorno := TStringStream.Create('');
try
JsonToSend := TStringStream.Create;
WriteStringToStream(JsonToSend,TIdHTTPAccess(httpClient).SetRequestParams(sList, TEncoding.UTF8));
JsonToSend.Position := 0;
TIdHTTPAccess(httpClient).DoRequest('POST',srv,JsonToSend,jsonRetorno,[]); //component freezes here...
strRetorno := TStringList.Create();
strRetorno.LoadFromStream(JsonRetorno);
lblInformacao.Visible := True;
ShowMessage(strRetorno.Text);
except
on E: Exception do
ShowMessage('Error on request: '#13#10 + E.Message);
end;
finally
sList.Free;
JsonRetorno.Free;
JsonToSend.Free;
jsonResponse.Free;
end;
except
on E: Exception do
ShowMessage('There are no firmwares available for this product.');
end;
Anyone can help me to solve this problem?
I'm using Indy 10.5.9.0.
That is a very old and outdated version. At the time of this writing, the latest version is 10.6.2.5455. Please upgrade, as there have been a lot of changes and fixes to Indy since 10.5.9.
When I call the TIdHTTP.Post() method, the component freezes, and raises an exception:
First, you are not calling TIdHTTP.Post(), you are calling TIdHTTP.DoRequest(). You should be calling TIdHTTP.Post() instead, which calls TIdHTTP.DoRequest() internally for you:
httpClient.Post(srv, JsonToSend, jsonRetorno);
Second, Post() can't freeze and raise an exception at the same time. Which is actually happening? How large is the JSON you are posting? Does the server send any kind of response before the exception is raised?
Third, you absolutely should NOT be calling TIdHTTP.SetRequestParams() at all. That method is meant for internal use by the TStrings overloaded version of TIdHTTP.Post() when it submits an HTML webform in application/x-www-webform-urlencoded format. That is NOT what you need in this situation. You need to post your JSON as-is, not webform-encode it.
Indy uses blocking sockets. Most operations in Indy are synchronous. TIdHTTP.Post() is no different. It blocks the calling thread until the HTTP post is finished. If it freezes, that means the post is taking a long time to send, or the server is not sending a valid response back. If TIdHTTP.Post() raises a "Connection closed gracefully" exception, that means the server has closed the socket connection on its end while TIdHTTP was still sending/reading data over the socket. That could happen, for instance, if the server doesn't like your POST request, or if it determines the JSON is invalid while it is being received.
Also, your code can be simplified in general. You are misusing TStringList for handling JSON data.
Try something more like this instead:
var
sJSON, sRetorno: string;
jsonLatestFirmware: TJSONValue;
JsonToSend: TStringStream;
...
begin
...
try
sJSON := httpClient.Get(strGetLatest);
except
ShowMessage('There are no firmwares available for this product.');
Exit;
end;
try
jsonLatestFirmware := TJSONObject.ParseJSONValue(sJSON);
try
sJson := (jsonLatestFirmware as TJSONObject).get('data').JsonValue.ToString;
finally
jsonLatestFirmware.Free;
end;
JsonToSend := TStringStream.Create(sJson, TEncoding.UTF8);
try
JsonToSend.SaveToFile(ExtractFilePath(Application.ExeName) + 'finals\' + currentMACQR + '-' + currentAlive.IP + '.json');
httpClient.Request.ContentType := 'application/json';
sRetorno := httpClient.Post('http://' + currentAlive.IP + ':9335/upgrade?command=start', JsonToSend);
finally
JsonToSend.Free;
end;
except
on E: Exception do
ShowMessage('Error: '#13#10 + E.Message);
Exit;
end;
lblInformacao.Visible := True;
ShowMessage(sRetorno);
...
end;

Delphi and indy TIDHTTP : distinguish between "server not found" and "not found" response error

I am using indy TIDHTTP to code a way to know whether my server on the internet is down or the address of the page on the same server is not available.
I copied the suggestion given in another thread on stackoverflow:
try
IdHTTP1.Get(mypage_address);
except
on E: EIdHTTPProtocolException do begin
if e.errorcode=404 then
showmessage('404 File not found');
// use E.ErrorCode, E.Message, and E.ErrorMessage as needed...
end;
end;
but this way I am only able to detect a server response code and not whether the server did not respond at all. I guess it's trivial but I do not know what is the way to do that?
An EIdHTTPProtocolException exception is raised when TIdHTTP successfully sends a request to the server and it sends an HTTP error reply back to TIdHTTP. If the server cannot be reached at all, a different exception (typically EIdSocketError, EIdConnectException, or EIdConnectTimeout) will be raised instead.
try
IdHTTP1.Head(mypage_address);
except
on E: EIdHTTPProtocolException do begin
ShowMessage(Format('HTTP Error: %d %s', [E.ErrorCode, E.Message]));
end;
on E: EIdConnectTimeout do begin
ShowMessage('Timeout trying to connect');
end;
on E: EIdSocketError do begin
ShowMessage(Format('Socket Error: %d %s', [E.LastError, E.Message]));
end;
on E: Exception do begin
ShowMessage(Format('Error: [%s] %s', [E.ClassName, E.Message]));
end;
end;
I attempted doing the server/site checking scientifically. but eventually simply came down to this:
function TFrameSiteChecker.GetSiteHeader(const AUrl: string): Integer;
begin
try
idhttp1.Head(AUrl);
Result := idhttp1.ResponseCode;
except
on E: exception do
Result := 0;
end;
end;
Logic being, getting the head reduces traffic, log sizes etc.
There is one one right result from the function - the return of status code 200, anything else is a fail.
Also I failed to force windows / the system / indy to not buffer/cache content, so also eventually, just run the checker every 30 minutes on a schedule. Otherwise (unless something else clears the cache) after the first connect it always succeeds, even if you unplug the machine from the network!

Using any exceptions to trigger code in Delphi

Hi I am new to using Delphi and am trying to write an application that will check to see if a website is up or if there is any thing wrong with it. I am using Indy's IdHTT. The problem is that it will catch any protocol errors but not things like socket errors.
procedure TWebSiteStatus.Button1Click(Sender: TObject);
var
http : TIdHTTP;
url : string;
code : integer;
begin
url := 'http://www.'+Edit1.Text;
http := TIdHTTP.Create(nil);
try
try
http.Head(url);
code := http.ResponseCode;
except
on E: EIdHTTPProtocolException do
code := http.ResponseCode;
end;
ShowMessage(IntToStr(code));
if code <> 200 then
begin
Edit2.Text:='Something is wrong with the website';
down;
end;
finally
http.Free();
end;
end;
I am basically trying to catch any thing that is not that the website is ok so I can call another form that will setup an email to tell me that the site is down.
update: First you are right I did miss that 'then' sorry about that was removing other code and it got deleted by mistake. I did not know the specific to general when dealing with exceptions thank you. Finally I did find what i was looking for was this code here
on E: EIdSocketError do
using the uses IdStack
Change your code to either catch all exceptions, or add more specific ones as well:
url := 'http://www.'+Edit1.Text;
http := TIdHTTP.Create(nil);
try
try
http.Head(url);
code := http.ResponseCode;
except
on E: EIdHTTPProtocolException do
begin
code := http.ResponseCode;
ShowMessage(IntToStr(code));
if code <> 200
begin
Edit2.Text:='Something is wrong with the website';
down;
end;
end;
// Other specific Indy (EId*) exceptions if wanted
on E: Exception do
begin
ShowMessage(E.Message);
end;
end; // Added missing end here.
finally
http.Free();
end;
Note that if you're going to handle multiple exception types, it's important to go from most specific to least specific. In other words, if you put less specific (more general types) exceptions first, this is what happens:
try
DoSomethingThatCanRaiseAnException();
except
on E: Exception do
ShowMessage('This one fires always (covers all exceptions)');
on E: EConvertError do
ShowMessage('This one will never happen - never gets this far');
end;
This one will work properly, because it's more specific to less specific. Properly, it would be reversed:
try
DoSomethingThatCanRaiseAnException();
except
on E: EConvertError do
ShowMessage('This one gets all EConvertError exceptions');
on E: Exception do
ShowMessage('This one catches all types except EConvertError');
end;

(Indy) TIdHTTP EIdSocketError Socket Error # 0 exceptions when downloading files

I have this delphi code that basically download files from a secure server (using Indy build 10.5.8 r4743 if I'm not mistaken):
The problem is: I'm getting random "Socket Error # 0" exceptions that I couldn't get fix or even understand:
Project MyProject.exe raised exception class EIdSocketError with message 'Socket Error # 0
Stack is here, pascal code is:
IdHTTP := TIdHTTP.Create(nil);
TheSSL := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
TheCompressor := TIdCompressorZLib.Create(nil);
TheCookieManager := TIdCookieManager.Create(IdHTTP);
try SetupWebComponents(IdHTTP, TheSSL, TheCompressor, TheCookieManager); except end;
TheCookieManager.OnNewCookie := MainForm.SyncNewCookie;
// Go!
Stream := TMemoryStream.Create;
try
IsError := False;
try
with IdHTTP do
begin
OnWork := MainForm.IdHTTPWork_Download;
try
try
IsNewFile := (Not FileExists(LocalFile));
if IsNewFile then
TheFile := TFileStream.Create(LocalFile, fmCreate)
else
// File already exist, resume download
TheFile := TFileStream.Create(LocalFile, fmOpenReadWrite);
DoExit := False;
// Use [ Request.Referer ] it to pass VersionGUID to Work event (to record download progress in db)
Request.Referer := VersionGUID;
repeat
// Get resume byte
if IsNewFile then
begin
ResumeByte := 0;
IsNewFile := False;
end
else
ResumeByte := GetResumeByteFromDB();
if FileExists(LocalFile) then
begin
// File already exist, resume download
DoExit := (ResumeByte >= TheFileSize);
TheFile.Seek(ResumeByte, soFromBeginning);
end;
// Save ResumeByte, it will be used to record download progress in db & eventually use it to resume downloads)
IdHTTP.Tag := ResumeByte;
Request.Range := IntToStr(ResumeByte) + '-';
Get(TheURL, TheFile);
IsError := (Not (ResponseCode in [200, 206])) OR (Pos('login', URL.Document) <> 0);
// Break if there's any error (to allow retrying later in a clean fashion)
if IsError then
Break;
until DoExit;
Disconnect;
except
if (ResponseCode = 404) then
begin
// File doesn't exist, get out
Result := False;
Exit;
end;
end; // try/except
finally
FreeAndNil(TheFile);
end; // try/finally
end; // with
except
IsError := True;
end; // try/except
finally
Stream.Free;
end;
I posted a similar question some time ago, but that was regarding upload, not download. The code was fixed since then with the kind help of SO members, and the same code (used to deal with cookies, re-login, etc...) is being used now so I assume the problem is really in the download procedure shown above
Can someone please take a look at this tell me what I'm doing wrong?
Just like with your other question, you should upgrade to a more recent version, if possible, and verify the problem still occurs before then asking for help. The current version is 10.5.9 r4861.

TCPclient.connected problem in Delphi - Indy

I am having problem with IdTCPclient.connected function from Indy in Delphi. I am using Indy10 and Delphi2010 environment. My problem is every time i check the TCP connection with IdTCPclient.connected, it raises exception with these errors EidSocketError, EidReadTimeOut. Is there any way to disconnect and reconnect the connection? (like reset the connection).
Note: I set TCPClient.ReTimeout:= 30000;
The implemented coding for reset the connection is follow.
if IdTCPclient.connected then
begin
IdTCPclient.IOHandler.InputBuffer.Clear;
IdTCPclient.Disconnect;
end;
sleep(1000);
try
IdTCPclient.connect;
except
on E: Exception do
MessageDlg('Connecting Error: '+E.Message, mtError, [mbOk], 0);
end;
But some point, i get exception and it couldn't connect at all. I am not sure what i am doing wrong.
Should i do this?
Disconnect first
Clear input buffer
Destroy TCPclient
Re-create new TCPclient
And then connect it again
If it is the case, can someone provide me a way how to do it properly?
Also, there is another function to re-connecting the TCP in my coding. It also gives me exception as well. I check the connecting before i send a message to TCP. If there is no connection, i reconnect for five times.
result := IdTCPclient.connected
if not result then
begin
for k:=0 to 4 do
beign
sleep(1000);
try
TCPclient.connect;
except
on E: Exception do
MessageDlg('Connecting Error: '+E.Message, mtError, [mbOk], 0);
end
result := TCPclient.connected;
if result then break;
end;
With above two coding, program handles reconnecting and reset the connection pretty well. But some point the program cannot re-connect or reset the connection at all.
What should i do when i get exception? Should i reconnect from exception?
How do we build coding to check the connection regularly?
How do we build coding to to get back the connection when it lost?
Kind regards,
Connected() should not be raising any exceptions at all. If it is, then it is likely a bug. Please provide a stack trace showing that.
The best option is to simply avoid using Connected() whenever possible. When you need to perform an I/O operation, just do so, and let Indy raise an exception if a failure occurs. You can then handle it at that moment, eg:
try
IdTCPClient.DoSomething...
except
on E: EIdException do begin
Reconnect;
end;
end;
procedure Reconnect;
var
k: Integer;
begin
IdTCPClient.Disconnect;
if IdTCPClient.IOHandler <> nil then
IdTCPClient.IOHandler.InputBuffer.Clear;
for k := 0 to 4 do
begin
Sleep(1000);
try
IdTCPClient.Connect;
Exit;
except
on E: Exception do
begin
MessageDlg('Connecting Error: '+E.Message, mtError, [mbOk], 0);
if k = 4 then
raise;
end;
end;
end;
end;
before you connect make sure that the passive Boolean of idftp is false
when you need file transfert change it to true with binary file option

Resources