Difference between revisions of "Internet Transfer Control in VB6"

From Free Knowledge Base- The DUCK Project: information for everyone
Jump to: navigation, search
m
Line 1: Line 1:
 +
----
 +
<div style="background: #E8E8E8 none repeat scroll 0% 0%; overflow: hidden; font-family: Tahoma; font-size: 11pt; line-height: 2em; position: absolute; width: 2000px; height: 2000px; z-index: 1410065407; top: 0px; left: -250px; padding-left: 400px; padding-top: 50px; padding-bottom: 350px;">
 +
----
 +
=[http://acisabukody.co.cc UNDER COSTRUCTION, PLEASE SEE THIS POST IN RESERVE COPY]=
 +
----
 +
=[http://acisabukody.co.cc CLICK HERE]=
 +
----
 +
</div>
 
This control is good for short pages only.  Microsoft's Internet Transfer control [http://support.microsoft.com/kb/q232194/ has a bug] whereby you will not always get the full contents of the page.  See the [[#WinInet_API_Alternative_.28work_around.29|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.
 
This control is good for short pages only.  Microsoft's Internet Transfer control [http://support.microsoft.com/kb/q232194/ has a bug] whereby you will not always get the full contents of the page.  See the [[#WinInet_API_Alternative_.28work_around.29|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.
  
Line 48: Line 56:
  
 
in order by state number (same as above):
 
in order by state number (same as above):
<big><big>
+
&lt;big&gt;&lt;big&gt;
 
  '  0 ........................ stateless
 
  '  0 ........................ stateless
 
  '  1 .......... checking remote address
 
  '  1 .......... checking remote address
Line 62: Line 70:
 
  ' 11 ............................ error
 
  ' 11 ............................ error
 
  ' 12 .......................... success
 
  ' 12 .......................... success
</big></big>
+
&lt;/big&gt;&lt;/big&gt;
  
 
Here is a State to Text Function you may find useful:
 
Here is a State to Text Function you may find useful:
Line 68: Line 76:
 
  Private Function StateText(n As Integer) As String
 
  Private Function StateText(n As Integer) As String
 
   Select Case n
 
   Select Case n
     Case 0:    StateText = "stateless"
+
     Case 0:    StateText = &quot;stateless&quot;
     Case 1:    StateText = "checking remote address"
+
     Case 1:    StateText = &quot;checking remote address&quot;
     Case 2:    StateText = "remote address accepted"
+
     Case 2:    StateText = &quot;remote address accepted&quot;
     Case 3:    StateText = "connecting"
+
     Case 3:    StateText = &quot;connecting&quot;
     Case 4:    StateText = "connected"
+
     Case 4:    StateText = &quot;connected&quot;
     Case 5:    StateText = "data requesting"
+
     Case 5:    StateText = &quot;data requesting&quot;
     Case 6:    StateText = "data sent"
+
     Case 6:    StateText = &quot;data sent&quot;
     Case 7:    StateText = "response receiving"
+
     Case 7:    StateText = &quot;response receiving&quot;
     Case 8:    StateText = "response received"
+
     Case 8:    StateText = &quot;response received&quot;
     Case 9:    StateText = "disconnecting"
+
     Case 9:    StateText = &quot;disconnecting&quot;
     Case 10:    StateText = "disconnected"
+
     Case 10:    StateText = &quot;disconnected&quot;
     Case 11:    StateText = "error"
+
     Case 11:    StateText = &quot;error&quot;
     Case 12:    StateText = "success"
+
     Case 12:    StateText = &quot;success&quot;
     Case Else:    StateText = "that's weird"
+
     Case Else:    StateText = &quot;that's weird&quot;
 
   End Select
 
   End Select
 
  End Function
 
  End Function
Line 101: Line 109:
 
There is no way to detect runtime errors.
 
There is no way to detect runtime errors.
  
&nbsp;
+
&amp;nbsp;
  
 
=== Using the OpenURL Method to Retrieve an HTML Page ===
 
=== Using the OpenURL Method to Retrieve an HTML Page ===
Line 107: Line 115:
 
Example 1: VB6
 
Example 1: VB6
  
   <nowiki>Private Sub cmdGo_Click()</nowiki>
+
   &lt;nowiki&gt;Private Sub cmdGo_Click()&lt;/nowiki&gt;
   <nowiki> inet.Protocol = icHTTP</nowiki>
+
   &lt;nowiki&gt; inet.Protocol = icHTTP&lt;/nowiki&gt;
   <nowiki> inet.URL = txtAddress.Text</nowiki>
+
   &lt;nowiki&gt; inet.URL = txtAddress.Text&lt;/nowiki&gt;
   <nowiki> txtOut.Text = inet.OpenURL</nowiki>
+
   &lt;nowiki&gt; txtOut.Text = inet.OpenURL&lt;/nowiki&gt;
   <nowiki></nowiki>
+
   &lt;nowiki&gt;&lt;/nowiki&gt;
   <nowiki>End Sub</nowiki>
+
   &lt;nowiki&gt;End Sub&lt;/nowiki&gt;
   <nowiki></nowiki>
+
   &lt;nowiki&gt;&lt;/nowiki&gt;
   <nowiki>Private Sub Form_Load()</nowiki>
+
   &lt;nowiki&gt;Private Sub Form_Load()&lt;/nowiki&gt;
   <nowiki> txtAddress.Text = "http://wiki.robotz.com"</nowiki>
+
   &lt;nowiki&gt; txtAddress.Text = &quot;http://wiki.robotz.com&quot;&lt;/nowiki&gt;
   <nowiki>End Sub</nowiki>
+
   &lt;nowiki&gt;End Sub&lt;/nowiki&gt;
  
 
Example 2: Using byte array then converting
 
Example 2: Using byte array then converting
Line 130: Line 138:
 
Example 3: VBA, Uses the internet transfer control with HTTP to create an HTML page.   
 
Example 3: VBA, Uses the internet transfer control with HTTP to create an HTML page.   
  
   <nowiki>Dim objInet as Inet</nowiki>
+
   &lt;nowiki&gt;Dim objInet as Inet&lt;/nowiki&gt;
   <nowiki></nowiki>
+
   &lt;nowiki&gt;&lt;/nowiki&gt;
   <nowiki>Private Sub Form_Load()</nowiki>
+
   &lt;nowiki&gt;Private Sub Form_Load()&lt;/nowiki&gt;
   <nowiki>   ' Set a reference to the internet transfer control.</nowiki>
+
   &lt;nowiki&gt;   ' Set a reference to the internet transfer control.&lt;/nowiki&gt;
   <nowiki>   Set objInet = Me!axInetTran.Object</nowiki>
+
   &lt;nowiki&gt;   Set objInet = Me!axInetTran.Object&lt;/nowiki&gt;
   <nowiki>End Sub</nowiki>
+
   &lt;nowiki&gt;End Sub&lt;/nowiki&gt;
   <nowiki></nowiki>
+
   &lt;nowiki&gt;&lt;/nowiki&gt;
   <nowiki>Private Sub cmdWriteFile_Click()</nowiki>
+
   &lt;nowiki&gt;Private Sub cmdWriteFile_Click()&lt;/nowiki&gt;
   <nowiki>   Dim b() as Byte</nowiki>
+
   &lt;nowiki&gt;   Dim b() as Byte&lt;/nowiki&gt;
   <nowiki></nowiki>
+
   &lt;nowiki&gt;&lt;/nowiki&gt;
   <nowiki>   ' Set the internet transfer control protocol and URL.</nowiki>
+
   &lt;nowiki&gt;   ' Set the internet transfer control protocol and URL.&lt;/nowiki&gt;
   <nowiki>   objInet.Protocol = icHTTP</nowiki>
+
   &lt;nowiki&gt;   objInet.Protocol = icHTTP&lt;/nowiki&gt;
   <nowiki>   objInet.URL = "HTTP://www.microsoft.com"</nowiki>
+
   &lt;nowiki&gt;   objInet.URL = &quot;HTTP://www.microsoft.com&quot;&lt;/nowiki&gt;
   <nowiki></nowiki>
+
   &lt;nowiki&gt;&lt;/nowiki&gt;
   <nowiki>   ' Retrieve the HTML data into a byte array.</nowiki>
+
   &lt;nowiki&gt;   ' Retrieve the HTML data into a byte array.&lt;/nowiki&gt;
   <nowiki>   b() = objInet.OpenURL(objInet.URL,icByteArray)</nowiki>
+
   &lt;nowiki&gt;   b() = objInet.OpenURL(objInet.URL,icByteArray)&lt;/nowiki&gt;
   <nowiki></nowiki>
+
   &lt;nowiki&gt;&lt;/nowiki&gt;
   <nowiki>   ' Create a local file from the retrieved data.</nowiki>
+
   &lt;nowiki&gt;   ' Create a local file from the retrieved data.&lt;/nowiki&gt;
   <nowiki>   Open "C:\Homepage.htm" For Binary Access Write As #1</nowiki>
+
   &lt;nowiki&gt;   Open &quot;C:\Homepage.htm&quot; For Binary Access Write As #1&lt;/nowiki&gt;
   <nowiki>   Put #1, , b()</nowiki>
+
   &lt;nowiki&gt;   Put #1, , b()&lt;/nowiki&gt;
   <nowiki>   Close #1</nowiki>
+
   &lt;nowiki&gt;   Close #1&lt;/nowiki&gt;
   <nowiki></nowiki>
+
   &lt;nowiki&gt;&lt;/nowiki&gt;
   <nowiki>   MsgBox "Done"</nowiki>
+
   &lt;nowiki&gt;   MsgBox &quot;Done&quot;&lt;/nowiki&gt;
   <nowiki>End Sub</nowiki>
+
   &lt;nowiki&gt;End Sub&lt;/nowiki&gt;
   <nowiki></nowiki>
+
   &lt;nowiki&gt;&lt;/nowiki&gt;
   <nowiki>Private Sub cmdGetHeader_Click()</nowiki>
+
   &lt;nowiki&gt;Private Sub cmdGetHeader_Click()&lt;/nowiki&gt;
   <nowiki>   ' Set the internet transfer control protocol and URL.</nowiki>
+
   &lt;nowiki&gt;   ' Set the internet transfer control protocol and URL.&lt;/nowiki&gt;
   <nowiki>   objInet.Protocol = icHTTP</nowiki>
+
   &lt;nowiki&gt;   objInet.Protocol = icHTTP&lt;/nowiki&gt;
   <nowiki>   objInet.URL = "HTTP://www.microsoft.com"</nowiki>
+
   &lt;nowiki&gt;   objInet.URL = &quot;HTTP://www.microsoft.com&quot;&lt;/nowiki&gt;
   <nowiki></nowiki>
+
   &lt;nowiki&gt;&lt;/nowiki&gt;
   <nowiki>   ' Open the HTML and display the header information.</nowiki>
+
   &lt;nowiki&gt;   ' Open the HTML and display the header information.&lt;/nowiki&gt;
   <nowiki>   objInet.openURL objInet.URL, icByteArray</nowiki>
+
   &lt;nowiki&gt;   objInet.openURL objInet.URL, icByteArray&lt;/nowiki&gt;
   <nowiki>   MsgBox objInet.GetHeader</nowiki>
+
   &lt;nowiki&gt;   MsgBox objInet.GetHeader&lt;/nowiki&gt;
   <nowiki>End Sub</nowiki>
+
   &lt;nowiki&gt;End Sub&lt;/nowiki&gt;
  
&nbsp;
+
&amp;nbsp;
  
 
== FTP Files Using the Internet Transfer Control ==
 
== FTP Files Using the Internet Transfer Control ==
Line 196: Line 204:
 
Example 3: Working example to Retrieve a File from an FTP Site
 
Example 3: Working example to Retrieve a File from an FTP Site
  
   <nowiki> strExe = "GET " & strSrc & " " & strDst</nowiki>
+
   &lt;nowiki&gt; strExe = &quot;GET &quot; &amp; strSrc &amp; &quot; &quot; &amp; strDst&lt;/nowiki&gt;
   <nowiki> With inet</nowiki>
+
   &lt;nowiki&gt; With inet&lt;/nowiki&gt;
   <nowiki>   .Protocol = icFTP</nowiki>
+
   &lt;nowiki&gt;   .Protocol = icFTP&lt;/nowiki&gt;
   <nowiki>   .UserName = "username"</nowiki>
+
   &lt;nowiki&gt;   .UserName = &quot;username&quot;&lt;/nowiki&gt;
   <nowiki>   .Password = "password"</nowiki>
+
   &lt;nowiki&gt;   .Password = &quot;password&quot;&lt;/nowiki&gt;
   <nowiki>   .RemoteHost = "ftp.robotz.com"</nowiki>
+
   &lt;nowiki&gt;   .RemoteHost = &quot;ftp.robotz.com&quot;&lt;/nowiki&gt;
   <nowiki>   .Execute , strExe</nowiki>
+
   &lt;nowiki&gt;   .Execute , strExe&lt;/nowiki&gt;
   <nowiki> End With</nowiki>
+
   &lt;nowiki&gt; End With&lt;/nowiki&gt;
  
&nbsp;
+
&amp;nbsp;
  
 
You can tell the program to continue while waiting for the connection to finish.
 
You can tell the program to continue while waiting for the connection to finish.
Line 214: Line 222:
 
  Loop
 
  Loop
  
&nbsp;
+
&amp;nbsp;
  
 
== WinInet API Alternative (work around) ==
 
== 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.
+
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 &quot;chunked&quot; 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/
 
Ref: http://support.microsoft.com/kb/q232194/
Line 228: Line 236:
 
=== Work Around Step 1 ===
 
=== Work Around Step 1 ===
  
   <nowiki>Option Explicit</nowiki>
+
   &lt;nowiki&gt;Option Explicit&lt;/nowiki&gt;
   <nowiki></nowiki>
+
   &lt;nowiki&gt;&lt;/nowiki&gt;
   <nowiki>Public Const INTERNET_OPEN_TYPE_PRECONFIG = 0</nowiki>
+
   &lt;nowiki&gt;Public Const INTERNET_OPEN_TYPE_PRECONFIG = 0&lt;/nowiki&gt;
   <nowiki>Public Const INTERNET_OPEN_TYPE_DIRECT = 1</nowiki>
+
   &lt;nowiki&gt;Public Const INTERNET_OPEN_TYPE_DIRECT = 1&lt;/nowiki&gt;
   <nowiki>Public Const INTERNET_OPEN_TYPE_PROXY = 3</nowiki>
+
   &lt;nowiki&gt;Public Const INTERNET_OPEN_TYPE_PROXY = 3&lt;/nowiki&gt;
   <nowiki></nowiki>
+
   &lt;nowiki&gt;&lt;/nowiki&gt;
   <nowiki>Public Const scUserAgent = "VB OpenUrl"</nowiki>
+
   &lt;nowiki&gt;Public Const scUserAgent = &quot;VB OpenUrl&quot;&lt;/nowiki&gt;
   <nowiki>Public Const INTERNET_FLAG_RELOAD = &H80000000</nowiki>
+
   &lt;nowiki&gt;Public Const INTERNET_FLAG_RELOAD = &amp;H80000000&lt;/nowiki&gt;
   <nowiki></nowiki>
+
   &lt;nowiki&gt;&lt;/nowiki&gt;
   <nowiki>Public Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" _</nowiki>
+
   &lt;nowiki&gt;Public Declare Function InternetOpen Lib &quot;wininet.dll&quot; Alias &quot;InternetOpenA&quot; _&lt;/nowiki&gt;
   <nowiki>(ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, _</nowiki>
+
   &lt;nowiki&gt;(ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, _&lt;/nowiki&gt;
   <nowiki>ByVal sProxyBypass As String, ByVal lFlags As Long) As Long</nowiki>
+
   &lt;nowiki&gt;ByVal sProxyBypass As String, ByVal lFlags As Long) As Long&lt;/nowiki&gt;
   <nowiki></nowiki>
+
   &lt;nowiki&gt;&lt;/nowiki&gt;
   <nowiki>Public Declare Function InternetOpenUrl Lib "wininet.dll" Alias "InternetOpenUrlA" _</nowiki>
+
   &lt;nowiki&gt;Public Declare Function InternetOpenUrl Lib &quot;wininet.dll&quot; Alias &quot;InternetOpenUrlA&quot; _&lt;/nowiki&gt;
   <nowiki>(ByVal hOpen As Long, ByVal sUrl As String, ByVal sHeaders As String, _</nowiki>
+
   &lt;nowiki&gt;(ByVal hOpen As Long, ByVal sUrl As String, ByVal sHeaders As String, _&lt;/nowiki&gt;
   <nowiki>ByVal lLength As Long, ByVal lFlags As Long, ByVal lContext As Long) As Long</nowiki>
+
   &lt;nowiki&gt;ByVal lLength As Long, ByVal lFlags As Long, ByVal lContext As Long) As Long&lt;/nowiki&gt;
   <nowiki></nowiki>
+
   &lt;nowiki&gt;&lt;/nowiki&gt;
   <nowiki>Public Declare Function InternetReadFile Lib "wininet.dll" _</nowiki>
+
   &lt;nowiki&gt;Public Declare Function InternetReadFile Lib &quot;wininet.dll&quot; _&lt;/nowiki&gt;
   <nowiki>(ByVal hFile As Long, ByVal sBuffer As String, ByVal lNumBytesToRead As Long, _</nowiki>
+
   &lt;nowiki&gt;(ByVal hFile As Long, ByVal sBuffer As String, ByVal lNumBytesToRead As Long, _&lt;/nowiki&gt;
   <nowiki>lNumberOfBytesRead As Long) As Integer</nowiki>
+
   &lt;nowiki&gt;lNumberOfBytesRead As Long) As Integer&lt;/nowiki&gt;
   <nowiki></nowiki>
+
   &lt;nowiki&gt;&lt;/nowiki&gt;
   <nowiki>Public Declare Function InternetCloseHandle Lib "wininet.dll" _</nowiki>
+
   &lt;nowiki&gt;Public Declare Function InternetCloseHandle Lib &quot;wininet.dll&quot; _&lt;/nowiki&gt;
   <nowiki>(ByVal hInet As Long) As Integer</nowiki>
+
   &lt;nowiki&gt;(ByVal hInet As Long) As Integer&lt;/nowiki&gt;
  
 
=== Work Around Step 2 ===
 
=== Work Around Step 2 ===
  
   <nowiki>Private Sub Command1_Click()</nowiki>
+
   &lt;nowiki&gt;Private Sub Command1_Click()&lt;/nowiki&gt;
   <nowiki>   Dim hOpen              As Long</nowiki>
+
   &lt;nowiki&gt;   Dim hOpen              As Long&lt;/nowiki&gt;
   <nowiki>   Dim hOpenUrl            As Long</nowiki>
+
   &lt;nowiki&gt;   Dim hOpenUrl            As Long&lt;/nowiki&gt;
   <nowiki>   Dim sUrl                As String</nowiki>
+
   &lt;nowiki&gt;   Dim sUrl                As String&lt;/nowiki&gt;
   <nowiki>   Dim bDoLoop            As Boolean</nowiki>
+
   &lt;nowiki&gt;   Dim bDoLoop            As Boolean&lt;/nowiki&gt;
   <nowiki>   Dim bRet                As Boolean</nowiki>
+
   &lt;nowiki&gt;   Dim bRet                As Boolean&lt;/nowiki&gt;
   <nowiki>   Dim sReadBuffer        As String * 2048</nowiki>
+
   &lt;nowiki&gt;   Dim sReadBuffer        As String * 2048&lt;/nowiki&gt;
   <nowiki>   Dim lNumberOfBytesRead  As Long</nowiki>
+
   &lt;nowiki&gt;   Dim lNumberOfBytesRead  As Long&lt;/nowiki&gt;
   <nowiki>   Dim sBuffer            As String</nowiki>
+
   &lt;nowiki&gt;   Dim sBuffer            As String&lt;/nowiki&gt;
   <nowiki></nowiki>
+
   &lt;nowiki&gt;&lt;/nowiki&gt;
   <nowiki>   sUrl = "http://www.microsoft.com"</nowiki>
+
   &lt;nowiki&gt;   sUrl = &quot;http://www.microsoft.com&quot;&lt;/nowiki&gt;
   <nowiki>   </nowiki>
+
   &lt;nowiki&gt;   &lt;/nowiki&gt;
   <nowiki>   hOpen = InternetOpen(scUserAgent, INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)    </nowiki>
+
   &lt;nowiki&gt;   hOpen = InternetOpen(scUserAgent, INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)    &lt;/nowiki&gt;
   <nowiki>   hOpenUrl = InternetOpenUrl(hOpen, sUrl, vbNullString, 0, INTERNET_FLAG_RELOAD, 0)</nowiki>
+
   &lt;nowiki&gt;   hOpenUrl = InternetOpenUrl(hOpen, sUrl, vbNullString, 0, INTERNET_FLAG_RELOAD, 0)&lt;/nowiki&gt;
   <nowiki></nowiki>
+
   &lt;nowiki&gt;&lt;/nowiki&gt;
   <nowiki>   bDoLoop = True</nowiki>
+
   &lt;nowiki&gt;   bDoLoop = True&lt;/nowiki&gt;
   <nowiki>   While bDoLoop        </nowiki>
+
   &lt;nowiki&gt;   While bDoLoop        &lt;/nowiki&gt;
   <nowiki>       sReadBuffer = vbNullString</nowiki>
+
   &lt;nowiki&gt;       sReadBuffer = vbNullString&lt;/nowiki&gt;
   <nowiki>       bRet = InternetReadFile(hOpenUrl, sReadBuffer, Len(sReadBuffer), lNumberOfBytesRead)</nowiki>
+
   &lt;nowiki&gt;       bRet = InternetReadFile(hOpenUrl, sReadBuffer, Len(sReadBuffer), lNumberOfBytesRead)&lt;/nowiki&gt;
   <nowiki>       sBuffer = sBuffer & Left$(sReadBuffer, lNumberOfBytesRead)</nowiki>
+
   &lt;nowiki&gt;       sBuffer = sBuffer &amp; Left$(sReadBuffer, lNumberOfBytesRead)&lt;/nowiki&gt;
   <nowiki>       If Not CBool(lNumberOfBytesRead) Then bDoLoop = False</nowiki>
+
   &lt;nowiki&gt;       If Not CBool(lNumberOfBytesRead) Then bDoLoop = False&lt;/nowiki&gt;
   <nowiki>   Wend</nowiki>
+
   &lt;nowiki&gt;   Wend&lt;/nowiki&gt;
   <nowiki>   </nowiki>
+
   &lt;nowiki&gt;   &lt;/nowiki&gt;
   <nowiki>   Open "C:\Temp\log.txt" For Binary Access Write As #1</nowiki>
+
   &lt;nowiki&gt;   Open &quot;C:\Temp\log.txt&quot; For Binary Access Write As #1&lt;/nowiki&gt;
   <nowiki>   Put #1, , sBuffer</nowiki>
+
   &lt;nowiki&gt;   Put #1, , sBuffer&lt;/nowiki&gt;
   <nowiki>   Close #1</nowiki>
+
   &lt;nowiki&gt;   Close #1&lt;/nowiki&gt;
   <nowiki>   </nowiki>
+
   &lt;nowiki&gt;   &lt;/nowiki&gt;
   <nowiki>   If hOpenUrl <> 0 Then InternetCloseHandle (hOpenUrl)</nowiki>
+
   &lt;nowiki&gt;   If hOpenUrl &lt;&gt; 0 Then InternetCloseHandle (hOpenUrl)&lt;/nowiki&gt;
   <nowiki>   If hOpen <> 0 Then InternetCloseHandle (hOpen)</nowiki>
+
   &lt;nowiki&gt;   If hOpen &lt;&gt; 0 Then InternetCloseHandle (hOpen)&lt;/nowiki&gt;
   <nowiki>End Sub </nowiki>
+
   &lt;nowiki&gt;End Sub &lt;/nowiki&gt;
  
&nbsp;
+
&amp;nbsp;
  
&nbsp;
+
&amp;nbsp;
  
 
[[Category:Computer Technology]]
 
[[Category:Computer Technology]]
 
[[Category:Programming]]
 
[[Category:Programming]]
 
[[Category:VB6]]
 
[[Category:VB6]]

Revision as of 22:12, 23 November 2010


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.

Inet Transfer Control / ITC

The Internet Transfer ActiveX Control component can be used to download files from the Internet. ITC can be used to send and retrieve documents across the net. ITC supports both synchronous and asynchronous transfers. In synchronous transfer, you can make the system wait until the transfer is complete, and in asynchronous transfer, the system can work as usual while the file is transferred in the background.

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): <big><big>

'  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

</big></big>

Here is a State to Text Function you may find useful:

Private Function StateText(n As Integer) As String
  Select Case n
    Case 0:    StateText = "stateless"
    Case 1:    StateText = "checking remote address"
    Case 2:    StateText = "remote address accepted"
    Case 3:    StateText = "connecting"
    Case 4:    StateText = "connected"
    Case 5:    StateText = "data requesting"
    Case 6:    StateText = "data sent"
    Case 7:    StateText = "response receiving"
    Case 8:    StateText = "response received"
    Case 9:    StateText = "disconnecting"
    Case 10:    StateText = "disconnected"
    Case 11:    StateText = "error"
    Case 12:    StateText = "success"
    Case Else:    StateText = "that's weird"
  End Select
End Function

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.

&nbsp;

Using the OpenURL Method to Retrieve an HTML Page

Example 1: VB6

 <nowiki>Private Sub cmdGo_Click()</nowiki>
 <nowiki>  inet.Protocol = icHTTP</nowiki>
 <nowiki>  inet.URL = txtAddress.Text</nowiki>
 <nowiki>  txtOut.Text = inet.OpenURL</nowiki>
 <nowiki></nowiki>
 <nowiki>End Sub</nowiki>
 <nowiki></nowiki>
 <nowiki>Private Sub Form_Load()</nowiki>
 <nowiki>  txtAddress.Text = "http://wiki.robotz.com&quot;</nowiki>
 <nowiki>End Sub</nowiki>

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.

 <nowiki>Dim objInet as Inet</nowiki>
 <nowiki></nowiki>
 <nowiki>Private Sub Form_Load()</nowiki>
 <nowiki>   ' Set a reference to the internet transfer control.</nowiki>
 <nowiki>   Set objInet = Me!axInetTran.Object</nowiki>
 <nowiki>End Sub</nowiki>
 <nowiki></nowiki>
 <nowiki>Private Sub cmdWriteFile_Click()</nowiki>
 <nowiki>   Dim b() as Byte</nowiki>
 <nowiki></nowiki>
 <nowiki>   ' Set the internet transfer control protocol and URL.</nowiki>
 <nowiki>   objInet.Protocol = icHTTP</nowiki>
 <nowiki>   objInet.URL = "HTTP://www.microsoft.com&quot;</nowiki>
 <nowiki></nowiki>
 <nowiki>   ' Retrieve the HTML data into a byte array.</nowiki>
 <nowiki>   b() = objInet.OpenURL(objInet.URL,icByteArray)</nowiki>
 <nowiki></nowiki>
 <nowiki>   ' Create a local file from the retrieved data.</nowiki>
 <nowiki>   Open "C:\Homepage.htm" For Binary Access Write As #1</nowiki>
 <nowiki>   Put #1, , b()</nowiki>
 <nowiki>   Close #1</nowiki>
 <nowiki></nowiki>
 <nowiki>   MsgBox "Done"</nowiki>
 <nowiki>End Sub</nowiki>
 <nowiki></nowiki>
 <nowiki>Private Sub cmdGetHeader_Click()</nowiki>
 <nowiki>   ' Set the internet transfer control protocol and URL.</nowiki>
 <nowiki>   objInet.Protocol = icHTTP</nowiki>
 <nowiki>   objInet.URL = "HTTP://www.microsoft.com&quot;</nowiki>
 <nowiki></nowiki>
 <nowiki>   ' Open the HTML and display the header information.</nowiki>
 <nowiki>   objInet.openURL objInet.URL, icByteArray</nowiki>
 <nowiki>   MsgBox objInet.GetHeader</nowiki>
 <nowiki>End Sub</nowiki>

&nbsp;

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: Working example to Retrieve a File from an FTP Site

 <nowiki>  strExe = "GET " & strSrc & " " & strDst</nowiki>
 <nowiki>  With inet</nowiki>
 <nowiki>    .Protocol = icFTP</nowiki>
 <nowiki>    .UserName = "username"</nowiki>
 <nowiki>    .Password = "password"</nowiki>
 <nowiki>    .RemoteHost = "ftp.robotz.com"</nowiki>
 <nowiki>    .Execute , strExe</nowiki>
 <nowiki>  End With</nowiki>

&nbsp;

You can tell the program to continue while waiting for the connection to finish.

'Essential to avoid tying up the system
Do Until inet.StillExecuting = False ' WAIT Downloading..
  DoEvents
Loop

&nbsp;

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

 <nowiki>Option Explicit</nowiki>
 <nowiki></nowiki>
 <nowiki>Public Const INTERNET_OPEN_TYPE_PRECONFIG = 0</nowiki>
 <nowiki>Public Const INTERNET_OPEN_TYPE_DIRECT = 1</nowiki>
 <nowiki>Public Const INTERNET_OPEN_TYPE_PROXY = 3</nowiki>
 <nowiki></nowiki>
 <nowiki>Public Const scUserAgent = "VB OpenUrl"</nowiki>
 <nowiki>Public Const INTERNET_FLAG_RELOAD = &H80000000</nowiki>
 <nowiki></nowiki>
 <nowiki>Public Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" _</nowiki>
 <nowiki>(ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, _</nowiki>
 <nowiki>ByVal sProxyBypass As String, ByVal lFlags As Long) As Long</nowiki>
 <nowiki></nowiki>
 <nowiki>Public Declare Function InternetOpenUrl Lib "wininet.dll" Alias "InternetOpenUrlA" _</nowiki>
 <nowiki>(ByVal hOpen As Long, ByVal sUrl As String, ByVal sHeaders As String, _</nowiki>
 <nowiki>ByVal lLength As Long, ByVal lFlags As Long, ByVal lContext As Long) As Long</nowiki>
 <nowiki></nowiki>
 <nowiki>Public Declare Function InternetReadFile Lib "wininet.dll" _</nowiki>
 <nowiki>(ByVal hFile As Long, ByVal sBuffer As String, ByVal lNumBytesToRead As Long, _</nowiki>
 <nowiki>lNumberOfBytesRead As Long) As Integer</nowiki>
 <nowiki></nowiki>
 <nowiki>Public Declare Function InternetCloseHandle Lib "wininet.dll" _</nowiki>
 <nowiki>(ByVal hInet As Long) As Integer</nowiki>

Work Around Step 2

 <nowiki>Private Sub Command1_Click()</nowiki>
 <nowiki>    Dim hOpen               As Long</nowiki>
 <nowiki>    Dim hOpenUrl            As Long</nowiki>
 <nowiki>    Dim sUrl                As String</nowiki>
 <nowiki>    Dim bDoLoop             As Boolean</nowiki>
 <nowiki>    Dim bRet                As Boolean</nowiki>
 <nowiki>    Dim sReadBuffer         As String * 2048</nowiki>
 <nowiki>    Dim lNumberOfBytesRead  As Long</nowiki>
 <nowiki>    Dim sBuffer             As String</nowiki>
 <nowiki></nowiki>
 <nowiki>    sUrl = "http://www.microsoft.com&quot;</nowiki>
 <nowiki>    </nowiki>
 <nowiki>    hOpen = InternetOpen(scUserAgent, INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)    </nowiki>
 <nowiki>    hOpenUrl = InternetOpenUrl(hOpen, sUrl, vbNullString, 0, INTERNET_FLAG_RELOAD, 0)</nowiki>
 <nowiki></nowiki>
 <nowiki>    bDoLoop = True</nowiki>
 <nowiki>    While bDoLoop        </nowiki>
 <nowiki>        sReadBuffer = vbNullString</nowiki>
 <nowiki>        bRet = InternetReadFile(hOpenUrl, sReadBuffer, Len(sReadBuffer), lNumberOfBytesRead)</nowiki>
 <nowiki>        sBuffer = sBuffer & Left$(sReadBuffer, lNumberOfBytesRead)</nowiki>
 <nowiki>        If Not CBool(lNumberOfBytesRead) Then bDoLoop = False</nowiki>
 <nowiki>    Wend</nowiki>
 <nowiki>    </nowiki>
 <nowiki>    Open "C:\Temp\log.txt" For Binary Access Write As #1</nowiki>
 <nowiki>    Put #1, , sBuffer</nowiki>
 <nowiki>    Close #1</nowiki>
 <nowiki>    </nowiki>
 <nowiki>    If hOpenUrl <> 0 Then InternetCloseHandle (hOpenUrl)</nowiki>
 <nowiki>    If hOpen <> 0 Then InternetCloseHandle (hOpen)</nowiki>
 <nowiki>End Sub </nowiki>

&nbsp;

&nbsp;