~ Edito ~
12/03/2006 @ 13:39
Depuis la dernière mise à jour (qui remonte à... oulala plusieurs mois), un petit ménage de printemps s'impose. Ca tombe bien, c'est presque la période. Au menu, et progressivement sur les jours à venir, rafraîchissement de plusieurs fonctions et procédures, nouvelles APIs et nouveaux programmes.
~ Rechercher ~
~ Annuaire VB ~
~ Partenaires ~
|
Fonction HttpQueryInfo
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
Versions : . Windows 95/98 : Oui . Windows NT : A partir de 4.0 . Windows 2000/XP : OuiDescription : Cette fonction sert à récupérer des informations faisant suite à une requête HTTP.
hHttpRequest est le pointeur vers la requête HTTP,
lInfoLevel est une constante représentant l'information que l'on cherche à obtenir (voir tableau ci-dessous),
sBuffer chaîne dans laquelle sera contenue la réponse,
lBufferLength précise la longueur de la chaîne sBuufer,
lIndex pointeur utilisé pour les fonctions d'énumération.
Retourne FAUX en cas d'échec et VRAI en cas de réussite.
Constante | Valeur |
Définition |
HTTP_QUERY_CONTENT_TYPE | 1 |
Récupère le type de contenu (valeur de la zone 'Content-type'). |
HTTP_QUERY_CONTENT_LENGTH | 5 |
Permet de retourner la longueur du contenu. |
HTTP_QUERY_EXPIRES | 10 |
Lit la date et l'heure d'expiration du document. |
HTTP_QUERY_LAST_MODIFIED | 11 |
Pour connaître la date de dernière modification du document. |
HTTP_QUERY_MIME_VERSION | 0 |
Retourne la version du protocole MIME utilisé pour la construction du message. |
HTTP_QUERY_VERSION | 18 |
Récupère le dernier code de réponse retourné par le serveur. |
HTTP_QUERY_STATUS_CODE | 19 |
Retourne le dernier code d'état HTTP du serveur (200, 403, 404, ...). |
HTTP_QUERY_STATUS_TEXT | 20 |
Retourne un texte lié au code d'état HTTP. |
HTTP_QUERY_RAW_HEADERS | 21 |
Renvoie tous les entêtes du serveur (séparés par des caractères nuls). |
HTTP_QUERY_RAW_HEADERS_CRLF | 22 |
Renvoie tous les entêtes du serveur (séparés par des sauts de lignes). |
HTTP_QUERY_SERVER | 37 |
Permet de retourner des informations sur le logiciel utilisé sur le serveur internet. |
HTTP_QUERY_USER_AGENT | 39 |
Indique le 'User Agent' qui a effectué la requête |
HTTP_QUERY_SET_COOKIE | 43 |
Retourne la valeur du cookie lié à la requête. |
HTTP_QUERY_REQUEST_METHOD | 45 |
Lit la méthode utilisée pour la requête (GET ou POST). |
Exemple d'utilisation :
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
Public Enum INTERNET_DEF
INTERNET_DEFAULT_HTTP_PORT = 80
INTERNET_DEFAULT_HTTPS_PORT = 443
End Enum
Public Function UrlPost(stURL As String, stPostData As String, _
stStatusCode As String, stStatusText As String, _
Optional lgInternet As INTERNET_DEF = INTERNET_DEFAULT_HTTP_PORT, _
Optional stUser As String = vbNullString, _
Optional stPass As String = vbNullString) 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
' 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)
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
' 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
UrlPost = stLoad
End Function
Retour à la liste des API |