Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

Lister les fichiers d'un répertoire sur un serveur HTTP

11 réponses
Avatar
JM
Bonjour =E0 tous les pros ;)

J'ai un soucis... Je ne sais pas comment lister les=20
fichiers contenus dans un r=E9pertoire d'un serveur HTTP...

J'ai bien trouv=E9 le moyen de downloader le fichier si je=20
connais son nom avec l'API URLDownloadToFileA, mais pas=20
moyen de trouver comment LISTER les fichiers contenus dans=20
un r=E9pertoire...

Merci d'avance pour votre aide =E0 tous...

Jean-Michel
.

1 réponse

1 2
Avatar
François Picalausa
Bonjour/soir,

Et-si on le faisait à coup de MSHTML.tlb/Internet Explorer?
Si ce sont des liens, y suffit de récupérer tout ce qu'il y a dans la
collection anchor d'un document ;-)

--
François Picalausa (MVP VB)
FAQ VB : http://faq.vb.free.fr
MSDN : http://msdn.microsoft.com


"ng" a écrit dans le message de
news:
Salut,

Donc je reviens à ce que je disais, on peut très bien faire cela avec
URLDownloadToFile !!!


Je récupère le code HTML de la page, je recherche les
fichiers listés dans le code : qui commencent par "toto_"
et qui finissent par .txt", et je renvoi la liste à
l'appelant sous forme de liste séparée par des ;

Ensuite, je peut utiliser la copie si dans cette liste, un
des fichiers n'est pas présent en local...

voilà...


-----Message d'origine-----
Salut,

Ce code permet juste de récpérer la page non ? Quels avantages par
rapport à URLDownloadToFile pour ton appli ?

--
Nicolas G.
FAQ VB : http://faq.vb.free.fr
API Guide : http://www.allapi.net
Google Groups : http://groups.google.fr/
MZ-Tools : http://www.mztools.com/

JM a écrit :

Merci à tous de m'avoir mis sur la piste...

Je me suis "bidouillé un truc pour récupérer la liste des
fichiers affichés dans une page web :

Declare Function InternetOpen Lib "wininet.dll"
Alias "InternetOpenA" ( _
ByVal sAgent As String, _
ByVal lAccessType As Long, _
ByVal sProxyName As String, _
ByVal sProxyBypass As String, _
ByVal lFlags As Long) As Long

Declare Function InternetConnect Lib "wininet.dll"
Alias "InternetConnectA" ( _
ByVal hInternetSession As Long, _
ByVal sServerName As String, _
ByVal nServerPort As Integer, _
ByVal sUsername As String, _
ByVal sPassword As String, _
ByVal lService As Long, _
ByVal lFlags As Long, _
ByVal lContext As Long) As Long

Declare Function HttpOpenRequest Lib "wininet.dll"
Alias "HttpOpenRequestA" ( _
ByVal hHttpSession As Long, _
ByVal sVerb As String, _
ByVal sObjectName As String, _
ByVal sVersion As String, _
ByVal sReferer As String, _
ByVal something As Long, _
ByVal lFlags As Long, _
ByVal lContext As Long) As Long

Declare Function HttpSendRequest Lib "wininet.dll"
Alias "HttpSendRequestA" ( _
ByVal hHttpRequest As Long, _
ByVal sHeaders As String, _
ByVal lHeadersLength As Long, _
ByVal sOptional As String, _
ByVal lOptionalLength As Long) As Long

Declare Function InternetReadFile Lib "wininet.dll" ( _
ByVal hFile As Long, _
ByVal sBuffer As String, _
ByVal lNumBytesToRead As Long, _
lNumberOfBytesRead As Long) As Long

Declare Function HttpQueryInfo Lib "wininet.dll"
Alias "HttpQueryInfoA" ( _
ByVal hHttpRequest As Long, _
ByVal lInfoLevel As Long, _
ByRef sBuffer As Any, _
ByRef lBufferLength As Long, _
ByRef lIndex As Long) As Integer

Declare Function InternetCloseHandle Lib "wininet.dll" ( _
ByVal hInet As Long) As Integer

Const INTERNET_FLAG_SECURE = &H800000
Const INTERNET_FLAG_IGNORE_CERT_CN_INVALID = &H1000
Const INTERNET_FLAG_RELOAD = &H80000000

Const HTTP_QUERY_STATUS_CODE = 19
Const HTTP_QUERY_STATUS_TEXT = 20

Const INTERNET_SERVICE_HTTP = 3

Const INTERNET_OPEN_TYPE_PRECONFIG = 0

Const INTERNET_DEFAULT_HTTP_PORT = 80
Const INTERNET_DEFAULT_HTTPS_PORT = 443

Public Function UrlPost(stURL As String, stPostData As
String, _
stStatusCode As String, stStatusText As
String, _
Optional lgInternet As Integer >>>> INTERNET_DEFAULT_HTTP_PORT, _
Optional stUser As String = vbNullString, _
Optional stPass As String = vbNullString, _
Optional stFiltreDeb As String, _
Optional stFiltreFin As String) As String
' Cette fonction permet lire le contenu d'une URL.
' Elle permet éventuellement aussi de poster des données
sur un formulaire
' et peut fonctionner en mode HTTP et HTTPS
Dim stRead As String * 2048, lgRead As Long
Dim stLoad As String
Dim blDoLoop As Boolean
Dim hISession As Long, hIConnect As Long, hRequest As Long
Dim stUrlDeb As String, stUrlFin As String
Dim stMethod As String
Dim stPost As String
Dim lgFlags As Long, lgRep As Long

