Connection does not disconnect when using Indy

I want to download a file from the Internet, and I think it should be a simple task. Having tried several different approaches, I found that each has its own drawback. Main problems:

  • Application freezes before file upload
  • The application freezes forever if the Internet connection is lost / the server is not responding.

(details:

How to get a file from the Internet via HTTP?
The connection does not disconnect when downloading a file from the Internet )

So finally, I used the suggestions I received from several people to use pro libraries such as Indy. However, Indy is not much better than the code snippets I tried (but it is much larger and difficult to maintain). When using Indy, the application does not freeze only for short periods of time, so it is still (somehow) applicable. However, the application cannot be disconnected until the download is completed (never if the Internet connections are damaged).

Other people reported the same issue: http://borland.newsgroups.archived.at/public.delphi.internet.winsock/200609/0609079112.html
https://forums.embarcadero.com/thread.jspa?threadID=25199&tstart=90

So, are there some hacks I had to do for TIDAntiFreeze to get it working?

In addition, the ConnectTimeout property is not recognized.

fIDHTTP := TIDHTTP.Create(NIL); fIDHTTP.ConnectTimeout:=5000; 

Should I throw Indy and return to the original idea of ​​loading a file into a separate stream and end the stream when it does not respond (at least, this is how I get rid of third-party libraries)? Will there be unforeseen side effects if I do this?

Usage: Delphi 7, Indy 10.1.5 10.5 (possible).

thanks

+5
delphi indy10
source share
2 answers

It is not so difficult to solve these problems. The first thing you need to do is make sure that you handle the error handling correctly. If something fails, make sure everything is cleaned correctly. In addition, make sure that the download code is part of a separate thread. If there is any problem, you can always stop the thread from your main program. Here's a code (only for downloading, not streaming) that works fine for me.

 with TDownloadURL.Create(nil) do try URL := 'myurltodownload.com'; filename := 'locationtosaveto'; try ExecuteTarget(nil); except result := false; end; if not FileExists(filename) then result := false; finally clear; free; end; 
0
source share

You probably need to use Indy the Indy way: using streams. Indy was specifically designed to work in blocking mode, because most Internet protocols work (for example: with HTTP, at the protocol level you send a request, then you read the answer. You do not send and receive at the same time). TIdAntiFreeze is supposed to help you use some Indy features without using threads; I never used this because, at least conceptually, this is an ugly hack.

If you do not want to deal with Threads, you should take a look at the ICS link ( the ICS homepage ). ICS was designed for use in Async mode without streaming. It does not need the equivalent of TIdAntiFreeze, because it does not block. You start the download and process some events to receive notifications of progress and completion. ICS is also well known, professional, and wildly used as Indy.

+4
source share

All Articles