Jump to content
Ian Branch

EIdConnClosedGracefully error..

Recommended Posts

Hi Team,

First time I have seen this error.

It happened apparently at the line indicated below..

 

  if MainForm.DBWReg.ReadBool('ThoughtForTheDay', 'ShowAtStart', True) and not lThoughtForTheDay then
  begin
    //
    if CheckUrl('http://quotes4all.net') then
    begin
      HTTP := TIdHTTP.Create;
      try
        interim := HTTP.Get('http://quotes4all.net');       <<<<<<<<<<   Error here..
        //
        ChopStart := pos(StartQuote, interim);
        if ChopStart > 1 then
        begin
          ChopEnd := PosEx(EndQuote, interim, ChopStart + 1);
          if ChopEnd > ChopStart then
          begin
            interim := Copy(interim, ChopStart, ChopEnd - ChopStart);
            InCmnd := False;
            for Cntr := 1 to Length(interim) do
            begin
              if interim[Cntr] = '<' then
                InCmnd := True
              else if interim[Cntr] = '>' then
                InCmnd := False
              else if not InCmnd then
                Quote := Quote + interim[Cntr];
            end;
            TaskMessageDlg('Thought for the Day...', Quote, mtInformation, [mbOK], 0);
          end;
        end;
      finally
        FreeAndNil(HTTP); // .Free;
        lThoughtForTheDay := True;
      end;
      //
    end;

I have seen Remy suggest using "TIdHTTP.Disconnect(False) and TIdHTTP.IOHandler.InputBuffer.Clear() ".

How/where do I incorporate them into the above code please?

Should they be incorporated into the above code?

 

Regards & TIA,

Ian

Share this post


Link to post
On 9/30/2021 at 5:09 PM, Ian Branch said:

First time I have seen this error.

It happened apparently at the line indicated below..

The only way that can happen is if the HTTP server is closing its end of the TCP connection prematurely before the end of the response has been sent.  If the server wants to use a disconnect to signal end-of-response (which is a valid use-case), it has to indicate that up-front in the HTTP response headers, in which case TIdHTTP would simply handle the disconnect internally and your code would not see this error.  So, the only way this error can reach your code is if the disconnect is unexpected.

Quote

I have seen Remy suggest using "TIdHTTP.Disconnect(False) and TIdHTTP.IOHandler.InputBuffer.Clear() ".

How/where do I incorporate them into the above code please?

Should they be incorporated into the above code?

No, you don't need to do that in this case, since you are destroying the TIdHTTP object after sending a single HTTP request.  That earlier advice only applies if you were reusing the TIdHTTP object for sending multiple HTTP requests. And even then, under ideal conditions, you still should not need to manually disconnect and clear the buffer, TIdHTTP should be handling that internally for you.

 

Also, your CheckUrl() appears to be unnecessary.  Just request the URL unconditionally, and handle any errors it may raise.

Edited by Remy Lebeau
  • Like 1

Share this post


Link to post

Ahhh.  Thanks Remy.  It is clearer now.

RE the CheckURL, that is a really old habit I have..

 

Regards,

Ian

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×