Dim i As Integer
Dim i2 As Integer
Dim stLoadFiltre As String
Dim strF As String

' Découpage de l'URL en serveur et fichier
If (InStr(1, stURL, "/") > 0) Then
'stUrlDeb = Replace(LCase$(stURL), "http://",
vbNullString)
'stUrlDeb = Replace(LCase$(stUrlDeb), "https://",
vbNullString)
stUrlDeb = stURL
stUrlFin = stUrlDeb
stUrlDeb = Left$(stUrlDeb, InStr(1, stUrlDeb, "/") -




1)
stUrlFin = Mid$(stUrlFin, InStr(1, stUrlFin, "/") + 1)
Else
stUrlDeb = stURL
stUrlFin = vbNullString
End If
' Mise au point de la méthode d'envoi
If (stPostData <> vbNullString) Then
stPost = stPostData
stMethod = "POST"
stLoad = "Content-Type: application/x-www-form-
urlencoded" & vbCrLf
Else
stPost = vbNullString
stMethod = "GET"
stLoad = vbNullString
End If
If (lgInternet = INTERNET_DEFAULT_HTTPS_PORT) Then
lgFlags = INTERNET_FLAG_SECURE Or _
INTERNET_FLAG_IGNORE_CERT_CN_INVALID
Else
lgFlags = INTERNET_FLAG_RELOAD
End If

' Mise en place de la connexion Internet
hISession = InternetOpen(stUserAgent,
INTERNET_OPEN_TYPE_PRECONFIG, _
vbNullString, vbNullString, 0)
If CBool(hISession) Then
' Ouverture de la connexion internet
hIConnect = InternetConnect(hISession, _
stUrlDeb, _
lgInternet, _
stUser, _
stPass, _
INTERNET_SERVICE_HTTP, _
0, _
0)
' Préparation de l'ouverture de la page
hRequest = HttpOpenRequest(hIConnect, _
stMethod, _
stUrlFin, _
"HTTP/1.0", _
vbNullString, _
0, _
lgFlags, _
0)
' Lancement de l'URL (avec les paramètres le cas
échéant)
lgRep = HttpSendRequest(hRequest, stLoad, Len (stLoad),
stPost, Len(stPost))
' Récupération du texte/contenu de la page
blDoLoop = True
stLoad = vbNullString
Do While blDoLoop
stRead = vbNullString
blDoLoop = InternetReadFile(hRequest, stRead, Len
(stRead), lgRead)
stLoad = stLoad & Left$(stRead, lgRead)
If Not CBool(lgRead) Then blDoLoop = False
Loop

If stFiltreDeb <> "" Then
stLoadFiltre = ""
i = InStr(1, stLoad, stFiltreDeb)
While i <> 0
i2 = InStr(i, stLoad, stFiltreFin)
strF = Mid$(stLoad, i + 1, i2 - i + Len
(stFiltreFin) - 2)
stLoadFiltre = stLoadFiltre & IIf (stLoadFiltre
= "", "", vbCrLf) & strF

i = InStr(i2, stLoad, stFiltreDeb)
Wend
End If

' Code http de retour (statut)
stStatusCode = Space$(1024)
lgRead = 1024
HttpQueryInfo hRequest, HTTP_QUERY_STATUS_CODE, ByVal
stStatusCode, lgRead, 0
stStatusCode = Left$(stStatusCode, lgRead)
' Texte associé
stStatusText = Space$(1024)
lgRead = 1024
HttpQueryInfo hRequest, HTTP_QUERY_STATUS_TEXT, ByVal
stStatusText, lgRead, 0
stStatusText = Left$(stStatusText, lgRead)
End If

' Fermeture des connexions
On Error Resume Next
InternetCloseHandle hISession
InternetCloseHandle hIConnect
InternetCloseHandle hRequest
On Error GoTo 0

' Retourne le contenu de la page chargée
If stFiltreDeb <> "" Then
UrlPost = stLoadFiltre
Else
UrlPost = stLoad
End If
End Function

Sub test()
Dim stStatusCode As String
Dim stStatusText As String
Dim stPageHtml As String

stPageHtml = UrlPost("www.toto.fr", "", stStatusCode,
stStatusText, , "", "", ">file_", ".txt<")
MsgBox stStatusCode & " - " & stStatusText
MsgBox stPageHtml
End Sub

-----Message d'origine-----
Bonjour à tous les pros ;)

J'ai un soucis... Je ne sais pas comment lister les
fichiers contenus dans un répertoire d'un serveur HTTP...

J'ai bien trouvé le moyen de downloader le fichier si je
connais son nom avec l'API URLDownloadToFileA, mais pas
moyen de trouver comment LISTER les fichiers contenus dans
un répertoire...

Merci d'avance pour votre aide à tous...

Jean-Michel
..


.






.






1 2