Differenze tra le versioni di "Sapere se un indirizzo web è valido"

Da Gambas-it.org - Wikipedia.
Riga 19: Riga 19:
 
   Endif
 
   Endif
 
   
 
   
End
 
 
 
Un'altra modalità con l'uso delle sole risorse di Gambas, attivando in particolare, come nell'esempio precedente, il componente ''gb.qt4.webkit'':
 
Public Sub Main()
 
 
  WebView1.Url = "http://www.gambas-it.org"
 
 
 
  While WebView1.Progress < 1.0
 
    Wait 0.01
 
  Wend
 
 
 
<FONT Color=gray>' ''Se la proprietà ".Text" dell'oggetto "WebView" è nulla, allora la URL molto probbilmente non è valida:''</font>
 
  If WebView1.Text = Null Then Print "La URL non esiste."
 
 
 
 
  End
 
  End
  
Riga 41: Riga 26:
  
 
Mostriamo un semplice esempio:
 
Mostriamo un semplice esempio:
 +
Library "libsnmp:40.1.0"
 +
 
  Public Struct hostent
 
  Public Struct hostent
 
   h_name As Pointer
 
   h_name As Pointer
Riga 48: Riga 35:
 
   h_addr_list As Pointer
 
   h_addr_list As Pointer
 
  End Struct
 
  End Struct
 
 
Library "libsnmp:40.1.0"
 
 
   
 
   
 
  <FONT Color=gray>' ''struct hostent *gethostbyname(const char *name)'
 
  <FONT Color=gray>' ''struct hostent *gethostbyname(const char *name)'

Versione delle 10:14, 18 gen 2024

Per sapere se una URL è valida, e quindi esistente, si possono utilizzare almeno due modalità.


Uso delle sole risorse di Gambas con il componente gb.qt4.webkit

Volendo utilizzare le sole risorse di Gambas, un possibile codice sarebbe ad esempio il seguente, che prevede sia attivato preliminarmente il componente gb.qt4.webkit e fa uso dell'oggetto WebView:

Public Sub Main()

 WebView1.Url = "http://www.gambas-it.org"
  
 While WebView1.Progress < 1.0
   Wait 0.01
 Wend
   
' Se la proprietà ".HTML" dell'oggetto "WebView" ha conservato la sua stringa predefinita di Tag html, allora la URL non è valida:
 If WebView1.HTML = "<html><head></head><body></body></html>" Then
   Print "La URL non esiste !"
 Else
   Print "La URL esiste."
 Endif

End


Uso della libreria esterna libsnmp

Questa'altra modalità fa uso della funzione esterna gethostbyname() contenuta dalla libreria libsnmp.so.40.1.0, che pertanto andrà richiamata nell'applicazione Gambas.

Mostriamo un semplice esempio:

Library "libsnmp:40.1.0"

Public Struct hostent
  h_name As Pointer
  h_aliases As Pointer
  h_addrtype As Integer
  h_length As Integer
  h_addr_list As Pointer
End Struct

' struct hostent *gethostbyname(const char *name)'
' Returns a structure of type hostent for the given host name.
Private Extern gethostbyname(idn As String) As Pointer


Public Sub Main()
 
 Dim host As Hostent
 Dim idn As String
 Dim p As Pointer
 
' Impostiamo il nome di un host inesistente, quindi non valido:
 idn = "www.yahoo.ex"
  
 p = gethostbyname(idn)
 If p = 0 Then
   Print "ping: host sconosciuto", Quote(idn)
 Else
   host = p
   Print "Ping: host "; Quote(idn); " valido\n"
   Print "Nome ufficiale dell'host:", String@(host.h_name)
   Print "Nome alias:", String@(Pointer@(host.h_aliases))
 Endif

End