| {\rtf1\ansi\ansicpg1252\deff0\deflang1044{\fonttbl{\f0\fnil\fcharset0 MS Sans Serif;}} \viewkind4\uc1\pard\f0\fs17 Download a web page on the internet (works with proxy servers) \par \par The following routine uses API calls to read/download an internet file or an html page from a remote web site. The code will work with a proxy server and a routine demonstrating how to use this code can be found at the bottom of the post. \par \par \par Option Explicit \par 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 \par Private Declare Function InternetCloseHandle Lib "wininet" (ByVal hInet As Long) As Integer \par Private Declare Function InternetReadFile Lib "wininet" (ByVal hFile As Long, ByVal sBuffer As String, ByVal lNumBytesToRead As Long, lNumberOfBytesRead As Long) As Integer \par 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 \par 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 \par 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 \par 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 \par \par \par 'Purpose : Retreview text from a web site \par 'Inputs : sServerName The server name where the file is located eg. "www.vbusers.com" \par ' sFileName The file name to download eg. "index.asp" or "/code/codetoc.asp" \par ' [sUsername] If required, the login user name. \par ' [sPassword] If required, the user's password. \par ' [lBufferSize] The size of that the packets data downloaded in. \par 'Outputs : The contents of the specified file \par 'Notes : Can be used through a proxy server by specifying a username and password \par \par 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 \par Dim hInternetSession As Long, hInternetConnect As Long, hHttpOpenRequest As Long \par Dim lRetVal As Long, lLenFile As Long, lNumberOfBytesRead As Long, lResLen As Long \par Dim sBuffer As String, lTotalBytesRead As Long \par \par Const clBufferIncrement As Long = 2000, scUserAgent As String = "VBUsers" \par Const INTERNET_OPEN_TYPE_PRECONFIG = 0, INTERNET_FLAG_EXISTING_CONNECT = &H20000000 \par Const INTERNET_OPEN_TYPE_DIRECT = 1, INTERNET_OPEN_TYPE_PROXY = 3 \par Const INTERNET_DEFAULT_HTTP_PORT = 80, INTERNET_FLAG_RELOAD = &H80000000 \par Const INTERNET_SERVICE_HTTP = 3 \par Const HTTP_QUERY_CONTENT_LENGTH = 5 \par \par If lBufferSize = -1 Then \par 'Create an arbitary buffer to read the whole file in parts \par sBuffer = String$(clBufferIncrement, vbNullChar) \par lBufferSize = clBufferIncrement \par Else \par 'Create a specified buffer size \par sBuffer = String$(lBufferSize, vbNullChar) \par End If \par \par 'Initializes an application's use of the Win32 Internet functions \par hInternetSession = InternetOpen(scUserAgent, INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0) \par 'Opens an FTP, Gopher, or HTTP session for a given site \par hInternetConnect = InternetConnect(hInternetSession, sServerName, INTERNET_DEFAULT_HTTP_PORT, sUsername, sPassword, INTERNET_SERVICE_HTTP, 0, 0) \par 'Create an HTTP request handle \par \par hHttpOpenRequest = HttpOpenRequest(hInternetConnect, "GET", sFileName, "HTTP/1.0", vbNullString, 0, INTERNET_FLAG_RELOAD, 0) \par \par 'Creates a new HTTP request handle and stores the specified parameters in that handle \par lRetVal = HttpSendRequest(hHttpOpenRequest, vbNullString, 0, 0, 0) \par If lRetVal Then \par 'Determine the file size \par lResLen = lBufferSize \par lRetVal = HttpQueryInfo(hHttpOpenRequest, HTTP_QUERY_CONTENT_LENGTH, ByVal sBuffer, lResLen, 0) \par If lRetVal Then \par 'Successfully returned file length \par lLenFile = Val(Left$(sBuffer, lResLen)) \par 'Create a buffer to hold file \par sBuffer = String$(lLenFile, vbNullChar) \par lBufferSize = lLenFile \par Else \par 'Unable to establish file length \par lLenFile = -1 \par End If \par \par 'Read the file \par Do \par lRetVal = InternetReadFile(hHttpOpenRequest, sBuffer, lBufferSize, lNumberOfBytesRead) \par 'Store the results \par InternetGetText = InternetGetText & Left$(sBuffer, lNumberOfBytesRead) \par lTotalBytesRead = lTotalBytesRead + lNumberOfBytesRead \par If lNumberOfBytesRead = 0 Or lTotalBytesRead = lLenFile Or lRetVal = 0 Then \par 'Finished reading file \par Exit Do \par End If \par Loop \par End If \par 'Close handles \par InternetCloseHandle hHttpOpenRequest \par InternetCloseHandle hInternetSession \par InternetCloseHandle hInternetConnect \par End Function \par \par 'Demonstration routine \par '(Note the Debug window will only show the last 255 lines) \par Sub Test() \par Dim sInternetFile As String \par sInternetFile = InternetGetText("www.vbusers.com", "/index.asp", "myusername", "mypassword") \par Debug.Print "File Donwloaded: " & vbNewLine \par Debug.Print sInternetFile \par End Sub \par } |
Download a web page on the internet |
India web developer web development India | India web development company India ecommerce web developer