Internet Transfer Control in VB6
This control is good for short pages only. Microsoft's Internet Transfer control has a bug whereby you will not always get the full contents of the page. See the WinInet section of this page for details on the work around. There is a fix out by Microsoft in the form of a service pack. If you use the service pack fix then you won't need to concern yourself with the bug or work around.
Contents
Inet Transfer Control
Properties
- AccessType property
- icUseDefault
- icDirect
- icNamedProxy
- protocols
- icUnknown
- icDefault
- icFTP
- icGopher
- icHTTP
- icHTTPS
icHTTP and icFTP
- URL - The complete URL of what you want to communicate with. e.g. http://wiki.robotz.com/
- RemoteHost - The name/IP address of the remote host you are connecting with.
- RemotePort - The port to connect on. For HTTP connections, you want port 80. For FTP, port 21.
- Document - The name of an individual document
- UserName - Username to login with
- Password - Password that goes with the username
Inet Control States
The Internet Transfer Control provides us with one event, StateChanged. And, when this event is raised, it provides only one parameters, State. We can use this parameter to detect what state the control is in, and then react accordingly.
State constants:
- icConnected (4) - Connected
- icConnecting (3) - Connecting
- icDisconnected (10) - Disconnected
- icDisconnecting (9) - Disconnecting
- icError (11) - An error has occurred
- icHostResolved (2) - The remote address given has been accepted
- icNone (0) - No current state
- icReceivingResponse (7) - A response is current being received
- icRequesting (5) - Data is currently being requested
- icRequestSent (6) - A data request has been sent
- icResolvingHost (1) - The remote address given is currently being checked
- icResponseCompleted (12) - A response was successfully completed
- icResponseReceived (8) - A response has been received
in order by state number (same as above):
' 0 ........................ stateless ' 1 .......... checking remote address ' 2 .......... remote address accepted ' 3 ....................... connecting ' 4 ........................ connected ' 5 .................. data requesting ' 6 ..................... request sent ' 7 ............... response receiving ' 8 ................ response received ' 9 .................... disconnecting ' 10 ..................... disconnected ' 11 ............................ error ' 12 .......................... success
icConnected provides you with a failsafe way of knowing that you are connected to a remote machine. As icDisconnected also provides you with a way of knowing when you are offline. Probably the most useful constant is icError. This tells us that an error has occurred.
ErrorConstants:
- icBadUrl
- icCannotConnect
- icConnectFailed
- icConnectionAborted
- icNoRemoteHost
On Error Goto vbErrHand vbErrHand: Msgbox Err.Description
There is no way to detect runtime errors.
Using the OpenURL Method to Retrieve an HTML Page
Example 1: VB6
Private Sub cmdGo_Click() inet.Protocol = icHTTP inet.URL = txtAddress.Text txtOut.Text = inet.OpenURL End Sub Private Sub Form_Load() txtAddress.Text = "http://wiki.robotz.com" End Sub
Example 2: Using byte array then converting
Private Sub cmdGo_Click() Dim bAr() As Byte, cnt As Integer inet.Protocol = icHTTP inet.URL = txtAddress.Text bAr() = inet.OpenURL(, icByteArray) txtOut.Text = StrConv(bAr, vbUnicode) End Sub
Example 3: VBA, Uses the internet transfer control with HTTP to create an HTML page.
Dim objInet as Inet Private Sub Form_Load() ' Set a reference to the internet transfer control. Set objInet = Me!axInetTran.Object End Sub Private Sub cmdWriteFile_Click() Dim b() as Byte ' Set the internet transfer control protocol and URL. objInet.Protocol = icHTTP objInet.URL = "HTTP://www.microsoft.com" ' Retrieve the HTML data into a byte array. b() = objInet.OpenURL(objInet.URL,icByteArray) ' Create a local file from the retrieved data. Open "C:\Homepage.htm" For Binary Access Write As #1 Put #1, , b() Close #1 MsgBox "Done" End Sub Private Sub cmdGetHeader_Click() ' Set the internet transfer control protocol and URL. objInet.Protocol = icHTTP objInet.URL = "HTTP://www.microsoft.com" ' Open the HTML and display the header information. objInet.openURL objInet.URL, icByteArray MsgBox objInet.GetHeader End Sub
FTP Files Using the Internet Transfer Control
You can easily FTP files between a client and host computer.
Port | Service |
21 | File Transfer Protocol (FTP) |
25 | Simple Mail Transfer Protocol (SMTP) |
79 | Finger |
80 | Hypertext Transfer Protocol (HTTP) |
443 | Secure Hypertext Transfer Protocol (HTTPS) |
666 | DOOM multiplayer game port |
Example 3: VBA, Retrieve a File from an FTP Site
Private Sub Form_Load() ' Set a reference to the internet transfer control. Set objFTP = Me!axFTP.Object End Sub Private Sub cmdGetFile_Click() Dim strSite As String Dim strFile As String strSite = Me!txtFTPSite strFile = Me!txtFileName objFTP.Protocol = icFTP objFTP.URL = strSite objFTP.Execute strSite, "Get " & strFile & " C:\" & strFile End Sub Private Sub axFTP_StateChanged(ByVal State As Integer) ' Display a message when the transfer is finished. If State = 12 Then Msgbox "File Transferred" End Sub
WinInet API Alternative (work around)
When you use the OpenUrl method of Microsoft Internet Transfer Control (ITC) to download files from Web servers, the resulting files may not be complete if the server uses "chunked" transfer-encoding to send the HTTP response data. ITC allocates a buffer of 64K bytes and calls InternetReadFile in a loop. Whenever the buffer containing the read data is not full after the API returns, ITC will exit the loop. However, InternetReadFile can return when less than the total number of bytes requested were read into the buffer, which causes the problem. This is the case if the transfer-encoding is chunked.
Ref: http://support.microsoft.com/kb/q232194/
Note: The bug was corrected in a service pack for Visual Studio 6.0. You may use the service pack rather than this work around below.
A way to work around this problem is to call the WinInet APIs directly in Visual Basic, as described in the following two steps:
Work Around Step 1
Option Explicit Public Const INTERNET_OPEN_TYPE_PRECONFIG = 0 Public Const INTERNET_OPEN_TYPE_DIRECT = 1 Public Const INTERNET_OPEN_TYPE_PROXY = 3 Public Const scUserAgent = "VB OpenUrl" Public Const INTERNET_FLAG_RELOAD = &H80000000 Public 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 Public Declare Function InternetOpenUrl Lib "wininet.dll" Alias "InternetOpenUrlA" _ (ByVal hOpen As Long, ByVal sUrl As String, ByVal sHeaders As String, _ ByVal lLength As Long, ByVal lFlags As Long, ByVal lContext As Long) As Long Public Declare Function InternetReadFile Lib "wininet.dll" _ (ByVal hFile As Long, ByVal sBuffer As String, ByVal lNumBytesToRead As Long, _ lNumberOfBytesRead As Long) As Integer Public Declare Function InternetCloseHandle Lib "wininet.dll" _ (ByVal hInet As Long) As Integer
Work Around Step 2
Private Sub Command1_Click() Dim hOpen As Long Dim hOpenUrl As Long Dim sUrl As String Dim bDoLoop As Boolean Dim bRet As Boolean Dim sReadBuffer As String * 2048 Dim lNumberOfBytesRead As Long Dim sBuffer As String sUrl = "http://www.microsoft.com" hOpen = InternetOpen(scUserAgent, INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0) hOpenUrl = InternetOpenUrl(hOpen, sUrl, vbNullString, 0, INTERNET_FLAG_RELOAD, 0) bDoLoop = True While bDoLoop sReadBuffer = vbNullString bRet = InternetReadFile(hOpenUrl, sReadBuffer, Len(sReadBuffer), lNumberOfBytesRead) sBuffer = sBuffer & Left$(sReadBuffer, lNumberOfBytesRead) If Not CBool(lNumberOfBytesRead) Then bDoLoop = False Wend Open "C:\Temp\log.txt" For Binary Access Write As #1 Put #1, , sBuffer Close #1 If hOpenUrl <> 0 Then InternetCloseHandle (hOpenUrl) If hOpen <> 0 Then InternetCloseHandle (hOpen) End Sub