FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Question about wfReadUrl() - SOLVED
Posts: 1467
Joined: Mon Oct 10, 2005 11:26 AM
Question about wfReadUrl() - SOLVED
Posted: Fri Apr 05, 2024 07:55 AM
Hello,

I use this code to check if a website exists or not.
Code (fw): Select all Collapse
FUNCTION wfReadURL(cUrl)

   LOCAL cFile  := SUBSTR(cUrl,RAT("/",cUrl)+1)
   LOCAL cExist := .F.

   LOCAL oUrl,oCli

   cUrl := LOWER(cUrl)

   BEGIN SEQUENCE

         oUrl := TUrl():New(cUrl)
         IF EMPTY(oUrl) ; BREAK ; ENDIF
         oCli := TIPClientHttp():New(oUrl)       ***
         IF EMPTY(oCli) ; BREAK ; ENDIF
         IF !oCli:Open() ; BREAK ; ENDIF
         IF !oCli:ReadToFile(cFile) ; BREAK ; ENDIF
         IF PAR1->TESTNOTRY
            cExist := "OK" $ UPPER(oCli:cReply)
         ELSE
            TRY
               cExist := "OK" $ UPPER(oCli:cReply)
            CATCH
               cExist := .T.
            END
         ENDIF
         oCli:Close()
   END SEQUENCE

RETURN(cExist)
It works fine for "http://" urls, but if I use to check it for a "https://", I got this error:
Code (fw): Select all Collapse
Error BASE/1081  Operation not supported: TIPCLIENTHTTP:new()
It happens on the line where I put ***.

I need this function to work for https. Who can I do this?

Thanks.

Regards,

Michel D.
Genk (Belgium)


_____________________________________________________________________________________________

I use : FiveWin for (x)Harbour v. 25.12 - Harbour 3.2.0 (May 2025) - xHarbour Builder (January 2020) - Bcc773

Posts: 1344
Joined: Wed Nov 16, 2005 09:14 PM
Re: Question about wfReadUrl()
Posted: Fri Apr 05, 2024 03:25 PM
Puedes usar la funcion WebPageContents(cUrl)
Code (fw): Select all Collapse
if !Empty(WebPageContents(cUrl))
   //... tu codigo
else
   MsgStop('Url not found')
endif
Posts: 1467
Joined: Mon Oct 10, 2005 11:26 AM
Re: Question about wfReadUrl()
Posted: Fri Apr 05, 2024 03:30 PM

Thanks a lot for trying to help me.

I tested you suggestion.

Unfortunately, the function you suggested is only working for http, not for https.

Regards,

Michel D.
Genk (Belgium)


_____________________________________________________________________________________________

I use : FiveWin for (x)Harbour v. 25.12 - Harbour 3.2.0 (May 2025) - xHarbour Builder (January 2020) - Bcc773

Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: Question about wfReadUrl()
Posted: Fri Apr 05, 2024 05:28 PM

Michel, please show a little and complete PRG example of what you are trying to do and I will fix it.

Posts: 1467
Joined: Mon Oct 10, 2005 11:26 AM
Re: Question about wfReadUrl()
Posted: Fri Apr 05, 2024 08:30 PM
Enrico,

Thank you so much for trying to help me.

My website has 3 different url's, all showing the same website.

Because some antivirus software is blocking some of my url's, I want to check which url is working fine.
Usuaslly my url's are working ok with the prefix "https".
THe function wfReadUrl() which I use to check if the url is working, is working fine with the prefix "http", but results into an error "no exported method" with the prefix "https".

