Changes

Internet Transfer Control in VB6

6,470 bytes added, 17:41, 24 November 2010
Reverted edits by [[Special:Contributions/Atekysepiko|Atekysepiko]] ([[User_talk:Atekysepiko|Talk]]); changed back to last version by [[User:Admin|Admin]]
The following lines were added (+) and removed (-):
:PropertiesThis 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.::AccessType property ::*icUseDefault* [http://msdn2.microsoft.com/en-us/vstudio/aa718364.aspx Visual Studio Service Pack 6]::*icDirect ::*icNamedProxy== Inet Transfer Control / ITC ==::protocols ::*icUnknownThe '''[http://support.microsoft.com/kb/q163653/ 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.::*icDefault ::*icFTPProperties::*icGopher:AccessType property::*icHTTP:*icUseDefault::*icHTTPS:*icDirect:*icNamedProxy:protocols:*icUnknown:*icDefault:*icFTP:*icGopher:*icHTTP:*icHTTPS*URL - The complete URL of what you want to communicate with. e.g. http://www.vbsquare.com/*URL - The complete URL of what you want to communicate with. e.g. http://wiki.robotz.com/== Inet Control States ===== Inet Control States ===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== Using the OpenURL Method to Retrieve an HTML Page ===== Using the OpenURL Method to Retrieve an HTML Page ===Example 2: VBA, Uses the internet transfer control with HTTP to create an HTML page.   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.   Example 3: VBA, Retrieve a File from an FTP SiteExample 3: Working example to Retrieve a File from an FTP Site   <nowiki>Private Sub Form_Load()</nowiki>   <nowiki> strExe = "GET " & strSrc & " " & strDst</nowiki>   <nowiki>  ' Set a reference to the internet transfer control.</nowiki>   <nowiki> With inet</nowiki>   <nowiki>  Set objFTP = Me!axFTP.Object</nowiki>   <nowiki>    .Protocol = icFTP</nowiki>   <nowiki>End Sub</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>Private Sub cmdGetFile_Click()</nowiki>   <nowiki>Public Const INTERNET_OPEN_TYPE_PRECONFIG = 0</nowiki>  <nowiki>  Dim strSite As String</nowiki>   <nowiki>Public Const INTERNET_OPEN_TYPE_DIRECT = 1</nowiki>  <nowiki>  Dim strFile As String</nowiki>   <nowiki>Public Const INTERNET_OPEN_TYPE_PROXY = 3</nowiki>  <nowiki>  strSite = Me!txtFTPSite</nowiki>   <nowiki>   strFile = Me!txtFileName</nowiki>   <nowiki>   objFTP.Protocol = icFTP</nowiki>  <nowiki>  objFTP.URL = strSite</nowiki>  <nowiki>  objFTP.Execute strSite, "Get " & strFile & " C:\" & strFile</nowiki>  <nowiki>End Sub</nowiki>   <nowiki>Private Sub axFTP_StateChanged(ByVal State As Integer)</nowiki>   <nowiki>Public Const scUserAgent = "VB OpenUrl"</nowiki>   <nowiki>  ' Display a message when the transfer is finished.</nowiki>  <nowiki>Public Const INTERNET_FLAG_RELOAD = &H80000000</nowiki>   <nowiki>  If State = 12 Then Msgbox "File Transferred"</nowiki>  <nowiki></nowiki>   <nowiki>End Sub</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"</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>
Bureaucrat, administrator
16,192
edits