Page 1 of 2

Question about wfReadUrl() - SOLVED

PostPosted: Fri Apr 05, 2024 7:55 am
by driessen
Hello,

I use this code to check if a website exists or not.
Code: Select all  Expand view
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: Select all  Expand view
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.

Re: Question about wfReadUrl()

PostPosted: Fri Apr 05, 2024 3:25 pm
by cmsoft
Puedes usar la funcion WebPageContents(cUrl)

Code: Select all  Expand view

if !Empty(WebPageContents(cUrl))
   //... tu codigo
else
   MsgStop('Url not found')
endif
 

Re: Question about wfReadUrl()

PostPosted: Fri Apr 05, 2024 3:30 pm
by driessen
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.

Re: Question about wfReadUrl()

PostPosted: Fri Apr 05, 2024 5:28 pm
by Enrico Maria Giordano
Michel, please show a little and complete PRG example of what you are trying to do and I will fix it.

Re: Question about wfReadUrl()

PostPosted: Fri Apr 05, 2024 8:30 pm
by driessen
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: Select all  Expand view
  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.

Re: Question about wfReadUrl()

PostPosted: Fri Apr 05, 2024 8:46 pm
by cmsoft
Prueba este ejemplo, aqui funciona bien con https
Code: Select all  Expand view

#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
 

Re: Question about wfReadUrl()

PostPosted: Fri Apr 05, 2024 9:13 pm
by Enrico Maria Giordano
Try something like this:

Code: Select all  Expand view
#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

Re: Question about wfReadUrl()

PostPosted: Fri Apr 05, 2024 10:16 pm
by driessen
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.

Re: Question about wfReadUrl()

PostPosted: Sat Apr 06, 2024 7:33 am
by Enrico Maria Giordano
Here it is returning immediately. Can you post a sample showing that long timeout, please?

Re: Question about wfReadUrl()

PostPosted: Sat Apr 06, 2024 8:05 am
by driessen
Enrico,

Try this:
Code: Select all  Expand view
  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.

Re: Question about wfReadUrl()

PostPosted: Sat Apr 06, 2024 8:17 am
by driessen
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.

Re: Question about wfReadUrl() - SOLVED

PostPosted: Sat Apr 06, 2024 8:18 am
by Enrico Maria Giordano
Try this:

Code: Select all  Expand view
#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

Re: Question about wfReadUrl() - SOLVED

PostPosted: Sat Apr 06, 2024 8:19 am
by Enrico Maria Giordano
Ok, great!

Re: Question about wfReadUrl() - SOLVED

PostPosted: Sat Apr 06, 2024 11:48 pm
by nageswaragunupudi
Another alternative
Code: Select all  Expand view
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?

Re: Question about wfReadUrl() - SOLVED

PostPosted: Sun Apr 07, 2024 4:58 am
by driessen
Mr. Rao,

Thanks for your suggestion.
I will test it.