Déterminer si un url est valide

Contenu du snippet

C'est simple. Ça permet de savoir si une URL est valide. Ça ne vient pas de moi. J'ai trouvé ça sur le site www.allapi.net

Source / Exemple :


Private Const S_FALSE = &H1
Private Const S_OK = &H0
'Only implemented as unicode...
Private Declare Function IsValidURL Lib "URLMON.DLL" (ByVal pbc As Long, ByVal szURL As String, ByVal dwReserved As Long) As Long
Private Sub Form_Load()
    'KPD-Team 2001
    'URL: http://www.allapi.net/
    'E-Mail: KPDTeam@Allapi.net
    MsgBox "Is valid URL: " + CStr(IsGoodURL("http://www.allapi.net"))
    MsgBox "Is valid URL: " + CStr(IsGoodURL("hxxp:/www.allapi.uhoh"))
End Sub
Public Function IsGoodURL(ByVal sURL As String) As Boolean
    'The IsValidURL always expects a UNICODE string, but whenever
    'VB calls an API function, it converts the strings to ANSI strings.
    'That's why we're going to use a trick here. Before calling the function,
    'We're going to convert the unicode string to unicode so we get a double
    'unicode string.
    'Before VB calls the API function, it converts our double unicode string
    'to a normal unicode string; exactely what IsValidURL is expecting.
    sURL = StrConv(sURL, vbUnicode)
    'Now call the function
    IsGoodURL = (IsValidURL(ByVal 0&, sURL, 0) = S_OK)
End Function 
Private Const S_FALSE = &H1
Private Const S_OK = &H0
'Only implemented as unicode...
Private Declare Function IsValidURL Lib "URLMON.DLL" (ByVal pbc As Long, ByVal szURL As String, ByVal dwReserved As Long) As Long
Private Sub Form_Load()
    'KPD-Team 2001
    'URL: http://www.allapi.net/
    'E-Mail: KPDTeam@Allapi.net
    MsgBox "Is valid URL: " + CStr(IsGoodURL("http://www.allapi.net"))
    MsgBox "Is valid URL: " + CStr(IsGoodURL("hxxp:/www.allapi.uhoh"))
End Sub
Public Function IsGoodURL(ByVal sURL As String) As Boolean
    'The IsValidURL always expects a UNICODE string, but whenever
    'VB calls an API function, it converts the strings to ANSI strings.
    'That's why we're going to use a trick here. Before calling the function,
    'We're going to convert the unicode string to unicode so we get a double
    'unicode string.
    'Before VB calls the API function, it converts our double unicode string
    'to a normal unicode string; exactely what IsValidURL is expecting.
    sURL = StrConv(sURL, vbUnicode)
    'Now call the function
    IsGoodURL = (IsValidURL(ByVal 0&, sURL, 0) = S_OK)
End Function

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.