Sapere se un indirizzo web è valido: differenze tra le versioni

Da Gambas-it.org - Wikipedia.
Vai alla navigazione Vai alla ricerca
Nessun oggetto della modifica
Nessun oggetto della modifica
Riga 1: Riga 1:
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 ''libsnmp.so.40.1.0'', che pertanto andrà richiamata nell'applicazione Gambas.
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:
Mostriamo un semplice esempio:
  Library "libsnmp:40.1.0"
  Library "libc:6"
   
   
  Public Struct hostent
  Public Struct hostent
Riga 14: Riga 14:
  <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
   
   
   
   
Riga 21: Riga 21:
   Dim host As Hostent
   Dim host As Hostent
   Dim idn As String
   Dim idn As String
  Dim p As Pointer
    
    
  <FONT Color=gray>' ''Impostiamo il nome di un host inesistente, quindi non valido:''</font>
  <FONT Color=gray>' ''Impostiamo il nome di un host inesistente, quindi non valido:''</font>
   idn = "www.yahoo.ex"
   idn = "www.yahoo.ex"
    
    
   p = gethostbyname(idn)
   host = gethostbyname(idn)
   If p = 0 Then
   If Not Object.IsValid(host) Then
     Print "ping: host sconosciuto", Quote(idn)
     Print "ping: host sconosciuto", Quote(idn)
   Else
   Else
    host = p
     Print "Ping: host "; Quote(idn); " valido\n"
     Print "Ping: host "; Quote(idn); " valido\n"
     Print "Nome ufficiale dell'host: "; String@(host.h_name)
     Print "Nome ufficiale dell'host: "; String@(host.h_name)
     Print "Nome alias dell'host:    "; String@(Pointer@(host.h_aliases))
     Print "Nome alias dell'host:    "; String@(Pointer@(host.h_aliases))
    Print "Indirizzo IP dell'host:  "; String@(Pointer@(host.h_addr_list) + 32)
   Endif
   Endif
   
   
  End
  End

Versione delle 09:49, 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 inesistente, quindi non valido:
 idn = "www.yahoo.ex"
  
 host = gethostbyname(idn)
 If Not Object.IsValid(host) Then
   Print "ping: host sconosciuto", Quote(idn)
 Else
   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)
 Endif

End