This is the code how I want to check my url's:
Code (fw): Select all Collapse
   DO CASE
      CASE !wfReadURL("http://www.ma-consult.be")  ; xDownWeb := "http://www.ma-consult.be"
      CASE !wfReadURL("https://www.ma-consult.be") ; xDownWeb := "https://www.ma-consult.be"
      CASE !wfReadURL("http://ma-consult.be")      ; xDownWeb := "http://ma-consult.be"
      CASE !wfReadURL("https://ma-consult.be")     ; xDownWeb := "https://ma-consult.be"
      CASE !wfReadURL("http://www.juda.be")        ; xDownWeb := "http://www.juda.be"
      CASE !wfReadURL("https://www.juda.be")       ; xDownWeb := "https://www.juda.be"
      CASE !wfReadURL("http://juda.be")            ; xDownWeb := "http://juda.be"
      CASE !wfReadURL("https://juda.be")           ; xDownWeb := "https://juda.be"
      CASE !wfReadURL("http://www.curato.be")      ; xDownWeb := "http://www.curato.be"
      CASE !wfReadURL("https://www.curato.be")     ; xDownWeb := "https://www.curato.be"
      CASE !wfReadURL("http://curato.be")          ; xDownWeb := "http://curato.be"
      CASE !wfReadURL("https://curato.be")         ; xDownWeb := "https://curato.be"
   ENDCASE
The result is put into the variable xDownWeb which I use later to perform a download.

The code of the wfReadUrl() can be found in this topic.

Is that enough information?

Thank you.

Regards,

Michel D.
Genk (Belgium)


_____________________________________________________________________________________________

I use : FiveWin for (x)Harbour v. 25.12 - Harbour 3.2.0 (May 2025) - xHarbour Builder (January 2020) - Bcc773

Posts: 1344
Joined: Wed Nov 16, 2005 09:14 PM
Re: Question about wfReadUrl()
Posted: Fri Apr 05, 2024 08:46 PM
Prueba este ejemplo, aqui funciona bien con https
Code (fw): Select all Collapse
#include "FiveWin.ch"
FUNCTION Main()
local aUrl := {'https://forums.fivetechsupport.com/download/file.php?avatar=86_1402579120.jpg',;
               'https://forums.fivetechsupport.com/download/file.php?avatar=294_1327638239.jpg',;
               'https://forums.fivetechsupport.com/download/file.php?avatar=xxxxxxxxxxxxxx.jpg',; //Not found
               'http://sistemas.mercedes.gob.ar/medidores/img/logo.png'}, i,  oDlg1, oBrw

FOR i := 1 to 4
    if Empty(WebPageContents(aUrl[i]))      
      aUrl[i] := '..\bitmaps\16exit.bmp'
    endif      
next i 
DEFINE DIALOG oDlg1 TITLE "Imagenes Web" SIZE 300,300 PIXEL TRUEPIXEL RESIZABLE
   @ 60, 20 XBROWSE oBrw SIZE 200,250 pixel OF oDlg1 ARRAY aUrl  ;
      HEADERS "Imagen";
      COLUMNS 1;
      SIZES 70;
      CELL LINES NOBORDER
   WITH OBJECT oBrw
      :nRowHeight    := 70
      :aCols[1]:cDataType := "P"
      :CreateFromCode()
   END
ACTIVATE DIALOG oDlg1 CENTERED             
RETURN nil
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: Question about wfReadUrl()
Posted: Fri Apr 05, 2024 09:13 PM
Try something like this:
Code (fw): Select all Collapse
#include "Fivewin.ch"


FUNCTION MAIN()

    ? URLEXIST( "https://www.emagsoftware.it" )
    ? URLEXIST( "https://www.notexistent.it" )

    RETURN NIL


#command IF <condition> THEN <*statements*> => if <condition> ; <statements> ; end


STATIC FUNCTION URLEXIST( cUrl, oHtp )

    LOCAL lOk := .F.

    TRY
        DEFAULT oHtp := CREATEOBJECT( "MSXML2.SERVERXMLHTTP" )

        oHtp:Open( "GET", cUrl, .F. )

        oHtp:Send()

        IF oHtp:Status != 200 THEN BREAK

        lOk = .T.
    CATCH
    END

    RETURN lOk
Posts: 1467
Joined: Mon Oct 10, 2005 11:26 AM
Re: Question about wfReadUrl()
Posted: Fri Apr 05, 2024 10:16 PM

Hello Ceasar and Enrico,

I tried both suggestions.

They both work, but both suggestions do have a problem in case the url doesn't exist or doesn't react.

It lasts at least 1 to 2 minutes before the test is passed.

Can we do something about it?

But thank you very much for your suggestions.

Regards,

Michel D.
Genk (Belgium)


_____________________________________________________________________________________________

