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

Da Gambas-it.org - Wikipedia.
(Creata pagina con 'Per sapere se una [https://it.wikipedia.org/wiki/Uniform_Resource_Locator URL] è valida, e quindi esistente, si possono utilizzare almeno due modalità. ==Uso delle sole ri...')
 
 
(16 versioni intermedie di uno stesso utente non sono mostrate)
Riga 1: Riga 1:
Per sapere se una [https://it.wikipedia.org/wiki/Uniform_Resource_Locator URL] è valida, e quindi esistente, si possono utilizzare almeno due modalità.
+
Per sapere se una [https://it.wikipedia.org/wiki/Uniform_Resource_Locator URL] è valida, e quindi esistente, si può utilizzare la funzione esterna "gethostbyname()" contenuta dalla libreria condivisa: "''libc.so.6'' ".
  
 
+
Mostriamo un semplice esempio:
==Uso delle sole risorse di Gambas con il componente ''gb.qt4.webkit''==
+
  Library "libc:6"
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"
+
  Public Struct hostent
 
+
   h_name As Pointer
  While WebView1.Progress < 1.0
+
   h_aliases As Pointer
    Wait 0.01
+
   h_addrtype As Integer
  Wend
+
   h_length As Integer
   
+
   h_addr_list As Pointer
  <FONT Color=gray>' ''Se la proprietà ".HTML" dell'oggetto "WebView" ha conservato la sua stringa predefinita di Tag ''html'', allora la URL non è valida:''</font>
+
  End Struct
  If WebView1.HTML = "<html><head></head><body></body></html>" Then
 
    Print "La URL non esiste !"
 
  Else
 
    Print "La URL esiste."
 
  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&#58;//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 no existe."
 
   
 
  '''End'''
 
 
 
 
 
 
 
==Uso della libreria esterna ''libsnmp.so.30.0.2''==
 
Questa'altra modalità fa uso della funzione esterna ''gethostbyname()'' contenuta dalla libreria ''libsnmp.so.30.0.2'', che pertanto andrà richiamata nell'applicazione Gambas.
 
 
 
Mostriamo un semplice esempio:
 
Library "libsnmp:30.0.2"
 
 
   
 
   
 
  <FONT Color=gray>' ''struct hostent *gethostbyname(const char *name)'
 
  <FONT Color=gray>' ''struct hostent *gethostbyname(const char *name)'
 
  ' ''Returns a structure of type hostent for the given host name.''</font>
 
  ' ''Returns a structure of type hostent for the given host name.''</font>
  Private Extern gethostbyname(idn As String) As Pointer
+
  Private Extern gethostbyname(idn As String) As Hostent
 
   
 
   
 
   
 
   
  '''Public''' Sub Main()
+
  Public Sub Main()
 
    
 
    
   Dim hos As Pointer
+
   Dim host As Hostent
 
   Dim idn As String
 
   Dim idn As String
    
+
  idn = "www.yahoo.ex"
+
<FONT Color=gray>' ''Impostiamo il nome di un host esistente, quindi valido:''</font>
 +
   idn = "www.gambas-it.org"
 
    
 
    
  hos = gethostbyname(idn)
+
  host = gethostbyname(idn)
  If IsNull(hos) Then
+
    Print "ping: host sconosciuto";; Quote(idn)
+
  Print "Ping: host "; Quote(idn); " valido\n"
  Else
+
  Print "Nome ufficiale dell'host: "; String@(host.h_name)
    Print "Ping: host "; Quote(idn); " valido\n"
+
  Print "Nome alias dell'host:    "; String@(Pointer@(host.h_aliases))
    Print "Nome ufficiale dell'host:";; String@(Pointer@(hos))
+
  Print "Indirizzo IP dell'host:   "; String@(Pointer@(host.h_addr_list) + 32)
    Print "Nome alias:";; String@(Pointer@(Pointer@(hos + 8)))
 
  Endif
 
 
   
 
   
  '''End'''
+
  End
 +
Se l'host è inesistente, sarà sollevato un errore di segmentazione (11).

Versione attuale delle 10:10, 20 gen 2024

Per sapere se una URL è valida, e quindi esistente, si può utilizzare la funzione esterna "gethostbyname()" contenuta dalla libreria condivisa: "libc.so.6 ".

Mostriamo un semplice esempio:

Library "libc:6"

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 Hostent


Public Sub Main()
 
 Dim host As Hostent
 Dim idn As String

' Impostiamo il nome di un host esistente, quindi valido:
 idn = "www.gambas-it.org"
  
 host = gethostbyname(idn)

 Print "Ping: host "; Quote(idn); " valido\n"
 Print "Nome ufficiale dell'host: "; String@(host.h_name)
 Print "Nome alias dell'host:     "; String@(Pointer@(host.h_aliases))
 Print "Indirizzo IP dell'host:   "; String@(Pointer@(host.h_addr_list) + 32)

End

Se l'host è inesistente, sarà sollevato un errore di segmentazione (11).