Recent

Author Topic: [Solved] Check if URL is ok?  (Read 440 times)

Fred vS

  • Hero Member
  • *****
  • Posts: 3573
    • StrumPract is the musicians best friend
[Solved] Check if URL is ok?
« on: April 14, 2025, 04:11:18 pm »
Hello.

How to check if a url is available and ok?

For example, this site is not available at the moment:
http://server1.harmonyradio.be:8000/harmonyradio.mp3

Trying to access it via firefox, after a +- short time there is a message "This is site is not accessible".

But with the pre-check to only get the header, the application crash and I have to reboot the system.
This to get the header:
 
Code: Pascal  [Select][+][-]
  1. Http.RequestHeaders.Clear;
  2. Http.KeepConnection := False;
  3. Http.HTTPMethod('HEAD', URL, Nil, [200, 204, 301, 302, 303, 307, 308, 400]);
  4. SL.Assign(http.ResponseHeaders);

What can be used to do a pre-check that will not crash the system if the url is not available?

Thanks.

« Last Edit: April 14, 2025, 04:52:20 pm by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

Fred vS

  • Hero Member
  • *****
  • Posts: 3573
    • StrumPract is the musicians best friend
Re: Check if URL is ok?
« Reply #1 on: April 14, 2025, 04:51:46 pm »
Yep, thanks to my friend Gork, here the solution, create a independent http call and use it before to call the "heavy" http:

Code: Pascal  [Select][+][-]
  1. { Pre-check URL accessibility }
  2. function IsURLAccessible(const URL: string): Boolean;
  3. var
  4.   Http: TFPHTTPClient;
  5. begin
  6.   Result := False;
  7.   if (URL = '') or (Pos('http', LowerCase(URL)) <> 1) then
  8.     Exit;
  9.  
  10.   Http := TFPHTTPClient.Create(nil);
  11.   try
  12.     Http.AllowRedirect := True;
  13.     Http.IOTimeout := 5000; // 5-second timeout
  14.     Http.ConnectTimeout := 5000;
  15.     Http.RequestHeaders.Clear;
  16.     try
  17.       Http.HTTPMethod('HEAD', URL, nil, [200, 204, 301, 302, 303, 307, 308]);
  18.       Result := True; // URL is accessible
  19.     except
  20.       on E: Exception do
  21.       begin
  22.         // Optional: Log error for debugging
  23.         // Writeln('URL check failed: ', E.Message);
  24.       end;
  25.     end;
  26.   finally
  27.     Http.Free;
  28.   end;
  29. end;

Thanks Gork (and tell your boss to be cooler).
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1505
    • Lebeau Software
Re: [Solved] Check if URL is ok?
« Reply #2 on: April 14, 2025, 04:57:58 pm »
How to check if a url is available and ok?

The only way is to actually request the url and handle any failures that may occur.

But with the pre-check to only get the header, the application crash and I have to reboot the system.
...
What can be used to do a pre-check that will not crash the system if the url is not available?

An unavailable url should not crash the whole system. It should not even crash your app, unless you are not doing adequate error handling to begin with. Even if the url were available, you still have to be prepared to handle server errors, transmission errors, memory errors, file errors, etc. So things can still go wrong that you need to handle. So a precheck isn't really that useful anyway, especially when an actual download would at least report the same server errors that a precheck would report.
« Last Edit: April 14, 2025, 05:00:42 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Fred vS

  • Hero Member
  • *****
  • Posts: 3573
    • StrumPract is the musicians best friend
Re: [Solved] Check if URL is ok?
« Reply #3 on: April 14, 2025, 06:50:18 pm »
Hello Remy and thanks for the advice.

Indeed it would be more logic to have the check done in the audio-thread but, after long nights to try to do it with the correct http.head(), it crash the app and sometimes all the system.

And the solution to first check the url with a independent check, outside the TThreadHttpGetter = class(TThread) of audio web streaming and before to create it works like charm.
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

d2010

  • Full Member
  • ***
  • Posts: 160
Re: Check if URL is ok?
« Reply #4 on: April 28, 2025, 02:23:01 pm »
Yep, thanks to my friend Gork, here the solution, create a independent http call and use it before to call the "heavy" http:
If you add your-routine inside cozipinc.dll, then you can avoid Exception
message, because , using value elseDoExcep<-100.
If you are inside mainprogram.exe with Debugger, then you cannot display errorException-> in this case you return wrong-error-number.
Perhaps, You scan too much the memory-of-EXE, with Pos('http:'),
i copied only first 15characters inside small-memory.(avoid memory-leaks)
Code: Pascal  [Select][+][-]
  1. Function cozipinc_urlacces(const curl:string;elseDoExcep:integer):integer;exports;
  2. var Http: TFPHTTPClient;
  3.     Buf3:array[0..15] of char;
  4. begin
  5.   Result := RTCAN;
  6.   fillchar(buf3,sizeof(Buf3),0);
  7.   if (length(curl)>=high(Buf3)) then copymemory(@Buf3[0],@curl[1],15)  else
  8.      if (length(curl)>5) then  copymemory(@Buf3[0],@curl[01],length(Curl));
  9.   Result:=RTINPUTTRUNCATED;
  10.   if (length(curl)>4094) then exit;  //url is to large
  11.   if (Buf3[5]=#00) then exit;
  12.   Result := RTFAIL;
  13.   if (Pos('://',Buf3) < 5) then Exit;
  14.     Http := TFPHTTPClient.Create(nil);
  15.     try
  16.       Http.AllowRedirect := True;
  17.       Http.IOTimeout := 5000; // 5-second timeout
  18.       Http.ConnectTimeout := 5000;
  19.       Http.RequestHeaders.Clear;
  20.       try
  21.         Http.HTTPMethod('HEAD',curl, nil, [200, 204, 301, 302, 303, 307, 308]);
  22.         Result := RTNORM; // URL is accessible
  23.       except
  24.         ON E: Exception do
  25.           if (-100>elseDoExcep) then result:=elseDoExcep else
  26.              if (elseDoExcep>00)and(elseDoExcep<256) then
  27.                Writeln('URL check failed: ', E.Message);
  28.       end;
  29.     finally
  30.       Http.Free;
  31.     end;
  32. end;
  33.  
« Last Edit: April 28, 2025, 02:29:32 pm by d2010 »

 

TinyPortal © 2005-2018