使用 Indy 时连接不会超时
我想从互联网下载一个文件,我想这应该是一个简单的任务。尝试了几种不同的方法,我发现每种方法都有其自身的缺点。 主要问题是:
- 应用程序在下载文件之前冻结
- 如果互联网连接丢失/服务器不响应,应用程序将永远冻结。
(详细信息:
如何通过 HTTP 从 Internet 检索文件?< /a>
从互联网下载文件时连接不会超时 )
所以,最后我根据几个人的建议使用了“专业”库,例如 Indy。然而,Indy 并不比我尝试过的代码片段好多少(但它要大得多并且难以维护)。使用 Indy 时,应用程序不会仅短暂冻结,因此它仍然(以某种方式)可用。但是,在下载完成之前无法关闭应用程序(如果互联网连接中断则不能关闭)。
其他人报告了同样的问题: http://borland .newsgroups.archived.at/public.delphi.internet.winsock/200609/0609079112.html
https://forums.embarcadero.com/thread.jspa?threadID= 25199&tstart=90
那么,我必须对 TIDAntiFreeze 进行一些黑客攻击才能使其正常工作?
此外,ConnectTimeout 属性也无法识别。
fIDHTTP := TIDHTTP.Create(NIL);
fIDHTTP.ConnectTimeout:=5000;
我是否应该放弃 Indy 并返回到在单独线程中下载文件的原始想法,并在线程没有响应时结束线程(至少这样我可以摆脱第 3 方库)?如果我这样做会产生不可预见的副作用吗?
使用:Delphi 7、Indy 10.1.5 10.5(可能)。
谢谢
I want to download a file from internet and I imagine this should be a simple task. Trying several different approaches I have found that each one has its own drawback.
The main issues are:
- Application freezes until it downloads the file
- Application freezes forever if the Internet connection is lost/server does not respond.
(details:
How to retrieve a file from Internet via HTTP?
The connection does not timeout while downloading file from internet )
So, finally I used the suggestions I got from several people to use "pro" libraries such as Indy. However, Indy is not much better than the pieces of code I have tried (but it is way much larger and difficult to maintain). While using Indy the application does not freezes only for short periods so it is still (somehow) usable. However, the application cannot be shut down until the download finishes (never if the Internet connections gets broken).
Other people reported the same problem: http://borland.newsgroups.archived.at/public.delphi.internet.winsock/200609/0609079112.html
https://forums.embarcadero.com/thread.jspa?threadID=25199&tstart=90
So, there is some hacking I had to do to TIDAntiFreeze in order to make it work?
Also, the ConnectTimeout property is not recognized.
fIDHTTP := TIDHTTP.Create(NIL);
fIDHTTP.ConnectTimeout:=5000;
Should I drop Indy and return to original idea of downloading the file in a separate thread and end the thread when it does not respond (at least this way I get rid of 3rd party libraries)? There will be unforeseen side effects if I do this?
Using: Delphi 7, Indy 10.1.5 10.5 (probably).
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能需要以 Indy 方式使用 Indy:使用线程。 Indy 专门设计为在阻塞模式下工作,因为这就是大多数互联网协议的工作方式(例如:使用 HTTP,在协议级别,您发送请求,然后读取响应。您不发送和 同时接收)。
TIdAntiFreeze
应该可以帮助您使用一些 Indy 功能,而无需处理线程;我从未使用过它,因为至少从概念上来说,这是一个丑陋的黑客。如果您不想处理线程,那么您应该看看 ICS - 它被设计为在异步模式下使用,无需线程。它不需要与
TIdAntiFreeze
等效的功能,因为它不是阻塞的。您开始下载并处理一些事件以获取进度和完成通知。 ICS 与 Indy 一样知名、专业且广泛使用。You probably need to use Indy the Indy way: using threads. Indy was specifically designed to work in blocking mode, because that's how most internet protocols work (example: with HTTP, at protocol level, you send a request, then you read the response. You don't send and receive at the same time).
TIdAntiFreeze
is supposed to help you use some Indy functionality without dealing with threads; I never used that because, at least conceptually, it's an ugly hack.If you don't want to deal with threads then you should take a look at ICS - it was designed to be used in async mode, without threading. It doesn't need the equivalent of
TIdAntiFreeze
because it's not blocking. You start a download and you handle some events to get progress and completion notifications. ICS is just as well-known, professional and wildly used as Indy.解决这类问题并不是太难。您要做的第一件事是确保您已正确处理错误处理。如果出现问题,请确保一切都正确清理。除此之外,请确保下载代码是单独线程的一部分。如果出现任何问题,您可以随时从主程序终止线程。这是对我来说工作正常的代码(仅用于下载,而不是线程)。
It's not too difficult to solve these sorts of problems. The first thing you have to do is make sure that you have properly handled error handling. If something fails then make sure everything cleans up properly. Beyond that make sure the downloading code is part of a separate thread. If there is any problem you can always terminate the thread from your main program. Here's the code (for downloading only, not the threading) which is working fine for me.