I use : FiveWin for (x)Harbour v. 25.12 - Harbour 3.2.0 (May 2025) - xHarbour Builder (January 2020) - Bcc773

Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: Question about wfReadUrl()
Posted: Sat Apr 06, 2024 07:33 AM

Here it is returning immediately. Can you post a sample showing that long timeout, please?

Posts: 1467
Joined: Mon Oct 10, 2005 11:26 AM
Re: Question about wfReadUrl()
Posted: Sat Apr 06, 2024 08:05 AM
Enrico,

Try this:
Code (fw): Select all Collapse
   DO CASE
     CASE UrlExist("https://www.maconsult.be") ; xDownWeb := "https://www.maconsult.be"
     CASE UrlExist("https://www.ma-consult.be") ; xDownWeb := "https://www.ma-consult.be"
  ENDCASE
The url http://www.maconsult.be doesn't exist. So it takes between 1 and 2 minutes before the next CASE is executed, resulting into xDownWeb = "https://www.ma-consult.be.

Another question. What if a url is blocked by a firewall or an antivirus. What will happen then?
That is de problem I have, especially wit BitDefender.

Regards,

Michel D.
Genk (Belgium)


_____________________________________________________________________________________________

I use : FiveWin for (x)Harbour v. 25.12 - Harbour 3.2.0 (May 2025) - xHarbour Builder (January 2020) - Bcc773

Posts: 1467
Joined: Mon Oct 10, 2005 11:26 AM
Re: Question about wfReadUrl()
Posted: Sat Apr 06, 2024 08:17 AM
Enrico,

I just did a test on a system of one of my customers who is using BitDefender.
BitDefender blocks www.ma-consult.be, but does not block the other url's.

With your suggestion using UrlExist(), a blocked URL returns into a .F., the other URL's, return into a .T.

So in case www.ma-consult.be is blocked, the url www.juda.be is used.
And that is what I wanted to happen.

So, my problem is solved, thanks to you.

Thank you so much for your help.
Have a nice weekend.

Regards,

Michel D.
Genk (Belgium)


_____________________________________________________________________________________________

I use : FiveWin for (x)Harbour v. 25.12 - Harbour 3.2.0 (May 2025) - xHarbour Builder (January 2020) - Bcc773

Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: Question about wfReadUrl() - SOLVED
Posted: Sat Apr 06, 2024 08:18 AM
Try this:
Code (fw): Select all Collapse
#include "Fivewin.ch"


FUNCTION MAIN()

    ? URLEXIST( "https://www.emagsoftware.it" )
    ? URLEXIST( "https://www.maconsult.be" )

    RETURN NIL


#command IF <condition> THEN <*statements*> => if <condition> ; <statements> ; end


STATIC FUNCTION URLEXIST( cUrl, oHtp )

    LOCAL lOk := .F.

    TRY
        DEFAULT oHtp := CREATEOBJECT( "MSXML2.SERVERXMLHTTP" )

        oHtp:SetTimeouts( 1, 1, 1, 1 )

        oHtp:Open( "GET", cUrl, .F. )

        oHtp:Send()

        IF oHtp:Status != 200 THEN BREAK

        lOk = .T.
    CATCH
    END

    RETURN lOk
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: Question about wfReadUrl() - SOLVED
Posted: Sat Apr 06, 2024 08:19 AM

Ok, great!

Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Question about wfReadUrl() - SOLVED
Posted: Sat Apr 06, 2024 11:48 PM
Another alternative
Code (fw): Select all Collapse
function ValidURL( cUrl )

   local cIp

   WsaStartUp()
   cIp = GetHostByName( cUrl )
   WsaCleanUp()

return cIp != "0.0.0.0"
Does this work for you? If it is working is it fast in all cases?
Regards



G. N. Rao.

Hyderabad, India
Posts: 1467
Joined: Mon Oct 10, 2005 11:26 AM
Re: Question about wfReadUrl() - SOLVED
Posted: Sun Apr 07, 2024 04:58 AM

Mr. Rao,

Thanks for your suggestion.

I will test it.

Regards,

Michel D.
Genk (Belgium)


_____________________________________________________________________________________________

I use : FiveWin for (x)Harbour v. 25.12 - Harbour 3.2.0 (May 2025) - xHarbour Builder (January 2020) - Bcc773