Changes

Internet Transfer Control in VB6

4,029 bytes added, 22:04, 6 February 2008
The following lines were added (+) and removed (-):
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.  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 section of this page for details on the work around.  == Inet Transfer Control ==== Inet Control States ===== Inet Control States ===== Using the OpenURL Method to Retrieve an HTML Page ===== Using the OpenURL Method to Retrieve an HTML Page ===&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.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"</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