Option Explicit
Private Declare Function InternetOpen Lib "wininet" Alias "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Long) As Long
Private Declare Function InternetCloseHandle Lib "wininet" (ByVal hInet As Long) As Integer
Private Declare Function InternetReadFile Lib "wininet" (ByVal hFile As Long, ByVal sBuffer As String, ByVal lNumBytesToRead As Long, lNumberOfBytesRead As Long) As Integer
Private 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
Private 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
Private 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
Private Declare Function HttpSendRequest Lib "wininet.dll" Alias "HttpSendRequestA" (ByVal hHttpRequest As Long, ByVal sHeaders As String, ByVal lHeadersLength As Long, sOptional As Any, ByVal lOptionalLength As Long) As Integer
'Purpose : Retreview text from a web site
'Inputs : sServerName The server name where the file is located eg.
' sFileName The file name to download eg. "index.asp" or "/code/codetoc.asp"
' [sUsername] If required, the login user name.
' [sPassword] If required, the user's password.
' [lBufferSize] The size of that the packets data downloaded in.
'Outputs : The contents of the specified file
'Notes : Can be used through a proxy server by specifying a username and password
'Revisions :
Function InternetGetText(sServerName As String, sFileName As String, Optional sUsername As String = vbNullString, Optional sPassword As String = vbNullString, Optional lBufferSize As Long = -1) As String
Dim hInternetSession As Long, hInternetConnect As Long, hHttpOpenRequest As Long
Dim lRetVal As Long, lLenFile As Long, lNumberOfBytesRead As Long, lResLen As Long
Dim sBuffer As String, lTotalBytesRead As Long
Const clBufferIncrement As Long = 2000, scUserAgent As String = "VBUsers"
Const INTERNET_OPEN_TYPE_PRECONFIG = 0, INTERNET_FLAG_EXISTING_CONNECT = &H20000000
Const INTERNET_OPEN_TYPE_DIRECT = 1, INTERNET_OPEN_TYPE_PROXY = 3
Const INTERNET_DEFAULT_HTTP_PORT = 80, INTERNET_FLAG_RELOAD = &H80000000
Const INTERNET_SERVICE_HTTP = 3
Const HTTP_QUERY_CONTENT_LENGTH = 5
If lBufferSize = -1 Then
'Create an arbitary buffer to read the whole file in parts
sBuffer = String$(clBufferIncrement, vbNullChar)
lBufferSize = clBufferIncrement
Else
'Create a specified buffer size
sBuffer = String$(lBufferSize, vbNullChar)
End If
'Initializes an application's use of the Win32 Internet functions
hInternetSession = InternetOpen(scUserAgent, INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)
'Opens an FTP, Gopher, or HTTP session for a given site
hInternetConnect = InternetConnect(hInternetSession, sServerName, INTERNET_DEFAULT_HTTP_PORT, sUsername, sPassword, INTERNET_SERVICE_HTTP, 0, 0)
'Create an HTTP request handle
hHttpOpenRequest = HttpOpenRequest(hInternetConnect, "GET", sFileName, "HTTP/1.0", vbNullString, 0, INTERNET_FLAG_RELOAD, 0)
'Creates a new HTTP request handle and stores the specified parameters in that handle
lRetVal = HttpSendRequest(hHttpOpenRequest, vbNullString, 0, 0, 0)
If lRetVal Then
'Determine the file size
lResLen = lBufferSize
lRetVal = HttpQueryInfo(hHttpOpenRequest, HTTP_QUERY_CONTENT_LENGTH, ByVal sBuffer, lResLen, 0)
If lRetVal Then
'Successfully returned file length
lLenFile = Val(Left$(sBuffer, lResLen))
'Create a buffer to hold file
sBuffer = String$(lLenFile, vbNullChar)
lBufferSize = lLenFile
Else
'Unable to establish file length
lLenFile = -1
End If
'Read the file
Do
lRetVal = InternetReadFile(hHttpOpenRequest, sBuffer, lBufferSize, lNumberOfBytesRead)
'Store the results
InternetGetText = InternetGetText & Left$(sBuffer, lNumberOfBytesRead)
lTotalBytesRead = lTotalBytesRead + lNumberOfBytesRead
If lNumberOfBytesRead = 0 Or lTotalBytesRead = lLenFile Or lRetVal = 0 Then
'Finished reading file
Exit Do
End If
Loop
End If
'Close handles
InternetCloseHandle hHttpOpenRequest
InternetCloseHandle hInternetSession
InternetCloseHandle hInternetConnect
End Function
'Demonstration routine
'(Note the Debug window will only show the last 255 lines)
Sub Test()
Dim sInternetFile As String
sInternetFile = InternetGetText("www.example", "/index.asp", "myusername", "mypassword")
Debug.Print "File Donwloaded: " & vbNewLine
Debug.Print sInternetFile
End Sub
No comments:
Post a Comment