Hit-Parade .VB Research Center . Compteur
Accueil ~  Code ~  Programmes ~  Api ~  Forum ~  Cours ~  Livres ~  Quiz ~  Annuaire
~ 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 ~
 Rechercher un site :
  

~ Partenaires ~

Fonction HttpSendRequest

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
Versions :
. Windows 95/98 : Oui
. Windows NT : A partir de 4.0
. Windows 2000/XP : Oui

Description :
Cette fonction envoie une requête vers un serveur HTTP.
hHttpRequest est un pointeur vers une requête HTTP (obtenu par HttpOpenRequest),
sHeaders permet d'indiquer des entêtes supplémentaires,
lHeadersLength contient la longueur de la chaîne précédente,
sOptional permet d'envoyer des données après les entêtes, utilisé en général pour les requêtes de type POST,
lOptionalLength donne la longueur de la chaîne précédente.

En cas de réussite la fonction retourne VRAI, sinon FAUX.

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

Visual Basic Research Center - (c) 2000/2002 -  Webmaster : docvb (chez) free (point) fr