Forum > Networking and Web Programming
[SOLVED] synapse: how to get a timeout for http-downloads to work?
(1/1)
Hartmut:
My program shall download a website with synapse. Sometimes this website is not available. Then my code runs into a timeout of 130 seconds, which is too much. I want a timeout of max. 4 seconds.
Here is a demo of my code from a bigger program:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$mode objfpc}{$H+} uses sysutils, classes, httpsend; const URL = 'http://xxx.yyy.com'; function myHttpGetText(url: string; HTTP: THTTPSend; var SL: TStringList): boolean; {loads URL 'url' from a Webserver into a StringList} var ok: boolean; begin ok:=false; SL:=TStringList.Create; HTTP.Clear; {Reset headers, document and Mimetype} if HTTP.HTTPMethod('GET',url) then begin SL.LoadFromStream(HTTP.Document); ok:=true; end; exit(ok); end; function download_Website(url: string; var SL: TStringList): boolean; var HTTP: THTTPSend; ok: boolean; begin HTTP:=THTTPSend.Create; HTTP.Timeout:=4000; HTTP.Sock.SetTimeout(4000); ok:=myHttpGetText(url, HTTP, SL); HTTP.Free; exit(ok); end; var SL: TStringList; t: TDateTime; sec: double; ok: boolean; begint:=now;ok:=download_Website(URL, SL);sec:=(now-t)*86400.0; {time in seconds}writeln('Time: ', sec:0:2, ' seconds'); if not ok then writeln('ERROR') else writeln(SL.Text);SL.Free;end.
I use FPC 3.2.0 on Linux Ubuntu 18.04 with synapse stable version 40 and trunk r266 (both have the same result).
What I'm doing wrong? Thanks in advance.
GetMem:
@Hartmut
On windows immediately returns with the message "Error". I don't have to wait even 4 seconds.
Hartmut:
Thank you GetMem for your reply. Yes, if the URL does not exist at all, then immediately "ERROR" is returned. But with my real URL - if it is currently unavailable - then I have a timeout of 130 seconds, which is too long for me.
rvk:
You probably want to set HTTP.Sock.ConnectionTimeout to whatever you want.
See my post here:
--- Quote from: rvk on February 21, 2018, 09:48:45 am ---Sorry, I spoke too soon. What is said above still holds but the connect timeout was discussed on the Synapse list in 2012 and the non-blockingmode (which is mentioned above) was implemented in revision #160.
You also need to set the HTTPSender.Sock.ConnectionTimeout which handles the Connect timeout in non-blockingmode after which the socket is switched back to blocking mode.
Example:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---const URL = '10.255.255.1'; // <-- this is always non responsevar HTTPSender: THTTPSend; HTTPGetResult: boolean; Tm: TDateTime;begin HTTPSender := THTTPSend.Create; try HTTPSender.Timeout := 1 * 1000; HTTPSender.Sock.ConnectionTimeout := 1 * 1000; Tm := Now; HTTPGetResult := HTTPSender.HTTPMethod('GET', URL); Tm := Now - Tm; ShowMessage(Format('%d: done in %.3f seconds', [HTTPSender.ResultCode, Tm * 24 * 60 * 60])); finally HTTPSender.Free; end;end;
--- End quote ---
PS. When ConnectionTimeOut > 0 then Synapse will connect in non-blocking mode.
In blocking mode (the default) there is no way to set the timeout.
See http://www.ararat.cz/synapse/doku.php/public:howto:connecttimeout
Hartmut:
Great! 'HTTPSender.Sock.ConnectionTimeout' works perfectly. Thanks a lot rvk.
Again I like synapse and it's capabilities.
Navigation
[0] Message Index