Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Visual Basic Programming Code Examples

Visual Basic > Internet Web Mail Stuff Code Examples

Download a web page on the internet (works with proxy servers)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
Download a web page on the internet (works with proxy servers) 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. 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