Déterminer si un url est valide

Soyez le premier à donner votre avis sur cette source.

Snippet vu 5 142 fois - Téléchargée 31 fois

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

Ajouter un commentaire Commentaires
cs_EBArtSoft Messages postés 4525 Date d'inscription dimanche 29 septembre 2002 Statut Modérateur Dernière intervention 22 avril 2019 9
8 déc. 2003 à 21:55
dans IsValidURL(ByVal 0&, sURL, 0) le byval 0& ne sert strictement a rien tu peut juste ecrire IsValidURL(0, sURL, 0)

@+
gabchampagne Messages postés 216 Date d'inscription mercredi 2 avril 2003 Statut Membre Dernière intervention 5 mai 2004
9 déc. 2003 à 04:29
Attention. J'ai spécifié : CE N'EST PAS DE MOI. mais, c bon à savoir
sonoboss Messages postés 178 Date d'inscription lundi 17 juin 2002 Statut Membre Dernière intervention 2 octobre 2007
9 déc. 2003 à 10:39
Le fait qu'il ne soit pas de toi n'implique pas forcément de le recracher bettement ;-)
Renfield Messages postés 17287 Date d'inscription mercredi 2 janvier 2002 Statut Modérateur Dernière intervention 27 septembre 2021 71
9 déc. 2003 à 13:54
Je trouve dommage de se contenter de faire un copier coller de codes presenrts sur le Net....... On pourrais jouer à ça longtemps !!

surtout si on colle ca deux fois de suite, n'est-ce pas....

Non, vraiment, je trouve pas ça très bien.....
gabchampagne Messages postés 216 Date d'inscription mercredi 2 avril 2003 Statut Membre Dernière intervention 5 mai 2004
9 déc. 2003 à 19:01
Ok. voici mon interprétation :
'<
Private Const S_OK = &H0 'Retroune ça si l'URL est OK
Private Declare Function IsValidURL Lib "URLMON.DLL" (ByVal pbc As Long, ByVal szURL As String, ByVal dwReserved As Long) As Long 'API utilisée
Public Function URLOk(URL As String) As Boolean 'Retourne TRUE si l'URL est OK
Dim str As String
str = StrConv(URL, vbUnicode) 'L'API prend juste le Unicode
If IsValidURL(0, str, 0) = S_OK Then
URLOk = True 'URL OK
Else
URLOk = False 'URL Invalide
End If
End Function
'>

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.