[Solved] URLtoStr when web site unavailable lazarus / 21-General ---------------------------------------------------------------------- bobonwhidbey · 2022-07-04 16:47 I really like this code (below) that I picked up a few years ago. It works very well. When the web site is unavailable  (perhaps the host of the web site is down for maintenance) this routine tries for several minutes before returning a blank string. My question is - what determines the period of time before the routine gives up and assumes the web site is unavailable? Is this under control of the user's computer? Is there a way for my app to limit this time? p.s. the user with the long wait was Linux. I'm not sure if this is a Win problem. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function URLtoStr(URL: string): string;var  NetHandle: HINTERNET;  UrlHandle: HINTERNET;  Buffer: array[0..1023] of byte;  BytesRead: dWord;  StrBuffer: utf8string;begin  strbuffer := URL;  Result := '';  NetHandle := InternetOpen('Mozilla/5.0(compatible; WinInet)',    INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);   // NetHandle valid?  if Assigned(NetHandle) then    try      UrlHandle := InternetOpenUrl(NetHandle, PChar(Url), nil, 0,        INTERNET_FLAG_RELOAD, 0);       // UrlHandle valid?      if Assigned(UrlHandle) then        try          repeat            BytesRead := 0;            InternetReadFile(UrlHandle, @Buffer, SizeOf(Buffer), BytesRead);            SetString(StrBuffer, pansichar(@Buffer[0]), BytesRead);            Result := Result + StrBuffer;          until BytesRead = 0;        finally          InternetCloseHandle(UrlHandle);        end      // o/w UrlHandle invalid      else        ShowMessage('Cannot open URL: ' + Url);    finally      InternetCloseHandle(NetHandle);    end  // NetHandle invalid  else    raise Exception.Create('Unable to initialize WinInet');end; ---------------------------------------------------------------------- dsiders · 2022-07-04 17:18 Quote from: bobonwhidbey on July 04, 2022, 06:47:01 pmI really like this code (below) that I picked up a few years ago. It works very well. When the web site is unavailable  (perhaps the host of the web site is down for maintenance) this routine tries for several minutes before returning a blank string. My question is - what determines the period of time before the routine gives up and assumes the web site is unavailable? Is this under control of the user's computer? Is there a way for my app to limit this time? p.s. the user with the long wait was Linux. I'm not sure if this is a Win problem. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function URLtoStr(URL: string): string;var  NetHandle: HINTERNET;  UrlHandle: HINTERNET;  Buffer: array[0..1023] of byte;  BytesRead: dWord;  StrBuffer: utf8string;begin  strbuffer := URL;  Result := '';  NetHandle := InternetOpen('Mozilla/5.0(compatible; WinInet)',    INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);   // NetHandle valid?  if Assigned(NetHandle) then    try      UrlHandle := InternetOpenUrl(NetHandle, PChar(Url), nil, 0,        INTERNET_FLAG_RELOAD, 0);       // UrlHandle valid?      if Assigned(UrlHandle) then        try          repeat            BytesRead := 0;            InternetReadFile(UrlHandle, @Buffer, SizeOf(Buffer), BytesRead);            SetString(StrBuffer, pansichar(@Buffer[0]), BytesRead);            Result := Result + StrBuffer;          until BytesRead = 0;        finally          InternetCloseHandle(UrlHandle);        end      // o/w UrlHandle invalid      else        ShowMessage('Cannot open URL: ' + Url);    finally      InternetCloseHandle(NetHandle);    end  // NetHandle invalid  else    raise Exception.Create('Unable to initialize WinInet');end;   Connection timeout is controlled by InternetSetOption: https://stackoverflow.com/questions/28774137/decrease-internetopenurl- timeout These routines are imported from wininet.dll and depend on Windows. So they are not cross-platform. ---------------------------------------------------------------------- bobonwhidbey · 2022-07-04 17:51 i get "illegal qualifier" compilation error for the IntPtr.zero argument.     InternetSetOption(IntPtr.zero, INTERNET_OPTION_CONNECT_TIMEOUT,                       @dwTimeOut, SizeOf(dwTimeOut)); That routine is being found in WinINet.pp ---------------------------------------------------------------------- bobonwhidbey · 2022-07-04 17:59 Once I used NetHandle all seems to be working perfectly. Thank you dsiders.   NetHandle := InternetOpen('Mozilla/5.0(compatible; WinInet)',     INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);   dwTimeOut := 5000; // Timeout in milliseconds, so 5 sec. //  InternetSetOption(IntPtr.zero, INTERNET_OPTION_CONNECT_TIMEOUT,   InternetSetOption(NetHandle, INTERNET_OPTION_CONNECT_TIMEOUT,                     @dwTimeOut, SizeOf(dwTimeOut));